我有一个用于gcc的C库。该库的扩展名为.lib,但始终作为静态库链接。如果我编写一个使用该库作为C代码的程序,一切都可以。但是,如果我将文件重命名为.cpp(做一些在C/C++中都起作用的简单操作),我会得到未定义的引用错误。这些是我为测试目的编写的简单小程序,没有花哨的东西。我使用以下命令进行编译:
gcc -g -Wall -I <path to custom headers> -o program main.c customlibrary.lib -lm -lpthread
上面的方法非常好用,不过:
g++ -g -Wall -I <path to custom headers> -o program main.cpp customlibrary.lib -lm -lpthread
或者gcc -g -Wall -I <path to custom headers> -o program main.cpp customlibrary.lib -lm -lpthread -lstdc++
使用自定义库customlibrary.lib时,会导致对任何函数的未定义引用。我尝试创建了一个名为customlibrary.a的符号链接,但是没有成功。
为什么g++无法识别我的库呢?不幸的是,我无法访问库的源代码,但将c-lib链接到c++应该不是问题,对吗?
__cplusplus
有两个下划线,如相关标准所定义。 - RBerteig