tags:

views:

158

answers:

2

I have a dependancy library (libfcgi) that I compiled with g++ (GCC v4.4 MinGW) using the following calls:

g++ -Iinclude -c -O2 *.c
ar rcs ../libfcgi.a *.o

Now, my main project is built like so:

g++ -Idependancies\libfcgi\include -Ldependancies -O2 -lfcgi *.cpp

g++ apparently finds libfcgi.a, but yet it fails to link to the following references:

'FCGI_printf'
'FCGI_Accept'

In the libfcgi sources, these functions are defined as follows:

#ifdef __cplusplus
extern "C" {
#endif
//...
DLLAPI int FCGI_printf(const char *format, ...);  
DLLAPI int FCGI_Accept(void);
//...
#ifdef __cplusplus
}
#endif

where DLLAPI is nothing (as it isn't compiled as a shared library) and __cplusplus is defined (g++).

Looking at libfcgi.a, those functions are exported as '_FCGI_Accept' and '_FCGI_printf', so with an underscore in front. That's what seems to hinder g++ to find them.

I thought using export "C" would suffice to link to a C function in C++, so what am I doing wrong?

Thanks :)

+3  A: 

If you have the same extern "C" definitions in your .cpp sources, then I think your problem is that the -lfcgi should follow the *.cpp in your command line:

g++ -Idependancies\libfcgi\include -Ldependancies -O2 *.cpp -lfcgi
Richard Pennington
You really thing that argument-order matters in this case?
edgar.holleis
Yes, it does. Libraries object files are included only of preceding object files have references to symbols in them.
Richard Pennington
Ha! Believe it or not, but combined with edgar.holleis' answer it actually works! Thanks!
milan1612
I stand corrected. :-)
edgar.holleis
+1  A: 

In your main-project, you tell the compiler to link C-functions, due to the extern "C". It therefore expects unmangled symbol-names. You should therefore compile the fcgi-library with the C compiler, not the C++ compiler.

edgar.holleis
Ok, I tried compiling libfcgi with gcc, but the problem still remains :(
milan1612
Nevermind, it works with Richard's answer below :)
milan1612