Milan
Milan

Reputation: 3360

g++ linking issue

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 :)

Upvotes: 1

Views: 1439

Answers (2)

edgar.holleis
edgar.holleis

Reputation: 4991

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.

Upvotes: 1

Richard Pennington
Richard Pennington

Reputation: 19965

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

Upvotes: 4

Related Questions