【问题标题】:Embedding Python in C/C++ from anaconda PyThreadState_Get: no current thread从 anaconda PyThreadState_Get 在 C/C++ 中嵌入 Python:没有当前线程
【发布时间】:2017-12-21 21:04:39
【问题描述】:

试图将 python 解释器嵌入到 c++ 应用程序中。认为这将是直截了当的,但我在链接方面遇到了麻烦。唯一不寻常的是我正在尝试使用特定的 Anaconda 包。简单的c++代码是:

# include <Python.h>

int main()
{
    printf("before initialize");
    Py_Initialize();
    printf("after initialize");
    PyRun_SimpleString("print('Hello from Python')");
    Py_Finalize();
    return 0;
}

编译我使用:

gcc pythonTest.cc  -I/path/to/anaconda/include/python3.6m -L/path/to/anaconda/lib/python3.6/config-3.6m-x86_64-linux-gnu -lm -lpthread -ldl -lutil -lpython3.6m -o pytest

它确实可以编译,但是当我运行这个简单的程序时,我得到了:

Fatal Python error: PyThreadState_Get: no current thread

before initializeAborted

我检查了程序看到的路径,它确实包含了与我使用包含和库指定的相同 python 可执行文件的路径。但是,一些搜索似乎表明调用的 python 版本与链接的版本有所不同。我有点不知如何解决这个问题。

【问题讨论】:

  • 我也有同样的问题。你有没有设法解决这个问题?

标签: python c++ anaconda linker-errors embedding


【解决方案1】:

尝试编译使用:

gcc pythonTest.cc -I/$HOME/anaconda3/include/python3.6m -lpython3.6m

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多