简体   繁体   English

提升python链接

[英]Boost python linking

I'm adding boost.python for my Game. 我正在为我的游戏添加boost.python。 I write wrappers for my classes to use them in scripts. 我为我的类编写包装器以在脚本中使用它们。 The problem is linking that library to my app. 问题是将该库链接到我的应用程序。 I'm using cmake build system. 我正在使用cmake构建系统。

Now I have a simple app with 1 file and makefile for it: 现在我有一个简单的应用程序,包含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

And this works. 这很有效。 It builds a 'so' file for me which I can import from python. 它为我构建一个'so'文件,我可以从python导入。

Now the question: how to get this for cmake? 现在的问题是:如何为cmake获取此信息?

I wrote in main CMakeList.txt : 我在主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 calls show: 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

Ok, so I've added simple .cpp-file for my project with include of <boost/python.hpp> . 好的,所以我为我的项目添加了简单的.cpp文件,其中包含<boost/python.hpp> I get an error at compiling: 我在编译时遇到错误:

/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory

So it doesn't take all need include directories. 所以它不需要包含目录。

And second question: 第二个问题:

How to organize 2-step building of script-cpp files? 如何组织script-cpp文件的两步构建? In makefile I showed there are TARGET.o and TARGET.so , how to process that 2 commands in cmake? 在makefile中,我展示了TARGET.oTARGET.so ,如何处理cmake中的2个命令?

As I understand, the best way is to create subproject and do something there. 据我了解,最好的方法是创建子项目并在那里做一些事情。

Thanks. 谢谢。

You are missing your include directory and libs for python in your CMakeList.txt. 你在CMakeList.txt中缺少你的include目录和python的libs。 Use the PythonFindLibs macro or the same find_package strategy you used for Boost 使用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} # <------
    ...
)
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM