Subject: Re: ot: cross linking problem with g++ and gcc
From: [email-masked] (Jim Balter)

27 Mar 1997 14:42:27 -0800
Approved: [email-masked]
Distribution: cygnus
Message-ID: <[email-masked]>
References: <[email-masked]>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Mailer: Mozilla 3.01Gold (WinNT; I)
Original-To: Atlas <[email-masked]>
Original-CC: [email-masked]
Original-Sender: [email-masked]

Atlas wrote:

> The linker error I get is:
> 
> undefined symbol "run" in file such and such, ect, ect.
> 
> I have the run function defined in my .cc file, and it is being called
> from my .c file. The .c file has the correct prototype for the run
> function but is unable to find it in the .cc file on link. If anyone
> has any ideas, lets me know..

The names of C++ functions are "mangled" to include information about
the types of the the function's arguments.  To use C functions from C++
source or vice versa, they must be declared extern "C".  You can group
these with extern "C" { ... }

If you look at the standard header files, you will see things like

#ifdef __cplusplus
extern "C" {
#endif

[declarations of C functions]

#ifdef __cplusplus
}
#endif

or, more cleanly, define in some common header file

#ifdef __cplusplus
#define BEGIN_C_DECLS extern "C" {
#define END_C_DECLS }
#else
#define BEGIN_C_DECLS
#define END_C_DECLS
#endif

and then

BEGIN_C_DECLS

[declarations of C functions]

END_C_DECLS

You should follow a similar strategy in the header files
for your project.  Make sure that every global function
is declared in one of those header files, and that the
appropriate header files are included.  Every function which
is defined or used in C code must be declared extern "C".
Try not to do so for functions that are only used in C++ code,
since that will lose type-safe chacks, but it should otherwise be
harmless.  As long as every global function is declared in a header
file and each header file is always included in every file that uses
any functions from it, the link should succeed.  (These are basic
principles of C/C++ engineering, regardless of whether you are
merging the two).

--
<J Q B>
-
For help on using this list, send a message to
"[email-masked]" with one line of text: "help".