前提・実現したいこと
現在pykepというpython用の宇宙軌道設計用ツールを導入しようとしています。
このツールはC++で書かれたものをboost pythonでpython側に公開しているためビルドが必要です。
pipを使った導入もできるのですが、C++側を弄って使う予定もあるのでソースコードからビルドする方法を確立したいのです。
作者のページのインストール手順に従っているのですが、boost pythonがうまく使えていないようでpythonでimportしようとするとエラーがでてしまいます。
makeとmake installは通ります。
ソースコードには一番上層にあるCMakeLists.txtを貼っていますがこれでいいのかもちょっとわかっていません。
発生している問題・エラーメッセージ
import pykep
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/pykep/init.py", line 47, in <module>
from pykep.core import *
File "/usr/local/lib/python3.5/dist-packages/pykep/core/init.py", line 3, in <module>
from ._core import _get_AU, _get_JR, _get_DAY2SEC, _get_DAY2YEAR, _get_DEG2RAD, _get_EARTH_VELOCITY, _get_EARTH_J2, _get_EARTH_RADIUS, _get_MU_EARTH, _get_G0, _get_MU_SUN, _get_RAD2DEG, _get_SEC2DAY
ImportError: /usr/local/lib/python3.5/dist-packages/pykep/core/_core.so: undefined symbol: _ZN5boost7archive18text_iarchive_implINS0_13text_iarchiveEE13load_overrideERNS0_15class_name_typeEi
該当のソースコード
CMakeLists
1# CMake version check. 2cmake_minimum_required(VERSION 3.2) 3 4# Main pykep project version. 5set(PYKEP_PROJECT_VERSION 2.3) 6project(pykep VERSION ${PYKEP_PROJECT_VERSION}) 7enable_testing() 8 9# Module path setup. 10set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake_modules" "${CMAKE_SOURCE_DIR}/cmake_modules/yacma") 11 12message(STATUS "System name: ${CMAKE_SYSTEM_NAME}") 13 14# Set default build type to "Release". 15if(NOT CMAKE_BUILD_TYPE) 16 set(CMAKE_BUILD_TYPE Release CACHE STRING 17 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." 18 FORCE) 19endif() 20 21# Writing some configuration files with version update ... 22configure_file("${CMAKE_CURRENT_SOURCE_DIR}/doc/sphinx/conf.py.in" "${CMAKE_CURRENT_SOURCE_DIR}/doc/sphinx/conf.py" @ONLY) 23configure_file("${CMAKE_CURRENT_SOURCE_DIR}/pykep/__init__.py.in" "${CMAKE_CURRENT_SOURCE_DIR}/pykep/__init__.py" @ONLY) 24 25# Initial setup of compiler flags. 26include(YACMACompilerLinkerSettings) 27 28# Provides build options to CMake 29# Build Option: when active the file main.cpp is built and linked to the PYKEP static library 30OPTION(BUILD_MAIN "Build 'main.cpp'." ON) 31 32# Build Option: when active builds Python bindings called pykep 33OPTION(BUILD_PYKEP "Build pykep." OFF) 34 35# Build Option: when active the examples in the folder example are compiled into executables 36OPTION(BUILD_TESTS "Build tests'." ON) 37 38# Build Option: when active the JPL SPICE toolbox is used to extend pykep. You can choose 39# to build the library or use a pre-build library 40OPTION(BUILD_SPICE "Interface pykep to JPL SPICE toolbox'." OFF) 41 42# Build Option: when active headers will be installed in the CMAKE_INSTALL_PREFIX/include directory 43OPTION(INSTALL_HEADERS "Installs the header files" OFF) 44 45# Assemble the flags. 46set(PYKEP_CXX_FLAGS_DEBUG ${YACMA_CXX_FLAGS} ${YACMA_CXX_FLAGS_DEBUG} ${YACMA_THREADING_CXX_FLAGS}) 47set(PYKEP_CXX_FLAGS_RELEASE ${YACMA_CXX_FLAGS} ${YACMA_THREADING_CXX_FLAGS}) 48if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND YACMA_COMPILER_IS_CLANGXX) 49 message(STATUS "Clang compiler on OSX detected, setting the standard library to 'libc++'.") 50 list(APPEND PYKEP_CXX_FLAGS_DEBUG "-stdlib=libc++") 51 list(APPEND PYKEP_CXX_FLAGS_RELEASE "-stdlib=libc++") 52endif() 53if(YACMA_COMPILER_IS_MSVC) 54 # Disable the idiotic minmax macros on MSVC, some annoying warnings, 55 # and enable the bigobj option. 56 list(APPEND PYKEP_CXX_FLAGS_DEBUG "-DNOMINMAX" "/wd4459" "/wd4127" "/wd4702" "/bigobj") 57 list(APPEND PYKEP_CXX_FLAGS_RELEASE "-DNOMINMAX" "/wd4459" "/wd4127" "/wd4702" "/bigobj") 58endif() 59if(YACMA_COMPILER_IS_INTELXX) 60 # NOTE: on MSVC we use the push/pop pragmas, but they do not seem to work on Intel (the pragmas 61 # in icc influence the behaviour at instantiation point, not at definition point). 62 # These warnings are useful in principle, but they are generated a lot from cereal and we have no 63 # way of disabling them selectively. Just rely on the other compilers to provde good diagnostic. 64 list(APPEND PYKEP_CXX_FLAGS_DEBUG "-diag-disable" "2259,1682,68") 65 list(APPEND PYKEP_CXX_FLAGS_RELEASE "-diag-disable" "2259,1682,68") 66endif() 67if(MINGW) 68 # Flag needed to deal with big binaries in MinGW. 69 message(STATUS "Enabling the '-Wa,-mbig-obj' flag in MinGW builds.") 70 list(APPEND PYKEP_CXX_FLAGS_DEBUG "-Wa,-mbig-obj") 71 list(APPEND PYKEP_CXX_FLAGS_RELEASE "-Wa,-mbig-obj") 72endif() 73 74if(BUILD_PYKEP) 75 # pygmo dependencies. 76 include(YACMAPythonSetup) 77 78 # Python version check. 79 if(${PYTHON_VERSION_MAJOR} LESS 2 OR (${PYTHON_VERSION_MAJOR} EQUAL 2 AND ${PYTHON_VERSION_MINOR} LESS 7)) 80 message(FATAL_ERROR "Minimum supported Python version is 2.7.") 81 endif() 82 83 # Internal variable that will be used to tell PYKEPFindBoost to locate Boost.Python. 84 set(_PYKEP_FIND_BOOST_PYTHON TRUE) 85endif() 86 87# Boost setup. Will allow using Boost::library_name for the needed components 88include(PykepFindBoost) 89 90# We build cspice static library and link it in pykep libraries 91if(BUILD_SPICE) 92add_subdirectory("${CMAKE_SOURCE_DIR}/src/third_party/cspice") 93 add_definitions(-DPYKEP_USING_SPICE) 94endif(BUILD_SPICE) 95 96# Here we define the file list and make the static and dynamic library 97add_subdirectory("${CMAKE_SOURCE_DIR}/src") 98 99# Build main and link it to static library. 100if(BUILD_MAIN) 101 add_executable(main main.cpp) 102 target_link_libraries(main keplerian_toolbox_static Boost::boost Boost::serialization Boost::date_time) 103 set_property(TARGET main PROPERTY CXX_STANDARD 11) 104 set_property(TARGET main PROPERTY CXX_STANDARD_REQUIRED YES) 105 set_property(TARGET main PROPERTY CXX_EXTENSIONS NO) 106endif(BUILD_MAIN) 107 108# Build Tests and link them to static library. 109if(BUILD_TESTS) 110 add_subdirectory("${CMAKE_SOURCE_DIR}/tests") 111 file(COPY "${CMAKE_SOURCE_DIR}/tests/data/sgp4_test.txt" DESTINATION "${CMAKE_BINARY_DIR}/tests") 112 file(COPY "${CMAKE_SOURCE_DIR}/tests/data/C_G_1000012_2012_2017.bsp" DESTINATION "${CMAKE_BINARY_DIR}/tests") 113 file(COPY "${CMAKE_SOURCE_DIR}/tests/data/pck00010.tpc" DESTINATION "${CMAKE_BINARY_DIR}/tests") 114 FILE(COPY "${CMAKE_SOURCE_DIR}/tests/data/gm_de431.tpc" DESTINATION "${CMAKE_BINARY_DIR}/tests") 115endif(BUILD_TESTS) 116 117# Build pykep and link it to dynamic library. 118if(BUILD_PYKEP) 119 add_subdirectory("${CMAKE_SOURCE_DIR}/pykep") 120 if(MINGW OR ${CMAKE_SYSTEM_NAME} MATCHES "Linux") 121 message(STATUS "Creating the files for the generation of a binary wheel.") 122 configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/wheel_setup.py" "${CMAKE_CURRENT_BINARY_DIR}/wheel/setup.py" @ONLY) 123 if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") 124 # NOTE: this is necessary on linux but harmful on mingw. 125 configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/wheel_setup.cfg" "${CMAKE_CURRENT_BINARY_DIR}/wheel/setup.cfg" @ONLY) 126 endif() 127 if(MINGW) 128 configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/mingw_wheel_libs_python${PYTHON_VERSION_MAJOR}.txt" "${CMAKE_CURRENT_BINARY_DIR}/wheel/mingw_wheel_libs_python${PYTHON_VERSION_MAJOR}.txt" @ONLY) 129 endif() 130 endif() 131endif() 132
試したこと
以下のページを参考にしました。
Install pykep
調べてみたところ以下の似たような事例を見つけました。
boost.pythonでundefined symbolエラーが発生する
発生しているエラーを見るとまさにこれと同様なので-lboost_pythonの位置を変えたいのですが、Cmakeを使っているのでどこをいじればこれができるのかわかりません。
CmakeでできたMakefileを書き直すのでもいいのですが、いかんせん該当箇所も多くまたこれでうまく行く保証もないため今後試行錯誤を繰り返すことを考えるとCMake側でこれを解決したいです。
###試したことその2
boost.pythonのチュートリアルを説明しているページを元に以下を試しました。
Boost.Python の機能をざっと紹介してみる
C++
1#include <boost/python.hpp> 2 3int add(int lhs, int rhs) 4{ 5 return lhs + rhs; 6} 7 8BOOST_PYTHON_MODULE(basic) 9{ 10 using namespace boost::python; 11 def("add", &add); 12} 13
これを作って
cmd
1takehiro@takehiro-desktop:~/ドキュメント$ g++ -DPIC -shared -fPIC -o basic.so basic.cpp -I/usr/include/python3.5m -lboost_python3 2>>> import basic 3>>> basic.add(1,2) 43 5>>> quit() 6takehiro@takehiro-desktop:~/ドキュメント$ g++ -I/usr/include/python3.5m -lboost_python3 -DPIC -shared -fPIC -o basic.so basic.cpp 7takehiro@takehiro-desktop:~/ドキュメント$ python3 8Python 3.5.2 (default, Nov 23 2017, 16:37:01) 9[GCC 5.4.0 20160609] on linux 10Type "help", "copyright", "credits" or "license" for more information. 11>>> import basic 12Traceback (most recent call last): 13 File "<stdin>", line 1, in <module> 14ImportError: /home/takehiro/ドキュメント/basic.so: undefined symbol: _ZNK5boost6python7objects21py_function_impl_base9max_arityEv 15
これを試してみました。やはりリンカフラッグの位置は大事なようです。
補足情報(FW/ツールのバージョンなど)
Ubuntu16.04LTS
CMake3.12.3
python3.5
teratailで質問をするのが初めてです。至らないことも多々あると思いますがよろしくお願いいたします。