Link with Boost (Homebrew) Mac c++

31,639

Solution 1

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

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

The Boost libraries are symbolic linked to the actual binaries and headers in /usr/local/Cellar/ - (/usr/local/lib and /usr/local/include). Your PATH should include these, so no need to specify these in your 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, file or lipo)

Solution 2

As indicated by Jesse Good you have a problem with the -mt suffix. For all running again into this (even on 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/2310892 for this.

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:

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()
Share:
31,639
JonMorehouse
Author by

JonMorehouse

Engineer. Innovator. Entrepreneur.

Updated on July 09, 2022

Comments

  • JonMorehouse
    JonMorehouse almost 2 years

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

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

    My main file is very simple right now...

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

    My make file is like this:

    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:

    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
    
  • inetknght
    inetknght over 11 years
    What if he does want to build for 32-bit?
  • Cattani Simone
    Cattani Simone about 10 years
    I'm sorry, I'm having some problems with the installation of boost. when I install boost with brew it doesn't create any "symbolic link" in the /usr/local/include, and if I try to include the library in a XCode project it doesn't find the library, how can I fix it?
  • Chris
    Chris about 8 years
    Actually, boost does generally build both versions on Mac OS X (one with and one without multithreading). This isn't just a naming convention and you can explicitly disable the single-threading variant in homebrew with --without-single. However, there is no single threaded version for boost_thread it appears.