Problem linking object with extern "C"
Andrew Hardy
Andrew.Hardy@marconi.com
Tue Aug 2 14:01:00 GMT 2005
I am compiling some 'C' source code and some 'C++' source code. The C++
source code has 'C' functions with C++ features in the function body. The
C++ header, where C___plusplus is defined has the function declarations in
an extern "C" block.
It is my understanding that this will enable the linker to assosiate
function calls in the 'C' compiled object file with the 'C' functions in
the 'C++' compiled object file. However although the C code and the C++
code both compile successfully I cannot get them to link. Although I felt
I had got the syntax etc correct, I have tried several changes in a vain
attempt to get this working, including compiling the C++ with CC and with
gcc, moving the extern "C" around the header include instaed of in the
header file around the function declarations and also placing the function
definitions in an extern "C" block, and others, all to no avail.
If I remove any C++ features from the function definitions linking
succeeds, but as soon as I introduce any C++ feature to the function body,
then compile succeeds, but linking fails. I undewrstood the idea of this
feature was to allow C++ code to provide a 'C' interface for C code to call
and pass agruments!
Any help would be grately appreciated.
Here is the header:
#ifdef __cplusplus
#include <string>
#include <ostream>
extern "C"
{
#endif
void loadLookupData(void);
void getValueName(char *ob, char *val, char *valName);
void getValueNameForRole(char *ob, char *val, char *rt, char *rv,
char *valName);
#ifdef __cplusplus
}
#endif
Here is the source code ( I plan to have much more in the functions, but am
trying to get the lnking to work.
#include "import_lookup.h"
void loadLookupData(void)
{
}
void getValueName(char *ob, char *val, char *valName)
{
char x[100] = "hi there";
std::string rtString("0");
}
void getValueNameForRole(char *ob, char *val, char *rt, char *rv,
char *valName)
{
}
Here is a fragment of the compile all with gcc:
gcc -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes
-fexceptions -DHAVE_EXPAT_CONFIG_H -I../expat/lib -I. -static -o
find_role_characteristics look_up_support_functions.o
find_role_characteristics.o -L../expat/lib -lexpat
gcc -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes
-fexceptions -DHAVE_EXPAT_CONFIG_H -I../expat/lib -I. -o
import_lookup.o -c import_lookup.cpp
import_lookup.cpp: In function `void getValueName(char *, char *,
char *)':
import_lookup.cpp:12: warning: unused variable `char x[100]'
gcc -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes
-fexceptions -DHAVE_EXPAT_CONFIG_H -I../expat/lib -I. -o
import_functions.o -c import_functions.c
gcc -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes
-fexceptions -DHAVE_EXPAT_CONFIG_H -I../expat/lib -I. -o
import_ideas.o -c import_ideas.c
import_ideas.c: In function `end':
import_ideas.c:140: warning: unused variable `i'
gcc -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes
-fexceptions -DHAVE_EXPAT_CONFIG_H -I../expat/lib -I. -static -o
import_ideas import_lookup.o import_functions.o import_ideas.o
-L../expat/lib -lexpat -L/usr/lib/ -L../../../arch_lib
-L/usr/informix/lib -L/usr/informix/lib/esql -L/usr/informix/lib/cli
-lcurses -lthsql -lthasf -lthgen -lthos -lifgls -lifglx -lsocket
-lnsl -lgen
Undefined first referenced
symbol in file
ostream::operator<<(char const *) import_lookup.o
endl(ostream &) import_lookup.o
__out_of_range(char const *) import_lookup.o
__length_error(char const *) import_lookup.o
cerr import_lookup.o
ld: fatal: Symbol referencing errors. No output written to
import_ideas
collect2: ld returned 1 exit status
*** Error code 1
make: Fatal error: Command failed for target `import_ideas'
If I compile import_lookup.cpp with CC I get this:
CC -mt -O -I../../../../arch/source/library/include -DARCH_EPILOGUE
-c -o import_lookup.o import_lookup.cpp
gcc -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes
-fexceptions -DHAVE_EXPAT_CONFIG_H -I../expat/lib -I. -o
import_functions.o -c import_functions.c
gcc -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes
-fexceptions -DHAVE_EXPAT_CONFIG_H -I../expat/lib -I. -o
import_ideas.o -c import_ideas.c
import_ideas.c: In function `end':
import_ideas.c:140: warning: unused variable `i'
gcc -g -O2 -Wall -Wmissing-prototypes -Wstrict-prototypes
-fexceptions -DHAVE_EXPAT_CONFIG_H -I../expat/lib -I. -static -o
import_ideas import_lookup.o import_functions.o import_ideas.o
-L../expat/lib -lexpat -L/usr/lib/ -L../../../arch_lib
-L/usr/informix/lib -L/usr/informix/lib/esql -L/usr/informix/lib/cli
-lcurses -lthsql -lthasf -lthgen -lthos -lifgls -lifglx -lsocket
-lnsl -lgen
Undefined first referenced
symbol in file
__1cDstdMbasic_string4Ccn0ALchar_traits4Cc__n0AJallocator4Cc___J__nullref_
import_lookup.o
__1c2k6Fpv_v_ import_lookup.o
__1cDstdMbasic_string4Ccn0ALchar_traits4Cc__n0AJallocator4Cc___2t6Mpkcrkn0C__v_
import_lookup.o
ld: fatal: Symbol referencing errors. No output written to
import_ideas
collect2: ld returned 1 exit status
*** Error code 1
make: Fatal error: Command failed for target `import_ideas'
Andrew H
More information about the Gcc-help
mailing list