Reference boost from CMake in CLion - c++

CLion 1.2, with bundled CMake 3.3.2 and MinGW-w64 4.8.4
I'm trying to reference boost in CMakeLists.txt
set(BOOST_ROOT "O:/Project/lib/windows/boost_1_59_0")
set(BOOST_LIBRARYDIR ${BOOST_ROOT}/stage/lib)
set(BOOST_COMPONENTS_NEEDED filesystem )
find_package(Boost 1.59.0 REQUIRED COMPONENTS ${BOOST_COMPONENTS_NEEDED})
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
If there is no libraries needed, so I use
find_package(Boost 1.59.0)
boost is found and all works well.
But when I'm trying to reference libraries "Boost_FOUND" is not set
Boost libraries is built and there are following files in the O:/Project/lib/windows/boost_1_59_0/stage/lib folder
boost_filesystem-vc120-mt-1_59.dll
boost_filesystem-vc120-mt-1_59.lib
boost_filesystem-vc120-mt-gd-1_59.dll
boost_filesystem-vc120-mt-gd-1_59.lib
boost_system-vc120-mt-1_59.dll
boost_system-vc120-mt-1_59.lib
boost_system-vc120-mt-gd-1_59.dll
boost_system-vc120-mt-gd-1_59.lib
libboost_filesystem-vc120-mt-1_59.lib
libboost_filesystem-vc120-mt-gd-1_59.lib
libboost_filesystem-vc120-mt-s-1_59.lib
libboost_filesystem-vc120-mt-sgd-1_59.lib
libboost_filesystem-vc120-s-1_59.lib
libboost_filesystem-vc120-sgd-1_59.lib
libboost_system-vc120-mt-1_59.lib
libboost_system-vc120-mt-gd-1_59.lib
libboost_system-vc120-mt-s-1_59.lib
libboost_system-vc120-mt-sgd-1_59.lib
libboost_system-vc120-s-1_59.lib
libboost_system-vc120-sgd-1_59.lib
What I missed?

Probably because you want to build it your project with MinGW, but your libraries are compiled for Visual studio (you can see it from vc120 in libraries name).
You must build boost with MinGW-64 (you can use the same stage/lib folder because names are different).
Open the MinGW console and follow the same compilation step that you use for Visual Studio, but change toolset from msvc to gcc.

Related

How to use the LLVM IR API

