2011-03-08 79 views
6

我爲我的遊戲添加boost.python。我爲我的類編寫包裝來在腳本中使用它們。問題是將該庫鏈接到我的應用程序。我正在使用cmake構建系統。Boost python linking

現在我有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 

因此它並不需要包含所有的目錄。

而且第二問題:

如何組織的腳本cpp文件2步建設呢?在makefile中我顯示有TARGET.oTARGET.so,如何在cmake中處理這2個命令?

據我所知,最好的方法是創建子項目,並在那裏做一些事情。

謝謝。

回答

12

您在CMakeList.txt中缺少您的include目錄和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} # <------ 
    ... 
) 
... 
+0

如果我的項目應該跨平臺該怎麼辦?該行不會在Windows上運行。 – Ockonal 2011-03-09 05:23:35

+1

的確,我已經更新了我的答案,是跨平臺的。 – Rod 2011-03-09 14:44:07

+0

好的,我明白了。謝謝。 – Ockonal 2011-03-09 16:50:04