链接器错误与PyList_New
Linker error with PyList_New
当我遇到此错误时,我正在尝试在python脚本中使用C函数:
Undefined symbols for architecture x86_64:
"_PyList_Append", referenced from:
_count_arr in mi1-5e9b85.o
"_PyList_New", referenced from:
_count_arr in mi1-5e9b85.o
"_Py_BuildValue", referenced from:
_count_arr in mi1-5e9b85.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
.cpp文件是:
#include <cstring>
#include <iostream>
//#include <Python.h>
#include "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/Python.h"
#include <vector>
#ifdef __cplusplus
extern "C" PyObject* count_arr(char** arr, unsigned n2, char* word)
#else
PyObject* count_arr(char** arr, unsigned n2, char* word)
#endif
{
PyObject * PList = PyList_New(0);
vector <int> intVector;
for(unsigned j = 0; j < n2; j++)
{
if(strstr(arr[j], word) != NULL) {intVector.push_back(j);}
}
vector<int>::const_iterator it;
for(it = intVector.begin(); it != intVector.end() ; it++ )
{
PyList_Append(PList, Py_BuildValue("i", *it));
}
return PList;
}
如果我尝试包含 Python.h,我会收到有关缺少此标头的消息。操作系统是 macos。
我找到了一个答案:如果你使用clang,你需要传递类似的东西
clang mi.o -I/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/include -lpython3.6 -L /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib
但是,如果使用 g++,则需要使用 命令
g++ -fPIC -c mi1.cpp -o mi.o -I/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/include/python3.6m/
g++ -shared -lpython3.6 -L /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib -olibmi.so mi.o
相关文章:
- 如果"new int"返回"int*",那么为什么"new int[n]"不返回"int**"?
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 重载运算符new[]的行为取决于析构函数
- 过载'operator new'如何导致无限循环?
- 创建具有 new in 函数和"this is nullptr"异常的对象
- Codelite C++ new project
- 在类c++中使用new声明数组
- g++用户定义的动态链接库上的全局new和delete运算符
- 如果整个应用程序是虚拟映射的,为什么 new 会进行系统调用?
- 在将 new 与指针一起使用时,创建数组的指定长度
- 体系结构x86_64的未定义符号:std:terminate(),typeinfo,运算符delete[],运算符new
- 为什么我的全局 new() 覆盖被绕过了?
- 声明C++数组(带或不带 "new" 关键字)
- 在C++中,如果我可以直接将整数分配给指针而不使用"new",为什么要使用"new"?
- 是否可以使用 new 指定具有宏常量的动态分配数组的元素?
- constexpr new 如何分配内存?
- 放置 new 和 uninitialized_fill() 的行为
- 使用 new 和 值进行数组初始化,但没有显式数量的元素
- different between int **arr =new int [ n]; and int a[i][j]?
- 为什么 std::make_shared 无法编译带有已删除运算符 new 的类型?