提升python链接

Max*_*rai 12 c++ python boost cmake hyperlink

我正在为我的游戏添加boost.python.我为我的类编写包装器以在脚本中使用它们.问题是将该库链接到我的应用程序.我正在使用cmake构建系统.

现在我有一个简单的应用程序,包含1个文件和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
Run Code Online (Sandbox Code Playgroud)

这很有效.它为我构建一个'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}
    ...
)
...
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

好的,所以我为我的项目添加了简单的.cpp文件<boost/python.hpp>.我在编译时遇到错误:

/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory
Run Code Online (Sandbox Code Playgroud)

所以它不需要包含目录.

第二个问题:

如何组织script-cpp文件的两步构建?在makefile中,我展示了TARGET.oTARGET.so,如何处理cmake中的2个命令?

据我了解,最好的方法是创建子项目并在那里做一些事情.

谢谢.

Rod*_*Rod 18

你在CMakeList.txt中缺少你的include目录和python的libs.使用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} # <------
    ...
)
...
Run Code Online (Sandbox Code Playgroud)