【问题标题】:Boost python linking提升python链接
【发布时间】:2011-07-11 10:10:24
【问题描述】:

我正在为我的游戏添加 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

这行得通。它为我构建了一个“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

所以它并不需要所有需要的包含目录。

还有第二个问题:

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

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

谢谢。

【问题讨论】:

    标签: c++ python boost hyperlink cmake


    【解决方案1】:

    您的 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} # <------
        ...
    )
    ...
    

    【讨论】:

    • 如果我的项目应该是跨平台的怎么办?该行在 Windows 上不起作用。
    • 对不起!我尝试投票,然后按下。我很抱歉。我认为您也应该发布原始的-非跨平台的-答案。我做了一些 python/cython/c 的东西,我只关心效率(在这个项目中)。再次为我愚蠢的鼠标感到抱歉(我知道,只有用户是愚蠢的,而不是工具......)
    • @cox 为你解决了这个问题 :)
    • @cox 访问edit history 看看有什么变化。我只是将路径更改为与平台无关,而不是基于 unix。
    猜你喜欢
    • 2011-12-24
    • 2013-01-21
    • 2011-01-20
    • 2011-12-08
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多