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

所以我准备把笔记本电脑从窗户里扔出窗外,去烧掉Apple HQ.

请参阅下面的更新:

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

clang++ <FLAGS/INCLUDES> -o hello.so hello.cpp 

调用响应:

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

非常感谢任何帮助.我想我已经包含了所有必要的东西.如果您需要额外信息,请与我们联系.

设置:

> OSX 10.11.6(El Capi-s#@%)
>必须使用Xcode 7.3(和适当的CLT):NVIDIA对CUDA编程的要求(已安装).
>必须使用Python3(已安装Homebrew)

> brew install python3
>哪个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安装加速
> brew install boost-python –with-python3 –without-python
> /usr/local/Cellar/boost-python/1.62.0/

>使用LLVM 3.9.0(安装了Homebrew)

> brew install llvm –universal

现在您可以要求一些有用的东西:

Clang:

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

makefile中的FLAGS和INCLUDES:

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

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

HELLO.CPP

#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)
    ;
};

最佳答案 经过多次心痛和痛苦,我有了答案!

对于那些使用OSX和自制程序的人来说,这就是你如何做到的.

> brew install python3 Python3具有UCS4本机(Unicode),这是此过程的重要部分.如果你需要Python2,那么确保它是为UCS4配置的,因为它本身就是UCS2.
> brew install boost首先安装一般提升.
> brew install boost-python –with-python3 –without-python这将为Python3安装boost-python而不用Python2.如果需要Python2,可以更改这些选项.
> brew install llvm –universal确保你安装了llvm,这应该包含clang,这是我们将使用的编译器(不是Xcode).
>创建一个makefile(见下文),其中包含所有python和boost头/库的目录,AND包含您要使用的库. (这是让我失望的原因,我有目录,但没有指定编译器应该使用的目录中的哪个库).

我的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)

然后你需要做的就是运行带有所有内容的makefile,一切都应该是甜蜜的:)我希望这可以帮助某人并消除我试图让insertProfanity提升工作的痛苦.

点赞