我尝试在共享库上工作。
详细的,我点击这个链接: http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html .
这个命令一切正常:
$ gcc -L/home/user/foo -Wl,-rpath=/home/user/foo -Wall -o test main.c -lfoo
$ ./test
但是当我从 gcc 更改为/usr/bin/c++ 时,并将命令更改为:
$ /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
它在我的 libfoo.so 中找不到方法 foo。
我也尝试用另一种方式使用 -rdynamic,它也适用于 gcc:
gcc -rdynamic ./libfoo.so -Wl,-rpath=/home/user/foo -Wall -o test main.c -lfoo
但是/usr/bin/c++也有同样的错误。
请告诉我如何让它与/usr/bin/c++ 一起工作。
非常感谢。
最佳答案
正如其他人所说,这是因为 C 和 C++ 编译库之间的符号命名不同,由于 C++ 具有重载函数,因此每个符号的命名应包括符号名称中的参数类型或基于以下内容标识实际函数或类方法的内容它的论据。 与 C++ 不同,C 没有这个问题,因为没有函数重载和它的标准 ABI。如果你想混合 C/C++ 文件,你必须用 g++ 编译所有的 C 和 C++ 代码,并在 C 代码的定义中添加 extern C 关键字
#ifdef __cplusplus
extern "C" {
#endif
// your C definitions (only function definitions) goes here
#ifdef __cplusplus
}
#endif
关于c++ -/usr/bin/C++ 无法使用共享库编译为 gcc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41136859/