Python3,Boost-Python和Cpp链接器错误

Nin*_*ils 5 c++ python macos boost

所以我要把我的笔记本电脑从窗户扔出去,从窗户上出来,去烧掉苹果总部。

请参阅下面的更新:

我无法让python3,boost-python和clang相互配合。我遇到的错误正在运行:

clang++ <FLAGS/INCLUDES> -o hello.so hello.cpp 
Run Code Online (Sandbox Code Playgroud)

调用响应:

Undefined symbols for architecture x86_64:
  "__Py_NoneStruct", referenced from:
      boost::python::api::object::object() in hello-0c512e.o
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [hello] Error 1
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激。我想我已经包括了所有必要的内容。让我知道您是否需要其他信息。

设置:

  • OSX 10.11.6(El Capi-s#@%)
  • 必须使用Xcode 7.3(和适当的CLT):NVIDIA对CUDA编程的要求(已安装)。
  • 必须使用Python3(已安装Homebrew)
    • brew install python3
    • which python -> / usr / bin / python
    • /usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/include/python3.5m
    • 我为python3.5设置了别名(见下文)。
  • 使用Boost-Python(已安装Homebrew)
    • brew install boost
    • brew install boost-python --with-python3 --without-python
    • / usr / local /地窖/boost-python/1.62.0/
  • 使用LLVM 3.9.0(已安装Homebrew)
    • brew install llvm --universal

现在,您可能需要一些有用的信息:

lang ++:

Apple LLVM version 7.3.0 (clang-703.0.29)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Run Code Online (Sandbox Code Playgroud)

Makefile中的标志和包含:

CPPFLAGS  = -g -Wall -std=c++11 -stdlib=libc++
LDHEADERS = -I/usr/local/opt/llvm/include
LDLIBS = -L/usr/local/opt/llvm/lib
BOOSTHEADERS = -I/usr/local/Cellar/boost/1.62.0/include/boost
BOOSTLIBS = -L/usr/local/Cellar/boost-python/1.62.0/lib
PYTHONHEADERS = -I/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/include/python3.5m
PYTHONLIBS = -L/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib
Run Code Online (Sandbox Code Playgroud)

最后,我尝试编译的代码:

你好

#include <boost/python.hpp>

struct World
{
    void set(std::string msg) { this->msg = msg; }
    std::string greet() { return msg; }
    std::string msg;
};

using namespace boost::python;

BOOST_PYTHON_MODULE(hello)
{
    class_<World>("World")
        .def("greet", &World::greet)
        .def("set", &World::set)
    ;
};
Run Code Online (Sandbox Code Playgroud)

Nin*_*ils 6

在经历了很多心痛和痛苦之后,有了答案!

对于所有使用OSX和自制软件的人,这是您的操作方法。

  1. brew install python3Python3具有UCS4本机(Unicode),这是此过程的重要组成部分。如果您需要Python2,请确保为UCS4配置了它,因为它本身就是UCS2。
  2. brew install boost 首先安装常规增强。
  3. brew install boost-python --with-python3 --without-python这将在没有Python2的情况下为Python3安装boost-python。如果需要Python2,可以更改这些选项。
  4. brew install llvm --universal 确保已安装llvm,其中应包含clang ++,这是我们将使用的编译器(而不是Xcode)。
  5. 创建一个makefile(请参见下文),其中包括所有python和boost头文件/库的目录,并包括要使用的库。(这就是让我震惊的原因,我拥有目录,但未指定编译器应在该目录中使用哪个库)。

我的makefile:

# compiler flags:
#  -g    adds debugging information to the executable file
#  -Wall turns on most, but not all, compiler warnings

COMPILER = /usr/local/Cellar/llvm/3.9.0/bin/clang++
CPPFLAGS  = -g -Wall -std=c++11 -stdlib=libc++

# Python and BoostPython links.
BOOSTHEADERS = -I/usr/local/Cellar/boost/1.62.0/include/boost
BOOSTLIBRARIES = -L/usr/local/Cellar/boost-python/1.62.0/lib/
# This is the boost library we want to use, there are also libraries for multithreading etc. 
# All we do is find the file libboost_python3.a and link it by taking away the 'lib' and '.a'.
BOOSTLIB = -lboost_python3
PYTHONHEADERS = -I/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/include/python3.5m
PYTHONLIBRARIES = -L/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib
# Link to python3.5 library, the same as we did for boost.
PYTHONLIB = -lpython3.5

# Collect links.
LIBRARIES = $(BOOSTLIBRARIES) $(PYTHONLIBRARIES) $(PYTHONLIB) $(BOOSTLIB)
HEADERS = $(BOOSTHEADERS) $(PYTHONHEADERS)

# Build target.
TARGET = hello


# BEGIN MAKE
all: $(TARGET)

$(TARGET): $(TARGET).cpp
    # Note that '-shared' creates a library that is accessible.
    $(COMPILER) -shared $(LIBRARIES) $(HEADERS) $(TARGET).cpp -o $(TARGET).so

clean:
    $(RM) $(TARGET)
Run Code Online (Sandbox Code Playgroud)

然后,您所需要做的就是运行包含所有包含项的makefile,一切都应该很甜:)我希望这对某人有所帮助,并消除了我试图使insertProfanityBoost正常工作的痛苦。