building c/c++ compilation errors

Status
Not open for further replies.

kuya

Ars Scholae Palatinae
881
(saw the new symposium, so i moved the thread over to this one)

hi all,
how do you get gcc to build a mixed c/c++ program?
i'm using rpcgen to build a networking app on a solaris machine (gcc version 2.95.2).

running rpcgen on my .x file creates the following C files for me
remote_db_clnt.c
remote_db_svc.c
remote_db_xdr.c
remote_db.h

the app involves database'ish code, so i decided to use the string class and map provided by the STL in C++. but i'm having issues building the app using gcc.

% gcc -x c++ remote_db_server.cpp -x c remote_db_svc.c -x cremote_db_xdr.c -lnsl -lsocket

produces the following errors:
Undefined first referenced
symbol in file
cout /var/tmp/ccMhmu6j.o
string_char_traits::compare(char const *, char const *, unsigned int)/var/tmp/ccMhmu6j.o

and then some more linker errors. i don't think i'm even really at the point where i'll extern "C" the functions between languages.

does anyonne know how to fix this?

thanks
 

kuya

Ars Scholae Palatinae
881
peter b:
i think it's telling me, it wasn't linking correctly to the standard c++ libraries like iostream. i'm not sure myself which exactly libraries to link it too for standard libraries like iostream, algorithm, map. i think i had tried to force bastard solaris to link it by using the gcc option -liostream and -lmap, but it was unable to find those.

Furyu:
i used
% g++ -x c++ remote_db_server.cpp -x c remote_db_svc.c -x cremote_db_xdr.c -lnsl -lsocket

and it yields the following linking errors:
Undefined first referenced
symbol in file
lookup_name_1 /var/tmp/ccF9sYpc.o
xdr_result /var/tmp/ccF9sYpc.o
xdr_db_entry /var/tmp/ccF9sYpc.o
remote_from_database_1 /var/tmp/ccF9sYpc.o
list_1 /var/tmp/ccF9sYpc.o
add_to_database_1 /var/tmp/ccF9sYpc.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

those are rpc functions i had specified in the .x protocol file for rpcgen. rpcgen then took that .x file and created a header file (which i included in the .cpp file) including typedefs, #defines and extern'ing those above mentioned (lookup_name_1, list_1) functions. i prototyped and defined those functions in the c++ file, but again, it didn't link.

thanks for the help, i'm going to keep trying, but for some reason i thought it would be easier to get mixed c/c++ to build...
 

kuya

Ars Scholae Palatinae
881
whoops

found out now that you need to extern "C" any c header files you include like

extern "C" {
#include <rpc/rpc.h>
}

so now it builds, working however is an entirely different matter...

the extern "C" will prevent name mangling by the c++ compiler. also, the code with the main loop needs to be compiled by the c++ compiler because any c++ code has static initialization and other stuff occurring BEFORE main() is entered. and the linker needs to be c++ too, to link to the standard c++ libraries like iostream.

...in case anyone needs it for future reference
 
Well instead of

extern "C" {
#include
}

I always prefered to fix the headers themselves, once and for all:

someheader.h:

#if defined(__cplusplus)
extern "C" {
#endif

\\ header stuff

#if defined(__cplusplus)
};
#endif


Otherwise, every time you include the header in C++ source, you have to remember to wrap it in extern"C", which can be a pain on a larger project. This way takes care of itself. :)
 

Tristram

Ars Scholae Palatinae
1,428
Subscriptor
Well, by the ISO standards, only C++ can call/use C libraries. No standard exists for calling C++ libraries from C code. Don't get your knickers in a twist! I KNOW most compilers will let you mix and match. This problem is not addressed by the current standards, hence no portable / universal answer exists.

In the simplest case, where you have all source code, and the C code is ANSI compiant, you should be able to simply recompile the C code as C++. There are some suble, exotic cases where ANSI C and ANSI C++ behave differently for the same source. While that is unlikely (in the extreme: it is very hard to write ANSI C that is not ANSI C++), many compilers like gcc, MS VC, and others have propriertary extensions, whose use can considerably complicate manners.

The next simplest case is when the libraries are dynamic shared objects (.dll or .so). The worst case is C++ static libraries with header files only.
 
Status
Not open for further replies.