I am trying to build a shared object library from a cpp
file that is a simple set of functions. I want to use ctypes
to interface with python.
Say I have the cpp
file:
#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";
}
}
}
and header file
#ifndef ADD_H
#define ADD_H
#include <vector>
void print_array(const std::vector<std::vector<float>> &A);
#endif
I tried to build
g++ -fpic -c print.cpp -o print.o
g++ -shared -o print.so print.o
Then in python
from cytpes import cdll
print_lib = cdll.LoadLibrary("print.so")
the line
print_lib.print_array()
yields
AttributeError: ./print.so: undefined symbol: print_array
nm -D print.so
gives the output
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
What am I fundamentally doing wrong in the compilation step?