Help on Makefile

Hi, I have a problem when compiling a c++ file, which using functions from other file in my Makefile. For instance, I am calling a function in cFile.c from my c++File.cpp. Since the cFile.c doesn't have the cFile.h for me to include (with extern), I thought I can somehow tell it in the Makefile. How? I don't know. Can you tell me please?

Thanks,
BB
BooBoo1025Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BooBoo1025Author Commented:
I hope this additional info help clear my question. Here is what I do and error that I get (please assume some other path include in it, and 'foo' is the function that I call from the cFile.c):

.cpp.o : g++ -c -g -O -I./ c++File.cpp -o c++File.o

*Error:
c++File.cpp: In function 'int main(int, char**, char**)
c++File.cpp:236: error: 'foo' undeclared (first use this function)
...

Thanks,
BB
0
jkrCommented:
This seems to not be a matter of the Makefile, but which header files you are #including. If you have created header files that you need to use in more tha a single project, I'd suggest placing them alltogether in a directory of your choice an add that one to your "INCLUDE" path environment variable. And, of course, to add the respective #include directive to all files in which you need to use 'foo()'.
0
BooBoo1025Author Commented:
In this case, the cFile.c doesn't have the header file. If it has the header file, I already do the #include in my source file (c++File.cpp). BTW, I didn't implement the cFile.c. It was written by someone else. I want to able to compile my c++File.cpp and create its object file. Of course, this file is calling some functions from the cFile.c. What is the syntax to let the main function knows that function 'foo' is called from cFile.c?

Thanks,
Hai
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

novitiateCommented:
that's pretty simple, you have the c code and you can create the header file.

You can either create a header file with the functions that you want to use from c file.

Or you can just declare them in your cpp, linker will get them obj file. Since the compiler will need a declaration its complaining for that.

like jkr said it has nothing to do with the make file. The declaration should be available for the dependent source to compile.

_novi_

0
stefan73Commented:
Hi BooBoo1025,
> What is the syntax to let the main function knows that function 'foo'
> is called from cFile.c?

C++ uses name mangling in functions/methods to support overloading, name spaces, etc. C wants plain names.
Use extern "C" { ... } to switch off name mangling.


You can simply do:

// C++ function prototype for header
extern "C" { void foo(void); }

// C++ function implementation
extern "C" {
void foo(void){
    cout << "Now we have C++ called from C" << endl;
}
}

// C prototype would simply be
void foo(void);


Cheers!

Stefan
0
stefan73Commented:
BTW: Switching off name mangling works the other way round, too. If you want to call C functions from C++, simply declare their prototypes as:

extern "C" { void my_c_func(void); }

...and you can call them from C++.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.