From: "John M. Aldrich" Newsgroups: comp.os.msdos.djgpp Subject: Re: Linking an object file in RHIDE/with GCC Date: Sun, 29 Mar 1998 00:57:36 -0500 Organization: Two pounds of chaos and a pinch of salt. Lines: 42 Message-ID: <351DE2D0.7600@cs.com> References: <351d8edd DOT 107493 AT news DOT ziplink DOT net> NNTP-Posting-Host: ppp245.cs.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk 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." | ---------------------------------------------------------------------