简体   繁体   中英

Compile c++ Programm with CMake that uses Boost/python.hpp

I'm currently trying to compile a c++ file using CMake. But since I'm using Boost::python it won't compile. I set up a little test file to figure out what I need to do but I just can't get it to work. Any Help would be greatly appreciated!!

The test file:

#include <Python.h>
#include <boost/python.hpp>
#include <iostream>

using std::cout;
using std::endl;

int main()
{
    namespace py = boost::python;

    Py_Initialize();

    // Retrieve the main module's namespace
    py::object global(py::import("__main__").attr("__dict__"));

    py::exec("print 'Hello from Python!' \n", global, global);

    return 0;
}

It will compile if I just use,

clang++ -I/usr/include/python2.7 -lpython2.7 -lboost_python -std=c++11 boosttest.cpp -o boosttest

I tried this CMakeLists.txt to get it to work.

cmake_minimum_required(VERSION 3.2)

FIND_PACKAGE(PythonLibs)
FIND_PACKAGE(Boost)

INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
LINK_LIBRARIES(${Boost_LIBRARIES} ${PYTHON_LIBRARIES})

add_executable(Test1 boosttest.cpp)
target_link_libraries(Test1 ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})

and what I get is

undefined reference to `boost::python::import(boost::python::str)'

and a couple more of the same category.

Thanks for your help Mark, thanks to the new errors after I included

find_package(Boost REQUIRED python)

I was able to figure out that the problem was that CMake selected the libs for python 3.4 but Boost was build against 2.7.

So the Solution was to include the version as so:

FIND_PACKAGE(PythonLibs 2.7 REQUIRED)

Did you try

find_package(Boost REQUIRED python)

also run with verbosity to see what is going on

cmake . --debug-output
make VERBOSE=1

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