Boost Python链接
我正在为我的游戏添加boost.python库。我为我的类写了一些包装器,以便在脚本中使用它们。现在的问题是如何将这个库链接到我的应用程序上。我使用的是cmake
构建系统。
目前我有一个简单的应用程序,只有一个文件和一个makefile:
PYTHON = /usr/include/python2.7
BOOST_INC = /usr/include
BOOST_LIB = /usr/lib
TARGET = main
$(TARGET).so: $(TARGET).o
g++ -shared -Wl,--export-dynamic \
$(TARGET).o -L$(BOOST_LIB) -lboost_python \
-L/usr/lib/python2.7/config -lpython2.7 \
-o $(TARGET).so
$(TARGET).o: $(TARGET).cpp
g++ -I$(PYTHON) -I$(BOOST_INC) -c -fPIC $(TARGET).cpp
这个可以正常工作。它为我生成了一个' so' 文件,我可以从python中导入它。
现在的问题是:如何在cmake中做到这一点?
我在主CMakeList.txt
中写了:
...
find_package(Boost COMPONENTS filesystem system date_time python REQUIRED)
message("Include dirs of boost: " ${Boost_INCLUDE_DIRS} )
message("Libs of boost: " ${Boost_LIBRARIES} )
include_directories(
${Boost_INCLUDE_DIRS}
...
)
target_link_libraries(Themisto
${Boost_LIBRARIES}
...
)
...
message
调用显示:
Include dirs of boost: /usr/include
Libs of boost: /usr/lib/libboost_filesystem-mt.a/usr/lib/libboost_system-mt.a/usr/lib/libboost_date_time-mt.a/usr/lib/libboost_python-mt.a
好的,我为我的项目添加了一个简单的.cpp文件,并包含了<boost/python.hpp>
。但是在编译时我遇到了一个错误:
/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory
所以它没有包含所有需要的目录。
还有第二个问题:
如何组织脚本-cpp文件的两步构建?在makefile中我展示了有TARGET.o和TARGET.so,那么在cmake中如何处理这两个命令呢?
我理解的最好方法是创建一个子项目,然后在里面做一些事情。
谢谢。
1 个回答
18
你在CMakeList.txt文件中缺少了Python的包含目录和库文件。可以使用PythonFindLibs这个宏,或者用你之前为Boost使用的find_package方法。
find_package(Boost COMPONENTS filesystem system date_time python REQUIRED)
message("Include dirs of boost: " ${Boost_INCLUDE_DIRS} )
message("Libs of boost: " ${Boost_LIBRARIES} )
find_package(PythonLibs REQUIRED)
message("Include dirs of Python: " ${PYTHON_INCLUDE_DIRS} )
message("Libs of Python: " ${PYTHON_LIBRARIES} )
include_directories(
${Boost_INCLUDE_DIRS}
${PYTHON_INCLUDE_DIRS} # <-------
...
)
target_link_libraries(Themisto
${Boost_LIBRARIES}
${PYTHON_LIBRARIES} # <------
...
)
...