Linking boost library with Boost_USE_STATIC_LIB OFF on Windows
I believe you need to add
add_definitions( -DBOOST_ALL_NO_LIB )
See http://www.boost.org/doc/libs/1_57_0/libs/config/doc/html/index.html. I have it set in my CMakeLists.txt and it works for my visual studio builds with boost. As a test, I removed it and got the same error you did.
For what it is worth, here is how I use boost with cmake.
# boost
set(Boost_NO_SYSTEM_PATHS true)
set (Boost_USE_STATIC_LIBS OFF CACHE BOOL "use static libraries from Boost")
set (Boost_USE_MULTITHREADED ON)
find_package(Boost REQUIRED
COMPONENTS
system program_options thread filesystem
date_time chrono timer regex serialization
)
include_directories(${Boost_INCLUDE_DIRS})
link_libraries(${Boost_LIBRARIES})
if (WIN32)
# disable autolinking in boost
add_definitions( -DBOOST_ALL_NO_LIB )
# force all boost libraries to dynamic link (we already disabled
# autolinking, so I don't know why we need this, but we do!)
add_definitions( -DBOOST_ALL_DYN_LINK )
endif()
How can I optionally link against static or dynamic boost library using CMake?
To force the FindBoost CMake module to search for the desired libraries again, you have to clear the cache variables Boost_INCLUDE_DIR
and Boost_LIBRARY_DIRS
, i.e.:
set(Boost_USE_STATIC_LIBS ${USE_STATIC_BOOST})
set(Boost_USE_MULTITHREADED ON)
unset(Boost_INCLUDE_DIR CACHE)
unset(Boost_LIBRARY_DIRS CACHE)
find_package(Boost REQUIRED COMPONENTS thread program_options system)
if(USE_STATIC_BOOST)
message(STATUS "Linking against boost static libraries")
else()
message(STATUS "Linking against boost dynamic libraries")
endif()
Note that the argument CACHE
is necessary to make the unset command clear the variables in the cache.
Also note that once the option variable USE_STATIC_BOOST
has been cached, you need to explicitly set the variable from the command line or edit the value in the cache file to make CMake notice the change:
cmake ../.. -DUSE_STATIC_BOOST=NO
Problem with linking Boost 1.79 libs, builded with MinGW GCC, with CMake on Windows
Well, I solve my problem by setting Boost_DEBUG
to ON
. After analyzing debug info it became clear that the problem was two empty variables: Boost_COMPILER
and Boost_ARCHITECTURE
. And to solve the problem i just set these variables by looking at the full filename, for example:
We have filename libboost_log-clang14-mt-x32-1_79.lib
, and we need this parts: -clang14
and -x32
. You should look at this parts at your filename and set this parts as CMake variables:
Boost_ARCHITECTURE
= -x32
Boost_COMPILER
= -clang14
How do you add Boost libraries in CMakeLists.txt?
Put this in your CMakeLists.txt
file (change any options from OFF to ON if you want):
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.45.0 COMPONENTS *boost libraries here*)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(progname file1.cxx file2.cxx)
target_link_libraries(progname ${Boost_LIBRARIES})
endif()
Obviously you need to put the libraries you want where I put *boost libraries here*
. For example, if you're using the filesystem
and regex
library you'd write:
find_package(Boost 1.45.0 COMPONENTS filesystem regex)
Link the static versions of the Boost libraries using CMake
In your CMakeLists.txt
file:
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost REQUIRED ...)
Where I have ...
, you optionally put the names of the libraries you want to use, and then target_link_libraries(targetname ${Boost_LIBRARIES})
later below. If you have a fairly recent distribution of CMake, it should work exactly as advertised. I do it exactly this way in my own projects.
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()
Related Topics
How to Use Makefiles in Visual Studio
G++ Error:/Usr/Lib/Rpm/Redhat/Redhat-Hardened-Cc1: No Such File or Directory
Recognize Open and Closed Shapes Opencv
Best Way to Start a Thread as a Member of a C++ Class
Reference to Non-Static Member Function Must Be Called
How to Create a Thread-Safe Singleton Pattern in Windows
Direct Boost Serialization to Char Array
C++ "Named Parameter Idiom" VS. Boost::Parameter Library
Creating a New Object from Dynamic Type Info
Send Email with Attachment in C++
Using Continue in a Switch Statement
How to Use C Source Files in a C++ Project
What Is Void* and to What Variables/Objects It Can Point To
Automatically Generate C++ File from Header