简体   繁体   English

来自python的Cpp编译失败但在shell中没有

[英]Cpp compilation from python fail but not in the shell

I have a cpp file that compiles fine with g++ by using the shell: 我有一个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;
}

However, when I try to compile it via a python scrit, it fails with the following error: 但是,当我尝试通过python scrit编译它时,它失败并出现以下错误:

"undefined reference to quadmath_snprintf" “对quadmath_snprintf的未定义引用”

Here the code of the python script: 这里是python脚本的代码:

import commands
import string
import os
(status, output) = commands.getstatusoutput("(g++ test/*.c -O3 -lquadmath -m64)")

Any idea how to solve this? 不知道怎么解决这个问题? Thanks. 谢谢。

When you open a shell a whole of stuff is silently initialized for you, and most important for your issue, environment variables are set. 当你打开一个shell时,会为你静默初始化一些东西,对你的问题最重要的是,设置了环境变量。 What you most likely miss is the definition of LIBRARY_PATH , which is the variable used by the linker to look for libraries matching the ones you instruct it to link using the -lNAME flags. 您最可能错过的是LIBRARY_PATH的定义, LIBRARY_PATH是链接器用于查找与您指示使用-lNAME标记链接的库匹配的库的变量。

What the linker needs is a list of directories where it will search for files matching libNAME.{a,so} . 链接器需要的是一个目录列表,它将搜索与libNAME.{a,so}匹配的文件libNAME.{a,so} You can also pass these directories directly using the -L flag, but in general, you should probably try to use a program like CMake, Make or any other build tool. 您也可以使用-L标志直接传递这些目录,但一般情况下,您应该尝试使用像CMake,Make或任何其他构建工具这样的程序。

This will give you access to commands like find_package and target_link_libraries (CMake), to find, respectively add libraries to your build targets, instead of having to maintain your python to compile your stuff. 这会给你像命令访问find_packagetarget_link_libraries (CMake的),找到,分别将库添加到您的构建目标,而不必保持你的Python编译你的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM