从单个.cpp构建共享对象
Building a shared object from a single .cpp
我正在尝试从一组简单函数的cpp
文件构建共享对象库。 我想使用ctypes
与python交互。
假设我有cpp
文件:
#include "print.h"
#include <vector>
#include <iostream>
#include <dlfcn.h>
void print_array(const std::vector<std::vector<float>> &A){
for(size_t i = 0; i < A.size(); i++) {
for(size_t j = 0; j < A[0].size(); j++) {
std::cout << A[i][j] << "n";
}
}
}
和头文件
#ifndef ADD_H
#define ADD_H
#include <vector>
void print_array(const std::vector<std::vector<float>> &A);
#endif
我试图建造
g++ -fpic -c print.cpp -o print.o
g++ -shared -o print.so print.o
然后在蟒蛇中
from cytpes import cdll
print_lib = cdll.LoadLibrary("print.so")
行
print_lib.print_array()
收益 率
AttributeError: ./print.so: undefined symbol: print_array
nm -D print.so
给出输出
0000000000201060 B __bss_start
U __cxa_atexit
w __cxa_finalize
0000000000201060 D _edata
0000000000201068 B _end
0000000000000c14 T _fini
w __gmon_start__
0000000000000898 T _init
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
w _Jv_RegisterClasses
0000000000000a50 T _Z11print_arrayRKSt6vectorIS_IfSaIfEESaIS1_EE
0000000000000bcc W _ZNKSt6vectorIfSaIfEE4sizeEv
0000000000000bf2 W _ZNKSt6vectorIfSaIfEEixEm
0000000000000b6a W _ZNKSt6vectorIS_IfSaIfEESaIS1_EE4sizeEv
0000000000000ba2 W _ZNKSt6vectorIS_IfSaIfEESaIS1_EEixEm
U _ZNSolsEf
U _ZNSt8ios_base4InitC1Ev
U _ZNSt8ios_base4InitD1Ev
U _ZSt4cout
U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
我在编译步骤中根本上做错了什么?
您正在调用C++函数,而ctypes
期望 C 函数链接:
- 正如您所注意到的,C 函数具有不同的(即无(名称重整
- C 函数不能有C++参数(std 对象,如 std::vector、引用等(
要解决您的问题,请在标头中声明一个干净的 C 样式 API,如果您打算在 C++ 中实现函数并使用 g++ 作为编译器/链接器,则必须在头文件中的函数声明之前添加一个extern "C"
声明,或者作为声明周围的块:
extern "C" void print_array(...);
这将防止C++名称重整。
在接口函数中使用向量和其他C++类型会导致 ABI 问题,即"它不起作用",即使所有内容的链接和编译看起来都很干净。
将 C 函数与 ctypes支持的数据类型一起使用(请参阅有关 ctypes 的 Python 文档(作为接口,随意在 C++ 中实现,但然后将 C 函数声明包装在extern "C"
块中以防止名称重整。
请参考 ctypes 的文档,了解如何正确使用结构、联合、引用等作为函数参数,因为有许多陷阱和问题需要考虑。
相关文章:
- 处理影响跨不同线程共享对象的定时回调的最佳方法是什么?
- 如何创建一个共享对象与另一个.所以在Cmake
- 内联函数的函数本地静态对象是否在共享对象文件之间共享?
- 在 Android Studio 中使用 C++ 共享对象时出现问题
- 共享对象、符号、C/C++ 库链接和加载
- ./main:加载共享库时出错:libopencv_highgui.so.4.0:无法打开共享对象文件:没有这样的文件或
- 在 win32 上生成 R 包:无法加载共享对象 (.dll)
- 共享对象中的符号
- sf::Windows上的音乐:api-ms-win-crt-locale-l1-1-0.dll:无法打开共享对象文件
- C++ 为什么要级联共享对象链接
- 加载共享库时出错:libbsoncxx.so._noabi:无法打开共享对象文件:没有此类文件或目录
- 从单个.cpp构建共享对象
- 与 32 位共享对象的链接问题
- 卸载共享对象(.so 文件)在 C++ 中用 dlopen() 打开
- 运行程序时找不到共享对象库,但在编译过程中链接了它
- 错误:无法打开共享对象文件:没有此类文件或目录
- Qt 创建器:将库移动到 subdir 后无法打开共享对象文件
- 共享对象中的符号名称与文件中.cpp函数不同
- C++:在共享对象中调用抽象基类构造函数/未定义的符号
- Cython:共享对象中未定义的符号