简体   繁体   中英

Error linking against boost python

Here's my simple HelloWorld program

#include <boost/python.hpp>
using namespace boost::python;

void greet() {
    // do nothing
}

BOOST_PYTHON_MODULE(HelloWorld)
{
    def("greet", greet);
}

and here's my CMakeLists.txt file

cmake_minimum_required(VERSION 2.8.4)
project(HW)

find_package(Boost COMPONENTS python3 REQUIRED)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories(${Boost_INCLUDE_DIRS} /Library/Frameworks/Python.framework/Versions/3.4/include/python3.4m include)

file(GLOB_RECURSE SRC
HelloWorld.cpp
)

add_library(HelloWorld SHARED ${SRC})
target_link_libraries(HelloWorld ${Boost_LIBRARIES})

However, I have been unable to build this simple program, with this build error

Undefined symbols for architecture x86_64:
  "__Py_NoneStruct", referenced from:
      boost::python::detail::none() in HelloWorld.cpp.o
      boost::python::api::object::object() in HelloWorld.cpp.o
  "boost::python::detail::init_module(PyModuleDef&, void (*)())", referenced from:
      _PyInit_HelloWorld in HelloWorld.cpp.o
ld: symbol(s) not found for architecture x86_64

What am I missing? Sorry if this looks like a newbie question but I'm actually stuck.

I think you're missing the a link to the Python library (as opposed to the Boost Python library)

Try something like find_package(Python) then target_link_libraries(HelloWorld ${Python_LIBRARY})

Additionally (based on this post https://www.preney.ca/paul/archives/107 ) the name of the library you're building doesn't match the name given in BOOST_PYTHON_MODULE . Change it to BOOST_PYTHON_MODULE(libHelloWorld) because cmake implicitly adds a lib to the module name.

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