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?
source share