简体   繁体   English

链接到boost python时出错

[英]Error linking against boost python

Here's my simple HelloWorld program 这是我简单的HelloWorld程序

#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 这是我的CMakeLists.txt文件

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) 我想你错过了Python库的链接(而不是Boost Python库)

Try something like find_package(Python) then target_link_libraries(HelloWorld ${Python_LIBRARY}) 尝试类似find_package(Python)然后尝试find_package(Python) 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 . 此外(基于此帖子https://www.preney.ca/paul/archives/107 )您正在构建的库的名称与BOOST_PYTHON_MODULE给出的名称不匹配。 Change it to BOOST_PYTHON_MODULE(libHelloWorld) because cmake implicitly adds a lib to the module name. 将其更改为BOOST_PYTHON_MODULE(libHelloWorld)因为cmake隐式地将lib添加到模块名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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