0

I try to work on shared library.

In detail, I following this link: http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html.

Everything ok with this command:

$ gcc -L/home/user/foo -Wl,-rpath=/home/user/foo -Wall -o test main.c -lfoo
$ ./test

But when i change from gcc to /usr/bin/c++, ANd change commands to:

$ /usr/bin/c++ -L/home/user/foo -Wl,-rpath=/home/user/foo -Wall -o test2 main.c -lfoo
/tmp/ccpEHbNV.o: In function `main':
main.c:(.text+0x16): undefined reference to `foo()'
collect2: error: ld returned 1 exit status

It can't find the method foo in my libfoo.so.

I also try by another way with -rdynamic, It also work well with gcc:

gcc -rdynamic ./libfoo.so -Wl,-rpath=/home/user/foo -Wall -o test main.c -lfoo

But It also have the same error with /usr/bin/c++.

Please show me how to make it work with /usr/bin/c++.

Thank a lot.

8
  • 1
    Do you have any other c++ compile than g++ installed? Commented Dec 14, 2016 at 7:25
  • 1
    Don't mix and match compilers, the name mangling won't match. Compile both the lib and main.c with either gcc, or g++, not both. You could also wrap the declarations with extern "C" and appropriate compiler guards if you really do need to use the lib in both C and C++. Commented Dec 14, 2016 at 7:25
  • /usr/bin/c++ is a standard name for C++ compiler. It can be any compiler on your system. For example, on my system it is the same as g++. You can check it with ls -l /usr/bin/{g,c}++. On some systems c++ is a symlink to g++. On my system it is just a copy of g++ (checked with md5sum /usr/bin/{g,c}++). So it is impossible to answer unless you tell us which compiler is disguised under /usr/bin/c++. By the way, the commands work with g++, so it is unlikely that your c++ is g++. Commented Dec 14, 2016 at 7:50
  • 2
    C and C++ are different languages. If you compile the library with a C compiler and your code with a C++ compiler, you need to ensure that the library's functions are declared with C linkage in C++. Commented Dec 14, 2016 at 8:02
  • I don't understand. /usr/bin/c++ is compiler in C++ right. If I change anything from C to Cpp. I also meet with the same problem. Commented Dec 14, 2016 at 8:17

3 Answers 3

2

As others said, this is because different symbol naming between C and C++ compiled libraries, As C++ have overloaded functions the naming for each symbol should include type of arguments in symbol name or something which identifies the actual function or class method based on its arguments. Unlike C++, C doesn't have this issue, because of no function overloading and its standard ABI. if you want mix C/C++ files you have to compile all the C & C++ codes with g++ and also add extern C keyword to definition of C codes

#ifdef __cplusplus
extern "C" {
#endif

// your C definitions (only function definitions) goes here

#ifdef __cplusplus
}
#endif
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is coming probably because of name mangling of C++. /usr/bin/c++ ends up as /usr/bin/g++. gcc is a C compiler while g++ is a C++ compiler. You need to compile your library libfoo.so with g++ and then try to link. It should work.

2 Comments

"gcc is a C compiler while g++ is a C++ compiler" - GCC is C and C++ compiler. Read NAME section from the man page.
I know gcc means GNU Compiler Collection. Historically, gcc is the command for C language.
-1

What is the version of gcc and c++ you are using ?

Ideally within the same compiler tool-chain family there should be no error.

In fact, i wrote the code, compiled and executed the same example without any issues.

Please note the version of gcc/c++ i am using.

gcc (GCC) 6.2.1 20160830
g++ (GCC) 6.2.1 20160830

i wrote a shell script file to execute the commands

strace c++ -c -Wall -Werror -fpic foo.c
strace c++ -shared -o libfoo.so foo.o
strace c++ -L. -Wall -o test main.c -lfoo

and collected the strace output of the compilation and linking. Please see the link below http://pastebin.com/nJpQuCfS

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.