delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1997/03/27/14:42:27

From: jqb AT netcom DOT com (Jim Balter)
Subject: Re: ot: cross linking problem with g++ and gcc
27 Mar 1997 14:42:27 -0800 :
Approved: cygnus DOT gnu-win32 AT cygnus DOT com
Distribution: cygnus
Message-ID: <333A0B5E.782A.cygnus.gnu-win32@netcom.com>
References: <3338B867 DOT 5E6 AT intcomm DOT net>
Mime-Version: 1.0
X-Mailer: Mozilla 3.01Gold (WinNT; I)
Original-To: Atlas <atlas AT intcomm DOT net>
Original-CC: gnu-win32 AT cygnus DOT com
Original-Sender: owner-gnu-win32 AT cygnus DOT com

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
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019