我有一个 cpp
文件,可以使用 shell 使用 g++
进行良好编译:
extern "C"{
#include <quadmath.h>
}
inline char* print( const __float128& val)
{
char* buffer = new char[128];
quadmath_snprintf(buffer,128,"%+.34Qe", val);
return buffer;
}
int main(){
__float128 a = 1.0;
print(a);
return 0;
}
但是,当我尝试通过 python
scrit 编译它时,它失败并出现以下错误:
"undefined reference to quadmath_snprintf"
这里是 python
脚本的代码:
import commands
import string
import os
(status, output) = commands.getstatusoutput("(g++ test/*.c -O3 -lquadmath -m64)")
知道如何解决这个问题吗?谢谢。
最佳答案
当你打开一个 shell 时,所有的东西都会为你默默地初始化,对你的问题来说最重要的是,环境变量已经设置好了。您最有可能错过的是 LIBRARY_PATH
的定义,它是链接器用来查找与您使用 -lNAME
标志指示链接的库相匹配的库的变量.
链接器需要的是一个目录列表,它将在其中搜索与 libNAME.{a,so}
匹配的文件。您也可以使用 -L
标志直接传递这些目录,但通常,您应该尝试使用 CMake、Make 或任何其他构建工具等程序。
这将使您能够访问诸如 find_package
和 target_link_libraries
(CMake) 之类的命令,以分别查找库并将其添加到您的构建目标,而不必维护您的 python编译你的东西。
关于python - 来自 python 的 Cpp 编译失败但不在 shell 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39868209/