简体   繁体   中英

CMAKE 3.9.3 Cannot Find Boost1.65.1 Boost_Python

it's kind of very daunting now. I've tried all I could possibly figure out, to no avail.

I am using ElementaryOS Loki, based on Ubuntu 16.04 LTS. I have boost 1.65.1 installed under /usr/local I am using cmake 3.9.3 which is supporting building boost 1.65.0 and forward.

I have tried every possible way to mess with my CMakeLists.txt , which as of now, looks like this

cmake_minimum_required( VERSION 2.8 FATAL_ERROR )
project( boostpythondemo )

set( Boost_DEBUG ON )

MESSAGE("Boost Debugging is on.")

set( Boost_NO_SYSTEM_PATHS TRUE )

if( Boost_NO_SYSTEM_PATHS)
    set( BOOST_ROOT "/usr/local/boost_1_65_1" )
    set( BOOST_INCLUDEDIR "/usr/local/boost_1_65_1/boost" )
    set( BOOST_LIBRARYDIR "/usr/local/boost_1_65_1/stage/lib" )
endif( Boost_NO_SYSTEM_PATHS )


find_package( PythonLibs 3.6 REQUIRED )
include_directories( ${PYTHON_INCLUDE_DIRS} )

find_package( Boost COMPONENTS python REQUIRED )

if( Boost_FOUND )
       MESSAGE("******************************BOOST FOUND*******************")
endif( Boost_FOUND )

include_directories( ${Boost_INCLUDE_DIRS} )
link_directories( ${Boost_LIBRARIES} )

add_library( heyall SHARED heyall.cpp )

add_library( heyall_ext SHARED heyall_ext.cpp )
target_link_libraries( heyall_ext ${BOOST_LIBRARIES} heyall )
set_target_properties( heyall_ext PROPERTIES PREFIX "" )

from the command line output I can see I am setting the boost variables to the correct locations.

However, cmake just can't find boost_python. I really can't figure out what's going on now. the line says "BOOST FOUND" never got printed.

here is also the full cmake output log .

I built boost with python 3.6.2 which will be used to build boost_python as well, so this way I can use python 3 against boost_python.

Anyone has bumped into this before?

Thanks @JohnZwinck for pointing out the obvious over-looked error I had and @James for sharing his answer. but it seems his answer is for Boost 1.63.0, so I wanted to post a solution here so anyone who's having problem with latest CMAKE and Boost Python (up to today) can save some head scratching time.

some prep work first, so go ahead download CMAKE 3.9.3, please beware that if you are using Boost 1.65.0 or above, you will need to use at least CMAKE 3.9.3, CMAKE explicitly bundles with different Boost versions and 3.9.3 is the one shipped with 1.65.0 or above.

Otherwise, you may get an error from CMAKE saying

imported targets not available for boost version

Install Boost 1.65.1 (with python3.6.2)

download Boost 1.65.1 and extract it under /usr/local

you can just follow Boost official guide (getting started guide) to install boost with python2, it should be hassle-free.

but to install boost with python3, you will firstly need to add a user-config.jam file and specify the python version you want to use to build Boost (Boost Python). You will need to specify the parameter on command line like James did ( ./bootstrap --with-python=Python3 ), and add an user-config.jam in your home directory.

firstly, you should create a user-config.jam under your /home/$USERNAME/ (a subdirectory of /home ). You can specify your compiler (gcc, clang, etc), and some other stuff, and for us it is the python version.

to create a user-config.jam, you can do

$ sudo cp /usr/local/boost_1_65_1/tools/build/example/user-config.jam $HOME/user-config.jam

inside your user-config.jam file, add this line:

using python : 3.6 : /usr/bin/python3.6 : /usr/include/python3.6 : /usr/lib ;

replace with your python 3 version.

now we are building and installing Boost 1.65.1

$ ./bootstrap.sh --prefix=/usr/local --with-python=python3

$ ./b2 --install -j 8 # build boost in parallel using all cores available

once it's finished make sure you in your .profile add:

export INCLUDE="/usr/local/include/boost:$INCLUDE"

export LIBRARY_PATH="/usr/local/lib:$LIBRARY_PATH"

export LD_LIBRARY_PATH="/usr/local/lib:$LD_LIBRARY_PATH"

Setting up CMAKELists.txt

The one in the question details works just fine; but once you have followed the above steps, a simple CMAKELists.txt like below should suffice.

cmake_minimum_required( VERSION 2.8 FATAL_ERROR )
project( boostpythondemo )     

find_package( PythonLibs 3.6 REQUIRED )
include_directories( ${PYTHON_INCLUDE_DIRS} )

find_package( Boost COMPONENTS python3 REQUIRED )

if( Boost_FOUND )
        MESSAGE("********************************FOUND BOOST***********************")
endif( Boost_FOUND )           

include_directories( ${Boost_INCLUDE_DIRS} )
link_directories( ${Boost_LIBRARIES} )

add_library( heyall SHARED heyall.cpp )

add_library( heyall_ext SHARED heyall_ext.cpp )
target_link_libraries( heyall_ext ${BOOST_LIBRARIES} heyall )
set_target_properties( heyall_ext PROPERTIES PREFIX "" )

Apparently the BOOST_FOUND message was for debugging you can safely remove it.

now you should just go ahead build using cmake & make .

There are some dependencies for both CMake and Boost, so I am removing my old answer and providing a link to the bash script on GitHubGist.

The script can be found here

To run the script first make it executable

chmod +x boost_python3_install.sh

then run with sudo.

sudo ./boost_python3_install.sh

enjoy!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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