How can I get CMake to find my alternative Boost installation?
After digging around in CMake and experimenting, I determined that CMake was unhappy with the fact that all of my Boost libraries were contained in /usr/local/lib/boost
and not /usr/local/lib
. Once I soft-linked them back out, the build worked.
CMake does not find Boost on Custom Location on Windows
I have found a solution.
As mentioned in the last edit I made to my question, the first issue with the compiler could be fixed with the -DBoost_COMPILER=-vc142
option.
The second could be solved by using the -DBoost_USE_STATIC_LIBS=ON
.
So the set
block in the CMakeLists
for boost is now:
set(BOOST_ROOT "boost_1_73_0")
set(Boost_COMPILER "-vc142")
set(Boost_USE_STATIC_LIBS ON)
With these variables set, CMake runs without any problems.
Cmake doesn't find Boost
Are you sure you are doing it the correct way? The idea is that CMake sets BOOST_INCLUDE_DIR
, BOOST_LIBRARYDIR
and BOOST_ROOT
automatically. Do something like this in CMakeLists.txt
:
FIND_PACKAGE(Boost)
IF (Boost_FOUND)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
ADD_DEFINITIONS( "-DHAS_BOOST" )
ENDIF()
If boost is not installed in a default location and can, thus, not be found by CMake, you can tell CMake where to look for boost like this:
SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "C:/win32libs/boost")
SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "C:/win32libs/boost/lib")
Of course, those two lines have to be before the FIND_PACKAGE(Boost)
in CMakeLists.txt
.
How to change where CMakeLists.txt looks for Boost Libraries Ubuntu
According to the FindBoost documentation (http://www.cmake.org/cmake/help/v3.1/module/FindBoost.html), you can set a CMake variable BOOST_ROOT
to give CMake a hint about where to look.
In your CMakeLists.txt file, you can add the following before the find_package(Boost...)
line:
set(BOOST_ROOT /usr/local)
Update:
I agree with the comments that putting machine specific configuration parameters directly in CMakeLists.txt is not best practice.
As an alternative to directly setting this variable, you can pass options like this to the cmake process in CLion by doing the following:
Navigate to File -> Settings... -> Build, Execution, and Deployment -> CMake
. Under Generation
, add -DBOOST_ROOT=/usr/local
to CMake options
.
Cmake can find boost but not boost core
As I know there is no such a boost library core
. You can check if a library should be linked here.
And Boost.Iterator
is a header-only library, so you don't need to link anything. Just include <boost/iterator/...>
. If you can't include, check whether these includes actually exist in your local boost distro.
I checked it for boost::counting_iterator<int>
and all works well for me.
How to link C++ program with Boost using CMake
In CMake you could use find_package
to find libraries you need. There usually is a FindBoost.cmake
along with your CMake installation.
As far as I remember, it will be installed to /usr/share/cmake/Modules/
along with other find-scripts for common libraries. You could just check the documentation in that file for more information about how it works.
An example out of my head:
FIND_PACKAGE( Boost 1.40 COMPONENTS program_options REQUIRED )
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
ADD_EXECUTABLE( anyExecutable myMain.cpp )
TARGET_LINK_LIBRARIES( anyExecutable LINK_PUBLIC ${Boost_LIBRARIES} )
I hope this code helps.
- Here's the official documentation about FindBoost.cmake.
- And the actual FindBoost.cmake (hosted on GitHub)
Related Topics
How to Configure Qt For Cross-Compilation from Linux to Windows Target
Automating Telnet Session Using Bash Scripts
How to Find the Original User Through Multiple Sudo and Su Commands
Exit Code of Variable Assignment to Command Substitution in Bash
Linux Flock, How to "Just" Lock a File
Using Grep to Get the Next Word After a Match in Each Line
Does "Argument List Too Long" Restriction Apply to Shell Builtins
How Does Mmap Improve File Reading Speed
How to Calculate Time For an Asm Delay Loop on X86 Linux
How to Count Lines in a Document
Ipc Performance: Named Pipe VS Socket
How to Split CSV Files as Per Number of Rows Specified
Linux and I/O Completion Ports
Shell/Bash Shortcut For Bulk Renaming of Files in a Folder
Make -J 8 G++: Internal Compiler Error: Killed (Program Cc1Plus)
How to Set Environment Variable For Everyone Under My Linux System