Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Help on Makefile

Posted on 2005-03-18
Medium Priority
Last Modified: 2011-09-20
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?

Question by:BooBoo1025

Author Comment

ID: 13579878
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

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

LVL 86

Assisted Solution

jkr earned 80 total points
ID: 13580255
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()'.

Author Comment

ID: 13581244
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?

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 13581413
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.


LVL 12

Expert Comment

ID: 13581589
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);


LVL 12

Accepted Solution

stefan73 earned 1920 total points
ID: 13586592
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++.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question