与 Boost (Homebrew) Mac c++ 的链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12518973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:21:10  来源:igfitidea点击:

Link with Boost (Homebrew) Mac c++

c++boostg++makefile

提问by JonMorehouse

Hello I'm trying to link with boost to use the threading library, but can not seem to get it built.

您好,我正在尝试与 boost 链接以使用线程库,但似乎无法构建它。

I installed boost with HomeBrew (mac package installer) and it is in the /usr/local/Cellar/boost/1.50.0directory.

我用 HomeBrew(mac 包安装程序)安装了 boost,它在/usr/local/Cellar/boost/1.50.0目录中。

My main file is very simple right now...

我的主文件现在非常简单......

#include <iostream>
#include <boost/thread.hpp>

My make file is like this:

我的make文件是这样的:

CC = g++


BASE_FLAGS = -m32 -wAll

# INCLUDE BASE DIRECTORY AND BOOST DIRECTORY FOR HEADERS
LDFLAGS = -I/usr/local/Cellar/boost/1.50.0/include -I/opt/local/include

# INCLUDE BASE DIRECTORY AND BOOST DIRECTORY FOR LIB FILES
LLIBFLAGS = -L/usr/local/Cellar/boost/1.50.0/

# SPECIFIY LINK OPTIONS
LINKFLAGS = -l boost_thread-mt -lboost_system

# FINAL FLAGS -- TO BE USED THROUGHOUT
FLAGS = $(BASE_FLAGS) $(LLIBFLAGS) $(LDFLAGS) $(LINKFLAGS)




# NOTE FOR BOOST -- YOU ONLY NEED TO INCLUDE THE PATH BECAUSE IT ONLY INSTALLS HEADER FILES
main: main.cpp
    $(CC) $(FLAGS) -o main.out main.cpp

And when I run this, I get a library not found for boost_system. If i take out the boost_system, then I get an error that looks like this:

当我运行它时,我得到了一个找不到 boost_system 的库。如果我取出 boost_system,则会收到如下所示的错误:

ld: warning: ignoring file /usr/local/lib/libboost_thread-mt.dylib, file was built for unsupported file format ( 0xcf 0xfa 0xed 0xfe 0x 7 0x 0 0x 0 0x 1 0x 3 0x 0 0x 0 0x 0 0x 6 0x 0 0x 0 0x 0 ) which is not the architecture being linked (i386): /usr/local/lib/libboost_thread-mt.dylib
Undefined symbols for architecture i386:
  "boost::system::system_category()", referenced from:
      __static_initialization_and_destruction_0(int, int)in ccKwJWzr.o
  "boost::system::generic_category()", referenced from:
      __static_initialization_and_destruction_0(int, int)in ccKwJWzr.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
make: *** [main] Error 1

回答by teopeurt

IF you have just used brew install boostwith no options, this builds a 64-bit binary - both static and dynamic.

如果您刚刚使用brew install boost没有选项,这将构建一个 64 位二进制文​​件 - 静态和动态。

Your main culprit from the code above is the use of -m32option, remove this and you should be okay. This means you are trying to link a 32-bit build against a 64-bit library.

上面代码的罪魁祸首是使用-m32选项,删除它,你应该没问题。这意味着您正在尝试将 32 位构建链接到 64 位库。

The Boost libraries are symbolic linked to the actual binaries and headers in /usr/local/Cellar/- (/usr/local/liband /usr/local/include). Your PATH should include these, so no need to specify these in your makefile.

Boost 库通过符号链接到/usr/local/Cellar/- (/usr/local/lib/usr/local/include) 中的实际二进制文件和头文件。你的 PATH 应该包括这些,所以不需要在你的 makefile 中指定这些。

Note that brew (by extension gcc) generally builds 64-bit binaries by default, which from your error output the Boost libraries have been built on. (you can check which architecture a library is by using these tools otool, fileor lipo)

请注意,brew(通过扩展 gcc)通常默认构建 64 位二进制文​​件,从您的错误输出中,Boost 库已构建在其上。(您可以使用这些工具检查库的架构otoolfile或者lipo

回答by orzechow

As indicated by Jesse Good you have a problem with the -mt suffix. For all running again into this (even on Windows):

正如 Jesse Good 所指出的,-mt 后缀有问题。对于再次遇到此问题的所有人(即使在 Windows 上):

It turns out that the -mt suffix (standing for multi-thread support) isn't used in Linux boost installations, but on Mac and others. Boost on Linux is still multi-thread supported, it's just a naming convention. See also https://stackoverflow.com/a/2310892for this.

事实证明,-mt 后缀(代表多线程支持)不用于 Linux boost 安装,而是用于 Mac 和其他安装。Linux 上的 Boost 仍然支持多线程,这只是一个命名约定。另请参阅https://stackoverflow.com/a/2310892

That's why you have to add the -mt suffix to all your boost libs in the link options on Mac and Windows, but not on Linux. If you are using CMake with Linux co-developers (like I do) you can modify CMakeLists.txt like this:

这就是为什么您必须在 Mac 和 Windows 上的链接选项中为所有 boost 库添加 -mt 后缀,而在 Linux 上则不需要。如果您正在与 Linux 合作开发人员一起使用 CMake(就像我一样),您可以像这样修改 CMakeLists.txt:

if(APPLE OR WIN32)
    target_link_libraries([other libs..] boost_thread-mt boost_system-mt)
else()
    target_link_libraries([other libs..] boost_thread boost_system)
endif()