3

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.

Fabian
  • 990
  • 3
  • 11
  • 24

1 Answers1

4

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.

DavidW
  • 29,336
  • 6
  • 55
  • 86
  • I changed to what you suggested and it still cannot link; and why did you suggest removing linking to `boost_python`? The link in your post also has example linking against `boost_python` (`TARGET_LINK_LIBRARIES(yay ${Boost_LIBRARIES})`) – Fabian Mar 24 '15 at 08:52
  • I didn't suggest removing the link to boost python (sorry if that was unclear! I just wanted to emphasise that the python and boost python libraries are different). I meant adding a target_link_libraries for python too. Keep linking to boost... – DavidW Mar 24 '15 at 09:12
  • Still same error even after adding `find_package(Python)` and `target_link_libraries(HelloWorld ${Python_LIBRARY} ${Boost_LIBRARIES})` – Fabian Mar 24 '15 at 19:06
  • 1
    If I change `find_package(Python)` to `find_package(PythonLibs)` and `Python_LIBRARY` to `PYTHON_LIBRARIES` then it works. However, I still don't understand (i) Why in many tutorials, linking against PYTHON_LIBRARIES seems not needed (ii) What's the difference between Python (your suggestion) and PythonLibs – Fabian Mar 24 '15 at 20:15
  • I don't know why many tutorials don't seem to link against Python directly. `find_package(Python)` vs `find_package(PythonLibs)` is just a mistake on my part! – DavidW Mar 24 '15 at 20:40
  • Thanks for helping me on this issue. I just asked a sub-sequent question (http://stackoverflow.com/questions/29245558/how-to-link-with-python3-libs-with-cmake). If you know how to troubleshoot that, that would be great :D. – Fabian Mar 25 '15 at 00:41