Mail Archives: djgpp/1998/03/29/02:22:53
Jeff W./DMT wrote:
>
> I have one example file that was written in C and came with
> instructions on how to compile it with the included object file to
> produce an executable file. That worked fine. I wrote a very basic
> C++ file, and at the command line tried doing the same thing to link
> my program with the same object file:
> gcc myfile.cc -O2 -o myfile.exe -lalleg objfile.o and for some reason
> gcc complains that it can't find the functions which are defined in
> the .O file.
This is a well-known occurrence. It happens because the C++ compiler
assumes that the functions which are declared in the header file are C++
functions, and therefore mangles their names according to the C++
name-mangling rules. When it looks in the object file (a library is
merely a collection of object files), it doesn't find the proper mangled
names and so it complains. The solution is simple - in the object
file's header file, wrap all the function declarations (and ideally the
entire header) with the following code:
#ifdef __cplusplus
extern "C" {
#endif
/* code */
#ifdef __cplusplus
}
#endif
That will cause the functions to be interpreted as C and the correct
names will be generated.
hth
--
---------------------------------------------------------------------
| John M. Aldrich | "Animals can be driven crazy by pla- |
| aka Fighteer I | cing too many in too small a pen. |
| mailto:fighteer AT cs DOT com | Homo sapiens is the only animal that |
| http://www.cs.com/fighteer | voluntarily does this to himself." |
---------------------------------------------------------------------
- Raw text -