How to link a C ++ shared library with gcc

I saw a GCC link with a C ++ shared library, but I cannot reproduce it myself. So first I create a C ++ library with a test function:

g++ -shared -o libtest.so test.c 

Then I have the main test function, which calls the library function and compiles it like this:

 gcc -o prog.out main.c -L. -ltest 

Then i get an error

 undefined reference to 'testfunc' 

which, it seems to me, is called by various links in the library ... C calls the function testfunc and C ++ calls the function [some stuff] __ testfunc [maybe some things again].

I also tried to use

 gcc -o prog.out main.c -l:libtest.so 

but this leads to the same error.

So my question is: how can I link a C ++ library with gcc with a c file?

Update: I know that I can use extern "C" , but that’s not how it is solved. Maybe there are some options for the linker?

Update2: I just thought it was also possible that the first part was just compiled with C ++ and linked to gcc. Also tried this:

 g++ -c testlib.c -o testlib.o gcc -shared -o libtest.so testlib.o gcc -o prog.out -l:libtest.so 

still not working. Is there something wrong with the flags?

+4
source share
3 answers

Yes, the problem has nothing to do with shared libraries (I think ...) and everything related to the name change.

In the header, you should declare the function as follows:

 #ifdef __cplusplus extern "C" { #endif void testfunc(void); #ifdef __cplusplus } #endif 

This will cause testfunc to have the same character and invoke conventions for C and C ++.

On the system I'm using right now, the C character name will be _testfunc , and the C ++ character name (unless you use extern "C" ) will be __Z8testfuncv , which encodes the parameter information so that overloading will work correctly. For example, void testfunc(int x) becomes __Z8testfunci , which does not collide with __Z8testfuncv .

+8
source

When you use g ++, it compiles ALL the source as C ++. This means that all functions use C ++ ABI (this also includes a name change). When you use gcc, it will compile * .c files using C ABI (without a name).

Thus, the same compilation function with two different compilers will generate different functions (in many ways). This is because these are different languages .

To force g ++ to compile a function using the C ABI prefix using extern "C"

 extern "C" void testfunc(char*); 

Alternatively use a block version

 extern "C" { <multiple Functions> } 

Honestly, I will never compile anything with gcc again (unless some kind of strict requirement is required (in this case I usually fix the code so that it works in C ++)). If you compile all files using g ++, just simplify the processes.

+2
source

If you are sure that this is not due to a name change. Then this means that gcc could not find the library trying to provide the full library path if the .so file is not in the standard location. If you are not sure, then recheck any conflict in the name of the variable (function). Use namespaces to group classes and define functions within classes to avoid naming conflicts

0
source

All Articles