Добавьте интерфейс Python в проект C++ через ctypes. OSError: ./libfastbn.so: неопределенный символ ⇐ C++
-
Гость
Добавьте интерфейс Python в проект C++ через ctypes. OSError: ./libfastbn.so: неопределенный символ
I want to add a simple python interface on a large c++ project so that I can call some c++ functions using python. It is a large c++ project and the structure is like
FastBN -src // many source files inside -include // many header files inside -lib CMakeLists.txt For my first try, I just created a new header file in include and a source file in src:
// test.h extern "C" { int add(int n1, int n2); } // test.cpp #include "test.h" int add(int n1, int n2) { return n1 + n2; } After building, I generated libfastbn.so. And then I tried a simple python script:
import ctypes mylib = ctypes.CDLL('./libfastbn.so') mylib.add.argtypes = [ctypes.c_int, ctypes.c_int] mylib.add.restype = ctypes.c_int v1= 32 v2= 121 c = mylib.add(v1, v2) print(c) But I got the following error when running the python script.
Traceback (most recent call last): File "/home/jjt/work/BN/FastBN/build/python_interface.py", line 3, in mylib = ctypes.CDLL('./libfastbn.so') File "/home/jjt/anaconda3/lib/python3.9/ctypes/__init__.py", line 382, in __init__ self._handle = _dlopen(self._name, mode) OSError: ./libfastbn.so: undefined symbol: _ZNK8tinyxml27XMLNode17FirstChildElementEPKc The .so file is generated through CMakeLists.txt, which I also attached below. However, this is for the whole c++ project.
cmake_minimum_required(VERSION 3.10) project(BayesianNetwork) add_definitions("-Wall -g") set(CMAKE_CXX_STANDARD 14) #set(CMAKE_CXX_FLAGS_RELEASE "-O3") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp") option(USE_MPI "option for mpi" OFF) if (USE_MPI) message("Use MPI") add_definitions(-DUSE_MPI) find_package(MPI REQUIRED) include_directories(SYSTEM ${MPI_INCLUDE_PATH}) endif () include_directories(${PROJECT_SOURCE_DIR}/lib/tinyxml2) include_directories(${PROJECT_SOURCE_DIR}/include) include_directories(${PROJECT_SOURCE_DIR}/lib/ARFF/src) include_directories(${PROJECT_SOURCE_DIR}/lib/stats/include) include_directories(${PROJECT_SOURCE_DIR}/lib/gcem/include) add_subdirectory(lib) add_subdirectory(src/fastbn) add_subdirectory(src/test) file(GLOB_RECURSE SRC src/fastbn/*.cpp) file(GLOB HEAD include/*.h) add_library(fastbn SHARED ${SRC} ${HEAD}) add_executable(BayesianNetwork ${SRC} ${HEAD}) target_link_libraries(BayesianNetwork tinyxml2 ARFF stats gcem) target_link_libraries(BayesianNetwork ${MPI_C_LIBRARIES}) I tried ldd ./libfastbn.so, and got:
linux-vdso.so.1 (0x00007fffd4b8d000) libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007ffa96e00000) libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007ffa97117000) libgomp.so.1 => /lib/x86_64-linux-gnu/libgomp.so.1 (0x00007ffa970cd000) libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007ffa970ad000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ffa96a00000) /lib64/ld-linux-x86-64.so.2 (0x00007ffa972b4000)
Источник: https://stackoverflow.com/questions/780 ... so-undefin
I want to add a simple python interface on a large c++ project so that I can call some c++ functions using python. It is a large c++ project and the structure is like
FastBN -src // many source files inside -include // many header files inside -lib CMakeLists.txt For my first try, I just created a new header file in include and a source file in src:
// test.h extern "C" { int add(int n1, int n2); } // test.cpp #include "test.h" int add(int n1, int n2) { return n1 + n2; } After building, I generated libfastbn.so. And then I tried a simple python script:
import ctypes mylib = ctypes.CDLL('./libfastbn.so') mylib.add.argtypes = [ctypes.c_int, ctypes.c_int] mylib.add.restype = ctypes.c_int v1= 32 v2= 121 c = mylib.add(v1, v2) print(c) But I got the following error when running the python script.
Traceback (most recent call last): File "/home/jjt/work/BN/FastBN/build/python_interface.py", line 3, in mylib = ctypes.CDLL('./libfastbn.so') File "/home/jjt/anaconda3/lib/python3.9/ctypes/__init__.py", line 382, in __init__ self._handle = _dlopen(self._name, mode) OSError: ./libfastbn.so: undefined symbol: _ZNK8tinyxml27XMLNode17FirstChildElementEPKc The .so file is generated through CMakeLists.txt, which I also attached below. However, this is for the whole c++ project.
cmake_minimum_required(VERSION 3.10) project(BayesianNetwork) add_definitions("-Wall -g") set(CMAKE_CXX_STANDARD 14) #set(CMAKE_CXX_FLAGS_RELEASE "-O3") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp") option(USE_MPI "option for mpi" OFF) if (USE_MPI) message("Use MPI") add_definitions(-DUSE_MPI) find_package(MPI REQUIRED) include_directories(SYSTEM ${MPI_INCLUDE_PATH}) endif () include_directories(${PROJECT_SOURCE_DIR}/lib/tinyxml2) include_directories(${PROJECT_SOURCE_DIR}/include) include_directories(${PROJECT_SOURCE_DIR}/lib/ARFF/src) include_directories(${PROJECT_SOURCE_DIR}/lib/stats/include) include_directories(${PROJECT_SOURCE_DIR}/lib/gcem/include) add_subdirectory(lib) add_subdirectory(src/fastbn) add_subdirectory(src/test) file(GLOB_RECURSE SRC src/fastbn/*.cpp) file(GLOB HEAD include/*.h) add_library(fastbn SHARED ${SRC} ${HEAD}) add_executable(BayesianNetwork ${SRC} ${HEAD}) target_link_libraries(BayesianNetwork tinyxml2 ARFF stats gcem) target_link_libraries(BayesianNetwork ${MPI_C_LIBRARIES}) I tried ldd ./libfastbn.so, and got:
linux-vdso.so.1 (0x00007fffd4b8d000) libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007ffa96e00000) libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007ffa97117000) libgomp.so.1 => /lib/x86_64-linux-gnu/libgomp.so.1 (0x00007ffa970cd000) libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007ffa970ad000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ffa96a00000) /lib64/ld-linux-x86-64.so.2 (0x00007ffa972b4000)
Источник: https://stackoverflow.com/questions/780 ... so-undefin
Мобильная версия