gpt4 book ai didi

c++ - boost python链接

转载 作者:IT老高 更新时间:2023-10-28 23:14:54 25 4
gpt4 key购买 nike

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

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

谢谢。

最佳答案

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

关于c++ - boost python链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5238160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com