LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   are g++ gcc libraries incompatible? (https://www.linuxquestions.org/questions/programming-9/are-g-gcc-libraries-incompatible-770419/)

bigearsbilly 11-20-2009 07:21 AM

are g++ gcc libraries incompatible?
 
I have a library of homegrown C functions;
I write stuff in C and C++
if I compile the library with g++, then gcc compiled sources won't link
and vice versa.

is there a way to get round this or do I need to make
two libraries, one for gcc and one for g++?

johnsfine 11-20-2009 07:29 AM

I don't see enough info in your post to be sure of what you are trying to do, but I have a good guess.

If you want functions that can be called from either C or C++, they must have a C interface. In C++ you use
Code:

extern "C"
to declare a C interface for a function (both for declaring an external function with C interface that is called here and defined elsewhere and for defining a function here with C interface to be called elsewhere).

You often need an include file that can declare the C interface and can be included in either C or C++ compiles. Usually that requires something like conditionally #defineing some symbol to be either nothing or "C" so it can be used every place a C interface must be predeclared with extern if including into a C module but with extern "C" if including into C++. Other cases may require two symbols, one for nothing vs. extern "C" { and the other for nothing vs. }

bigearsbilly 11-20-2009 07:41 AM

ahh thanks dude!
this works...

Code:

extern "C" {
#include "bill.h"
}

but...

do you happen to know if there's an #ifdef I could use
depending on whether it's gcc or g++?

then I could put the wrapper in the header file.

bigearsbilly 11-20-2009 08:05 AM

Quote:

Originally Posted by bigearsbilly (Post 3763852)
do you happen to know if there's an #ifdef I could use
depending on whether it's gcc or g++?

amazing what you can find in /usr/local/include

Code:

#ifdef __cplusplus
extern "C" {
   
}
#endif


ta0kira 11-20-2009 11:02 AM

As a side note, you should link g++-compiled objects using g++, not ld, which if you didn't already know you'd find out sooner or later.
Kevin Barry

bigearsbilly 11-20-2009 12:22 PM

yes, I always use GNU make so it generally works ok
with the implicit rules.


All times are GMT -5. The time now is 04:10 AM.