python - 来自 python 的 Cpp 编译失败但不在 shell 中

标签 python c++ g++

我有一个 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_packagetarget_link_libraries (CMake) 之类的命令,以分别查找库并将其添加到您的构建目标,而不必维护您的 python编译你的东西。

关于python - 来自 python 的 Cpp 编译失败但不在 shell 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39868209/

相关文章:

python - 自定义用户模型 django-allauth 出现断言错误

c++ - 如何更改 QLineEdit 中部分文本的颜色?

c++ - SFML:对 _imp_ 的 undefined reference

python - Tkinter 无法正确显示图像

python - 从搜索模式中排除空格

python - 使用自定义值在 Selenium 中定位元素

C++ 嵌入式 python PyArg_ParseTuple 因字符串参数而失败

c++ - openGL 中的对象有限制吗?

c++ - 全局 const 变量的 G++ 名称修饰

php - 使用 PHP 编译 C++