I have just finished building LLVM with ninja, clang-cl and cmake, and I was wondering how do I can get this working so I can include LLVM headers and libraries in my C++ visual studio project. I want to do something similar to what Kaleidoscope does, the tutorial by LLVM.
I am just wondering how I can compile my project using llvm headers and libraries. Should I copy the llvm build into the project so the headers work? Should I shange the project settings to include libs/dlls that are required? How should I do this?
Figured it out!
First I rebuilt with visual studio (release x64). Then I used cmake to generate .sln filed for project (instead of trying to use llvm-config). The important part was to set LLVM_DIR to the right location, for me this was llvm-project\llvm\cmake\modules.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.4.3)
project(SimpleProject)
set(LLVM_DIR C:\\...\\llvm-project\\llvm\\cmake\\modules)
find_package(LLVM REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
add_executable(test test.cpp)
llvm_map_components_to_libnames(llvm_libs support core irreader)
target_link_libraries(test ${llvm_libs})

Building TBB using Visual Studio 2015 x64 and CMake

i would like to build TBB to use it in another CMake project. I tried to build TBB from the Github source using the makefile (upgraded with VisualStudio 2015). This failed due to a mysterious error:
LINK : fatal error LNK1181: cannot open input file 'opencv_core300.lib'
Where does this error could originate from?
My second try was is to build TBB using another repository that allows a build using CMake. This build produces an tbb.lib, tbb.dll, etc. file.
Now I am stuck how to incorporate is in my other cmake files. There is no TBBConfig.cmake or similar.
My CMakeLists.txt for my project looks like this:
cmake_minimum_required(VERSION 3.10)
project(IntrafraktionelleRegistrierung)
find_package(ITK REQUIRED
COMPONENTS
ITKRegistrationCommon
ITKRegistrationMethodsv4
)
include(${ITK_USE_FILE})
set(SRC
${CMAKE_PROJECT_NAME}.cxx
)
if (DEFINED ENV{TBBROOT})
message(STATUS "TBBROOT: $ENV{TBBROOT}")
else()
message(STATUS "TBBROOT not defined!")
endif()
find_package(TBB REQUIRED)
add_executable(${CMAKE_PROJECT_NAME} ${SRC})
target_link_libraries( ${CMAKE_PROJECT_NAME}
${ITK_LIBRARIES}
tbb
)'
TBBROOT is the build directory of tbb. The FindTBB.cmake I have available is borrowed from here and copied to the modules directory of cmake.
The latest version of the binaries of TBB have a CMake folder with TBBConfig.cmake inside. I used this to link the TBB to my project but somehow I ended up by an error stating: "tbb-NOTFOUND.obj cannot be found". (This way is still under investigation.
Has someone used this repository to configure and build a cmake project?
About CMake related questions
Basically you have two options:
Integration of pre-built TBB binaries into your project
You can use the binaries of TBB (just as you did) according to the following example. After invocation of find_package(TBB REQUIRED) you will get TBB targets in a format TBB::<component> (e.g. TBB::tbb, TBB::tbbmalloc, etc). Also TBB_IMPORTED_TARGETS variable will contain all imported TBB targets.
So, you need to slightly modify your target_link_libraries:
target_link_libraries( ${CMAKE_PROJECT_NAME}
${ITK_LIBRARIES}
${TBB_IMPORTED_TARGETS}
)
or
target_link_libraries( ${CMAKE_PROJECT_NAME}
${ITK_LIBRARIES}
TBB::tbb
)
Also you can update your find_package if you need only TBB::tbb component in your project: find_package(TBB REQUIRED tbb)
Integration of TBB source code into your project
You can use tbb_build (it is a CMake wrapper using GNU Make on TBB Makefiles), but to run it on Windows under Visual Studio you will need to have GNU Make in your environment.
If you'd like to integrate this TBB (with CMake support) you can use add_subdirectory(<YOUR-TBB-ROOT>) (replace <YOUR-TBB-ROOT> with actual location of TBB) instead of find_package(TBB REQUIRED).

CMake is not finding Boost library binaries (new naming convention for the binaries)

I begin by saying that this is not an issue with environment variables.
When I use the header-only libraries, everything works fine with this:
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost REQUIRED)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(TestCMake Program.cpp)
target_link_libraries(TestCMake ${Boost_LIBRARIES})
endif()
But when I try to require a library by changing find_package(Boost REQUIRED) to find_package(Boost REQUIRED COMPONENTS system) I get an error:
Unable to find the requested Boost libraries.
Boost version: 1.67.0
Boost include path: D:/boost/boost_1_67_0
Could not find the following Boost libraries:
boost_system
No Boost libraries were found. You may need to set BOOST_LIBRARYDIR to the
directory containing Boost libraries or BOOST_ROOT to the location of
Boost.
This answer on another question suggests that CMake expects the Boost binaries to be named in a certain way:
boost_thread-vc100-mt-1_51
boost_thread-vc100-mt
boost_thread-mt-1_51
boost_thread-mt
boost_thread
I have a precompiled binary (acquired from here) named boost_system-vc141-mt-x32-1_67.lib. How could I make CMake recognize the naming convention used in my binaries?
Comments for this downvoted answer on that other question suggest against renaming the files.
In newer versions of Boost (1.66 and forward), the naming convention of the binaries has changed. Now, there is an additional x64 or x32.
As such, only CMake versions posterior to the 1.66 Boost release have this fix, which is the case starting at 3.11.
So two options for you:
Upgrade your CMake version to something above 3.11
Use the FindBoost.cmake from one of these newer CMake versions (they usually are compatible).
The latter solution would have to be used if you are "stuck" with one version due to company policies.

Cannot find boost libraries with cmake

I've install boost 1.63.0 on Windows and I am trying to build with CMake (using Visual Studio 2017 as a generator). I'm having trouble getting find_package() to find my boost libraries and I can't figure out why.
CMakeLists.txt:
find_package(Boost REQUIRED COMPONENTS system filesystem thread)
Output:
CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.8/Modules/FindBoost.cm
ake:1813 (message):
Unable to find the requested Boost libraries.
Boost version: 1.63.0
Boost include path: C:/Program Files (x86)/boost/boost_1_63_0
Could not find the following Boost libraries:
boost_system
boost_filesystem
boost_thread
Some (but not all) of the required Boost libraries were found. You may
need to install these additional Boost libraries. Alternatively, set
BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT
to the location of Boost.
Boost finds the includes, but not the libraries. Headers are located at:
%BOOST_ROOT%. Libraries are located at %BOOST_ROOT%/stage/lib. When I look at _boost_LIBRARY_SEARCH_DIRS_RELEASE, the first place it looks is the correct location. I've also tried hard-coding BOOST_LIBRARYDIR to that path just to be sure.
To install boost I extracted the downloaded archive to %BOOST_ROOT%, then ran bootstrap and .\b2 link=static,shared threading=single,multi. This should give me all versions of the libraries. In the case of boost:system, I have the following binaries in %BOOST_ROOT%/stage/lib%:
boost_system-vc100-mt-1_63.dll
boost_system-vc100-mt-1_63.lib
boost_system-vc100-mt-gd-1_63.dll
boost_system-vc100-mt-gd-1_63.lib
libboost_system-vc100-mt-1_63.lib
libboost_system-vc100-mt-gd-1_63.lib
I've tried enabling and disabling the following, but to no avail:
set( Boost_USE_STATIC_LIBS ON )
set( Boost_USE_MULTITHREADED OFF )
set( Boost_DEBUG ON )
Here is an interesting part. The Boost_DEBUG parameter spits out this line:
Searching for SYSTEM_LIBRARY_RELEASE: boost_system-vc141-mt-1_63;boost_system-vc141-mt;boost_system-mt-1_63;boost_system-mt;boost_system
Note the vc141 versus vc100. I think .\b2 built something for vc100. That's strange because I was running it from the Dev Command Prompt for VS 2017. I've taken a wild guess and tried to build boost with ./b2 toolset=msvc-14.1 but I get an error: *** argument error * rule maybe-rewrite-setup ( toolset : setup-script : setup-options : version : rewrite-setup ? )".
How can I ensure that I compile boost with VS2017 or MSVC141?
This thread seems related:
Version numbers for Visual Studio 2017, Boost and CMake
Check the FindBoost.cmake script that is being used. Depending on the version of CMake you use, this version of Boost may not be handled. The dependencies between libraries is set depending on the version of Boost found.
For example the latest version of the script in CMake sources on GitHub handles version 1.63. I had the problem with CMake v3.6.2 which does not handle it.
Regarding the version mismatch for MSVC I don't know, sorry.
I compiled boost, and am compiling the linking application with the same toolset. Therefore I decided that it was safe to simply rename all compiled libs from *-vc100-* to *-vc141-*. While normally I would discourage that (you could get subtle differences in the ABI), in this case I was certain that it was the same compiler and so it's clear that either cmake or b2 had a bug which created(or searched) a file with the wrong name.
After doing that, cmake not only found boost, but linked successfully.

generate vs studio can not open boost library

I have a cmake file that adds boost to a project.
the section that I add boost to project is as follow:
set(Boost_USE_STATIC_LIBS OFF)
set(BOOST_ROOT $ENV{BOOST_ROOT})
set(BOOST_LIBRARYDIR ${BOOST_ROOT}/lib64-msvc-12.0)
find_package(Boost COMPONENTS thread system unit_test_framework filesystem REQUIRED)
and I add boost to my project in this way:
target_link_libraries(MyProject
${Boost_FILESYSTEM_LIBRARY})
When I compile my code, I am getting this error:
cannot open file 'libboost_filesystem-vc120-mt-gd-1_57.lib'
looking at project file, I can see that this library was added to project in correct way (input section of linker):
C:\Local\boost\lib64-msvc-12.0\boost_filesystem-vc120-mt-gd-1_57.lib
I have a similar setup on another computer which works perfectly. The difference between the two computer is:
working computer: windows 7 +VS 2012
not working computer Windows 8.1 + Visual Studio 2013.
Since libboost_filesystem-vc120-mt-gd-1_57.lib is obviously not the same as
boost_filesystem-vc120-mt-gd-1_57.lib you will have to either change the name in the generated project's properties or in cmake itself.