gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:22:09 25 4
gpt4 key购买 nike

所以我打算把我的笔记本电脑扔出窗外,然后去烧掉 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 install boost
    • 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

最后,我要编译的代码:

你好.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 和自制软件的人来说,这里是你如何做的。

  1. brew install python3 Python3 具有原生 UCS4 (Unicode),这是此过程的重要组成部分。如果您需要 Python2,请确保为 UCS4 配置它,因为它本身就是 UCS2。
  2. brew install boost 首先安装 general 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 头文件/库的目录,并包含您要使用的库。 (这让我感到困惑,我有目录但没有指定编译器应该使用该目录中的哪个库)。

我的生成文件:

# 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 boost 工作时遇到的痛苦。

关于Python3、Boost-Python 和 Cpp 链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40732947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com