How Boost Auto-Linking Makes Choice

How boost auto-linking makes choice?

Visual Studio allows #pragma directives in the source code to set linker options. For "auto-linking", Boost uses these #pragma's in combination with existing macro's.

In particular, it sounds like you are looking for the BOOST_ALL_DYN_LINK macro.

Dynamic linking boost libraries and the auto-link not working properly?

From inside visual studio: go to your project and right click it to get the context menu. Go to properties. In the properties dialog go to:

Configuration Properties->C/C++->Preprocessor->Preprocessor Definitions

Try putting BOOST_ALL_DYN_LINK (or BOOST_THREAD_USE_DLL) in the Preprocessor Definitions list.

Then rebuild your demo.

How to auto-link boost libraries with CMake

Boost docs:

Auto-Linking

Most Windows compilers and linkers have so-called “auto-linking support,” which eliminates the second challenge. Special code in Boost header files detects your compiler options and uses that information to encode the name of the correct library into your object files; the linker selects the library with that name from the directories you've told it to search.

The GCC toolchains (Cygwin and MinGW) are notable exceptions; GCC users should refer to the linking instructions for Unix variant OSes for the appropriate command-line options to use.

Note that the auto-linking feature has been known to fail on occasion (e.g. when your Boost libraries are installed using a non-standard setting). You can define BOOST_ALL_NO_LIB to disable the feature on Windows.

You shouldn't be hard-coding Boost paths into your CMakeLists.txt, though. Better use the platform-independent find_package:

set( Boost_USE_STATIC_LIBS OFF )
set( Boost_USE_MULTITHREADED ON )
set( Boost_USE_STATIC_RUNTIME OFF )

find_package( Boost 1.72.0 COMPONENTS thread fiber context )

if ( Boost_FOUND )
include_directories( ${Boost_INCLUDE_DIRS} )
link_libraries( learn_asio ${Boost_LIBRARIES} )
else()
message( FATAL_ERROR "Required Boost packages not found. Perhaps add -DBOOST_ROOT?" )
endif()

How boost knows which LIB to link with and how to change it?

  • The first thing I cannot understand is how does boost decides the lib
    name it needs to link to? I searched the boost code for that string,
    but I couldn't find any. Where does this name comes from?

This is auto-link feature. You can see how boost composes the names of the libraries being linked in boost/config/auto_link.hpp header.

  • If I place the requested lib, everything works fine using the boost
    DLL, but I want to statically link to the libraries

Make sure that you link with the CRT statically (Project properties --> C++ --> Code generation --> Runtime library... should be /MT or /MTd). Then boost will be auto-linked statically as well.

Boost autolinks libraries which are not built by Boost, but the intended ones are built

The problem is fixed, during the compilation of the boost libraries, I selected the link=static option. Which creates static libraries. I also selected runtime-link=static option, and this was wrong!

The solution for this problem was compiling boost with runtime-link=shared. Now some extra libraries are added, with the correct filenames, so the linker can find them. At first the compiler still searches for the dll library (boost_python-vc90-mt-gd-1_43.lib, instead of libboost_python-vc90-mt-gd-1_43.lib), everything else from boost links automatically to a static library, but because boost.python has a different auto-linkage set up, when you provide BOOST_PYTHON_STATIC_LIB, it finally links to the right library and it works!

Boost build a new version with VS 2019

Boost library file names describe what they support and how they were built. In your case they support multithreading and were built with VS2019 (as opposed to MinGw, Clang or an earlier version of visual studio). For more information see: how can i decode boost library naming.

boost uses auto linking with visual studio, see how boost auto linking makes choice, so the library file names must match those that are required.

You haven't said why you want to "remove the mt and the toolset from the lib and dlls".

If it is to get boost to link with an existing project, then you'll need to build boost with the correct version of visual studio and without multi-threading, e.g.:

b2 toolset=msvc-??? threading=single ...

See: b2 invocation properties.

Unable to link to static boost library using VS2012 while dynamic linking works fine

Invoke b2 with the following parameters:

link=static runtime-link=static

Note that there are no hyphens before link and runtime-link.

How can I disable automatic linking using boost asio?

it result that when i added the .a file to my project, I needed to change the "file type" to Match-O object code, ( as this answer pointed https://stackoverflow.com/a/6124806/520221 notes), even if you add the file to the "link files to executable" in the build phase tab.

So i think that ASIO in some way knows if the library was already included (statically or dynamically). because of the of the "file type" it never gets linked , so asio include the -lboost_system flag.

another tip, you have to build boost and your project with libstdc++, it does not work with libc++, I don't know why. see this How I do compile a application against a static library compiled with libc++ in xcode/clang/macos? , i think its a bug or something

Boost VS2017 linking to the wrong DLL

When building with Visual Studio, boost has some pragma statements which do the linking for you. This is called "Auto-linking" and it over-rides any command-line arguments you may be passing to the linker.

The solution is to define BOOST_ALL_NO_LIB. This can be done in two ways:

  1. In source code before including boost headers as #define BOOST_ALL_NO_LIB.
  2. It could be added to your cmake file as: add_definitions("-DBOOST_ALL_NO_LIB").
  3. As of CMake 3.5: Use the disable_autolinking imported target:

    target_link_libraries(MyModule Boost::system Boost::filesystem Boost::disable_autolinking)



Related Topics



Leave a reply



Submit