2

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.

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

Comments

1

Did you try

find_package(Boost REQUIRED python)

also run with verbosity to see what is going on

cmake . --debug-output
make VERBOSE=1

1 Comment

I just added it and now I get a new error: /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/libboost_python.so: undefined reference to `PyString_InternFromString'. Also the debug output showed that findPythonLibs apparently finds the Libs for Python 3.4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.