python - 从 anaconda 将 Python 嵌入到 C++ 中,出现错误
问题描述
试图编译这个a.cpp
文件,使用
g++ a.cpp -I ~/anaconda3/include/python3.7m/ -l ~/anaconda3/lib/python3.7/
我得到的错误是
/usr/bin/ld: cannot find -l/home/rverma/anaconda3/lib/python3.7/
collect2: error: ld returned 1 exit status
也试过g++ a.cpp -I ~/anaconda3/include/python3.7m/ -l ~/anaconda3/lib/
这个
/usr/bin/ld: cannot find -l/home/rverma/anaconda3/lib/ collect2: error: ld returned 1 exit status
我的a.cpp
文件如下所示:
#include <Python.h>
#include <stdlib.h>
int main()
{
// Set PYTHONPATH TO working directory
setenv("PYTHONPATH",".",1);
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *presult;
// Initialize the Python Interpreter
Py_Initialize();
// Build the name object
pName = PyUnicode_FromString((char*)"arbName");
// Load the module object
pModule = PyImport_Import(pName);
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, (char*)"someFunction");
if (PyCallable_Check(pFunc))
{
pValue=Py_BuildValue("(z)",(char*)"something");
PyErr_Print();
printf("Let's give this a shot!\n");
presult=PyObject_CallObject(pFunc,pValue);
PyErr_Print();
} else
{
PyErr_Print();
}
printf("Result is %ld\n",PyLong_AsLong(presult));
Py_DECREF(pValue);
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
// Finish the Python Interpreter
Py_Finalize();
return 0;
}
我的argName.py
样子是这样的:
def someFunction(text):
print 'You passed this Python program '+text+' from C! Congratulations!'
return 12345
请帮忙
解决方案
推荐阅读
- python-3.x - ValueError:尝试共享变量 rnn/multi_rnn_cell/cell_0/lstm_cell/kernel
- python - Gunicorn 无法连接到 Alpine 上的 sock 文件
- php - 弹药口径的正则表达式设计
- python - 如何根据子列表中的条件删除列表元素
- performance - React Native 延迟渲染
- postgresql - Postgres pg_rewind 不适用于简单的用例
- html - 出现换行时如何调整行间距?
- java - 如何为数据库批处理作业选择线程数
- spring - Spring data Mongodb using mongoOperations - 使用查询条件获取嵌套/嵌入数组文档
- java - 如何设置 RatingBar 可点击但不可缩放?