1. 程式人生 > >Boost Python學習筆記(一)

Boost Python學習筆記(一)

enc dpkg 編譯工具 module 項目 href cloud etc DC

開發環境搭建

下載源碼

boost_1_66_0.tar.gz

生成編譯工具

# tar axf boost_1_66_0.tar.gz
# cd boost_1_66_0
# yum install gcc gcc-c++ python-devel cmake -y
# ./bootstrap.sh

編譯32位boost庫

# ./b2 install --with-system --with-thread --with-date_time --with-regex --with-serialization --with-python link=shared runtime-link=shared threading=multi debug

設置boost動態庫加載路徑

# tee /etc/ld.so.conf.d/boost-x86_64.conf << EOF
/usr/local/lib
EOF
# ldconfig

項目目錄結構

項目初始目錄結構樣例

[root@fcloud example]# tree .
.
├── build
├── CMakeLists.txt
├── include
│   └── header.h
├── main.cpp
└── src
    └── header.cpp

3 directories, 4 files

CMake樣例(編譯後將生成兩個文件,一個可執行文件core,一個動態庫boost.so)

cmake_minimum_required(VERSION 2.8)
project(boost)

set(CMAKE_CXX_FLAGS  "-Wall -g")

### 此處的動態庫名必須和BOOST_PYTHON_MODULE()中定義的保持一致,即最後生成的庫必須名為boost.so
file(GLOB SRC "src/*.cpp")
add_library(boost SHARED ${SRC})
add_executable(core main.cpp)
set_target_properties(boost PROPERTIES PREFIX "")

#dependencies
INCLUDE(FindPkgConfig)
pkg_check_modules(PYTHON REQUIRED python)
include_directories(include /usr/local/include ${PYTHON_INCLUDE_DIRS})
link_directories(/usr/local/lib ${PYTHON_LIBRARY_DIRS})
target_link_libraries(boost boost_python)
target_link_libraries(core boost ${PYTHON_LIBRARIES})

Boost Python學習筆記(一)