?
Solved

Help on Makefile

Posted on 2005-03-18
6
Medium Priority
?
273 Views
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?

Thanks,
BB
0
Comment
Question by:BooBoo1025
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Author Comment

by:BooBoo1025
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

*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
 
LVL 86

Assisted Solution

by:jkr
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()'.
0
 

Author Comment

by:BooBoo1025
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?

Thanks,
Hai
0
Industry Leaders: 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!

 
LVL 8

Expert Comment

by:novitiate
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.

_novi_

0
 
LVL 12

Expert Comment

by:stefan73
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);


Cheers!

Stefan
0
 
LVL 12

Accepted Solution

by:
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++.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

770 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