include cmake headerfile boost.hpp with cmake - c++

I like to add the boost/operators.hpp with cmake
From CMake's FindBoost Documentation:
find_package(Boost 1.36.0)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(foo foo.cc)
endif()
So I added
find_package(Boost 1.60.0)
if (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_library(core ${core_SRCS})
add_executable(app main.cpp)
target_link_libraries(app core)
endif ()
Output from message:
-- Boost version: 1.60.0
-- BOOST_ROOT=~/Projects/ClionProjects/.repo/boost_1_60_0
-- Boost_DIR=Boost_DIR-NOTFOUND
-- Boost_INCLUDE_DIR=/home/dornathal/Projects/ClionProjects/.repo/boost_1_60_0
However it builds (I can run the program and tests), but as soon as I try to include #include<boost/operators.hpp> it won't find it in the test project.
I actually extended one class by the boost::operators<T> and strangely my IDE (CLion) lets me jump to that sourcefile.

The include_directories CMake command adds include directories for the current directory and its subdirectories. Since you get the error in the test project and use include_directories in the main project, I guess the problem is that you have separate directories for these, for example:
src/
CMakeLists.txt - include_directories used here
test/
test.cc - no effect on this file
If this is the case you can either move include_directories up to their common parent directory or use target_include_directories that can propagate the public INCLUDE_DIRECTORIES property.
You can also see the commands passed to your compiler by adding VERBOSE=1 to your make command:
make VERBOSE=1
This shows what include directories are passed to the compiler via the -I... option among other things.

Related

CMake: Cannot link to a static library in a subdirectory

I have the following folder structure in my c++ project
*--build
|---(building cmake here)
|
*--main.cpp
|
*--CMakeLists.txt (root)
|
*--modules
|---application
|------app.h
|------app.cpp
|------CMakeLists.txt
And the code below for both CMakeLists.txt files:
CMakeLists.txt (module)
cmake_minimum_required(VERSION 3.15.2)
file(GLOB APPLICATION_HEADERS *.h *.hpp)
file(GLOB APPLICATION_SRC *.c *.cpp)
add_library(app_lib STATIC
${APPLICATION_HEADERS}
${APPLICATION_SRC})
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR})
CMakeLists.txt (root)
cmake_minimum_required(VERSION 3.15.2)
project(main)
enable_language(C CXX)
#set directories
set(CMAKE_BINARY_DIR build)
set(CMAKE_CONFIGURATION_TYPES UNIX)
set(CMAKE_MODULES_DIR ${SOURCE_DIR}/cmake)
add_executable(${PROJECT_NAME} main.cpp)
# Build sub-modules
include_directories(modules/application)
add_subdirectory(modules/application)
find_library(MY_APP_LIB app_lib REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC ${MY_APP_LIB})
However, when I do cmake .. in my build directory, it seems like my app library just doesn't build and it doesn't link to it. I end up with the following error:
CMake Error at CMakeLists.txt:80 (find_library):
Could not find MY_APP_LIB using the following names: app_lib
I tried looking at other stackoverflow questions but it seems like I'm missing something. Any help is appreciated!
Thanks!
You don't need to use find_* to locate the library. In fact you cannot locate the library this way, since find_library searches the file system for the library during configuration, i.e. before anything gets compiled.
There's good news though: If the targets are created in the same cmake project, you can simply use the name of the cmake target as parameter for target_link_libraries:
...
add_library(app_lib STATIC
${APPLICATION_HEADERS}
${APPLICATION_SRC})
# note: this should be a property of the library, not of the target created in the parent dir
target_include_directories(app_lib PUBLIC .)
...
add_subdirectory(modules/application)
target_link_libraries(${PROJECT_NAME} PUBLIC app_lib)
You don't need to do find_library for your own targets, just link directly to app_lib:
target_link_libraries(${PROJECT_NAME} PUBLIC app_lib)

cmake fails to look up external library(boost) directory

I am trying to use cmake to build a simple parser project. I used boost::program_options in my code, but it seems cmake does not look up the boost lib directory. Get confused and frustrated..
My CMakeLists.txt is
# basic info
CMAKE_MINIMUM_REQUIRED(VERSION 3.1.0)
PROJECT(parser CXX)
SET(CMAKE_CXX_STANDARD 14)
# Boost
FIND_PACKAGE(Boost 1.61.0 REQUIRED PATHS /path/to/boost NO_DEFAULT_PATH)
MESSAGE(STATUS "Boost version: ${Boost_VERSION}" )
MESSAGE(STATUS "Boost include dirs: ${Boost_INCLUDE_DIRS}" )
MESSAGE(STATUS "Boost library dirs: ${Boost_LIBRARY_DIRS}" )
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
# main
FILE(GLOB main_SRC *.cpp)
ADD_EXECUTABLE(main ${main_SRC})
TARGET_LINK_LIBRARIES(main boost_program_options)
I use a modified BoostConfig.cmake (which points to my own Boost library)
The result for running cd build; cmake .. is
-- Boost version: 1.61.0
-- Boost include dirs: /path/to/boost/include
-- Boost library dirs: /path/to/boost/lib
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/build
Thus I believe cmake has found the my Boost library. But then if I run make I will end up with a bunch of errors like
undefined reference to `boost::program_options ... `
If I run make VERBOSE=1 I will see
/path/to/g++ -rdynamic CMakeFiles/main.dir/main.cpp.o -o main -lboost_program_options
the command does not have -L or -Wl,rpath for ${Boost_LIBRARY_DIRS}. If I add the flag manually then I can compile the project successfully.
I also tried linking to static lib by TARGET_LINK_LIBRARIES(main ${Boost_LIBRARY_DIR}/libboost_program_options.a) instead of LINK_DIRECTORIES(), but the same error was thrown.
Not sure what makes things wrong..
Thanks in advance
What happens with the standard method using the imported targets? That is,
# Use and set variable/-Dflag/environment for custom Boost location
set(BOOST_ROOT /path/to/boost)
find_package(Boost 1.61.0 REQUIRED
COMPONENTS program_options)
add_executable(main ...)
target_link_libraries(main Boost::program_options)

gtest setup cmake to get it run

I want to include gtest to my C++ project. I am using Clion as IDE, which should work. Some tests are already working, but I cannot use any functions from B_RocChoice.h. It says that the function is not declared in this scope.
Can somebody tell me what I am doing wrong? How I must change my CMakeLists.txt files that it recogizes my methods?
This is my basic_tests.cpp, where my testcases will be written.
This is my Directory.
Here, the most outer CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(cli)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
set(SOURCE_FILES
include/A_WowbaggerChoice.h
include/AbstractChoice.h
include/B_RocChoice.h
include/C_CnnChoice.h
include/D_DetectorChoice.h
include/E_LearningChoice.h
include/Help.h
include/MyException.h
include/StartScreen.h
include/tinyxml.h
include/types.h
src/A_WowbaggerChoice.cpp
src/AbstractChoice.cpp
src/B_RocChoice.cpp
src/C_CnnChoice.cpp
src/D_DetectorChoice.cpp
src/E_LearningChoice.cpp
src/Help.cpp
src/main.cpp
src/MyException.cpp
src/StartScreen.cpp
tinyxml/tinystr.cpp
tinyxml/tinystr.h
tinyxml/tinyxml.cpp
tinyxml/tinyxml.h)
add_subdirectory(googletest)
add_executable(cli ${SOURCE_FILES})
target_link_libraries( cli ${OpenCV_LIBS} )
CMakeLists.txt for gtest.
cmake_minimum_required(VERSION 2.6.2)
project( googletest-distribution )
enable_testing()
option(BUILD_GTEST "Builds the googletest subproject" ON)
#Note that googlemock target already builds googletest
option(BUILD_GMOCK "Builds the googlemock subproject" OFF)
if(BUILD_GMOCK)
add_subdirectory( googlemock )
elseif(BUILD_GTEST)
add_subdirectory( googletest )
endif()
add_subdirectory(basic_tests)
CMakeLists.txt for basic_tests
include_directories($(gtest_SOURCE_DIR}/include
${getest_SOURCE_DIR}))
#include_directories(../../src/)
include_directories(../../include/)
add_executable(runBasicCli
basic_check.cpp)
target_link_libraries(runBasicCli gtest gtest_main)
#target_link_libraries(cli)
I'm assuming your compiler is complaining it can't find the B_RocChoices.h header? Your question seems to imply the compiler error is about not finding a function, but B_RocChoices is a header and not a function in your basic_tests.cpp file.
Assuming your problem is that the compiler isn't finding the B_RocChoices.h header, I expect that when you include_directories(../../include) you are wanting to make the directory where B_RocChoices.h resides part of the header search path. This is a relative path, so it depends where the compiler is being run from as to what path it means (it wouldn't work if you were doing out of source builds, for example). Try using either CMAKE_SOURCE_DIR or CMAKE_CURRENT_SOURCE_DIR to define the path unambiguously. For example:
include_directories(${CMAKE_SOURCE_DIR}/include)
If you are using CMake 2.8.11 or later, I'd recommend you consider using target_include_directories() instead and probably also read up on target_link_libraries(). Together, these allow you to make the header search paths and linked libraries specific to a target rather than global to all targets. Lastly, if you prefer to download GoogleTest as part of your build rather than embedding it directly in your project sources, you may find the following link useful:
https://crascit.com/2015/07/25/cmake-gtest/

Can I make a library from multiple targets?

I'm trying to learn cmake and have started converting an old make project over to cmake. Here is a simplified version of the directory structure I now have:
CMakeLists.txt
src/
CMakeLists.txt
main.cpp
core/
CMakeLists.txt
/sourcecode, other cmakes, etc.
test/
CMakeLists.txt
someTest.cpp
Currently, in my root CMakeLists.txt file I simply have this:
cmake_minimum_required(VERSION 2.8)
project(all)
add_subdirectory(src)
add_subdirectory(test)
What I wanted to do, was have a library created by core/CMakeLists.txt that can be used by both src/CMakeLists.txt to build the main executable, but also loaded by test/CMakeLists to build the unit tests.
So my src/core/CMakeLists.txt file currently looks sort of like this:
cmake_minimum_required(VERSION 2.8)
project(core)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wpedantic -Wreorder -DBOOST_TEST_DYN_LINK -DBOOST_LOG_DYN_LINK ")
#some other directories in my core code:
add_subdirectory(display)
add_subdirectory(training)
add_subdirectory(utility)
#some packages I use...
find_package(Boost 1.55.0
COMPONENTS
log
program_options
serialization
thread
system
filesystem
REQUIRED)
find_package(GLUT REQUIRED)
find_package(OpenGL REQUIRED)
find_package(Eigen3 REQUIRED)
include_directories(
${PROJECT_SOURCE_DIR}
${EIGEN3_INCLUDE_DIR})
target_link_libraries(core
display
training
utility
${Boost_LIBRARIES}
${OPENGL_LIBRARIES}
${GLUT_LIBRARY}
${OpenMP_LIBRARIES})
So the idea is that I now have a core target I can simply link against to run my tests, and everything should work. However, when I try to build main, for example, I get:
Cannot specify link libraries for target "core" which is not built by this
project.
I thought this might be because core doesn't have a add_library command, but if I add add_library(core) I get this error:
You have called ADD_LIBRARY for library core without any source files. This typically indicates a problem with your CMakeLists.txt file
But I don't want to add any source files; I just want this target to link the targets in the core directory and produce a target I can link against from test.
Clearly I'm missing some core knowledge here, either with cmake or the toolchain itself. Help is appreciated :)
If you only want to create a core target without source files, you need to declare it like an INTERFACE target. So, try to add the following code to your src/core/CMakeLists.txt:
cmake_minimum_required(VERSION 3.0) # REQUIRED 3.x.x version
project(core)
...
# Here declare your core_interface target
add_library(core_interface INTERFACE)
target_link_libraries(core_interface INTERFACE
display
training
utility
${Boost_LIBRARIES}
${OPENGL_LIBRARIES}
${GLUT_LIBRARY}
${OpenMP_LIBRARIES})
As you can see, if you make this, you'll need to upgrade your CMake installed version.
Then, you'll build your tests or any executable, linking with this interface target directly:
add_executable(main main.cpp)
target_link_libraries(main core_interface)

CMake with Google Protocol Buffers

I'm trying to use cmake to build my little project using protocol buffers.
There's a root directory with a number of subdirectories with a number of libraries and executables. My first thought was to have my .proto-files in a subdirectory, but when I read this answer I made a library out of it instead. But when I try to include a messages header in my executable it can't find it.
Error message:
fatal error: msgs.pb.h: No such file or directory
#include "msgs.pb.h"
^
compilation terminated.
I'm running it by creating a dir "build" and then "cmake .. && make" from inside it.
I've looked and it seems the generated files get put in build/messages, so I could do include_directories(build/messages) but that doesn't seem...proper. Is there a proper way of doing this with protobuf? The reason I want the messages file in their own folder is they they'll be used in a lot of different small executables.
Any other general tips for improvements to my CMake-structure is also appreciated :)
Directories:
root
messages
core
server
root/CMakeLists.txt:
project(lillebror)
cmake_minimum_required(VERSION 2.8)
cmake_policy(SET CMP0015 NEW)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost COMPONENTS date_time log thread system)
find_package(Protobuf REQUIRED)
if(Boost_FOUND)
add_definitions(-std=c++11)
add_subdirectory(messages)
add_subdirectory(core)
add_subdirectory(server)
add_subdirectory(testserver)
endif()
messages/CMakeLists.txt:
file(GLOB ProtoFiles "${CMAKE_CURRENT_SOURCE_DIR}/*.proto")
PROTOBUF_GENERATE_CPP(ProtoSources ProtoHeaders ${ProtoFiles})
add_library(messages STATIC ${ProtoSources} ${ProtoHeaders})
target_link_libraries(messages ${Boost_LIBRARIES} ${PROTOBUF_LIBRARY})
core/CMakeLists.txt:
aux_source_directory(src SRC_LIST)
add_library(core STATIC ${SRC_LIST})
target_link_libraries(core messages ${Boost_LIBRARIES})
server/CMakeLists.txt:
aux_source_directory(src SRC_LIST)
include_directories(../messages) <---- I thought this would sove my problem
include_directories(../core/src)
link_directories(../core/build)
add_executable(server ${SRC_LIST})
target_link_libraries(server core ${Boost_LIBRARIES})
server/main.cpp:
#include "msgs.pb.h"
int main()
{
return 0;
}
I think the problem here is that the PROTOBUF_GENERATE_CPP function sets up the .pb.h and .pb.cc files to exist in the build tree, not in the source tree.
This is good practice (not polluting the source tree), but it means that your call include_directories(../messages) is adding the wrong value to the search paths. This is adding the source directory "root/messages", whereas you want "[build root]/messages".
You could probably just replace that line with:
include_directories(${CMAKE_BINARY_DIR}/messages)
However, a more robust, maintainable way might be to set the required include path inside the messages/CMakeLists.txt. To expose this value to the parent scope, this would need to either use set(... PARENT_SCOPE) or:
set(ProtobufIncludePath ${CMAKE_CURRENT_BINARY_DIR}
CACHE INTERNAL "Path to generated protobuf files.")
Then in the top-level CMakeLists.txt, you can do:
include_directories(${ProtobufIncludePath})
If your messages library itself needs to #include the generated protobuf files (this would be normal), then it too should have a similar include_directories call.
Having said all that, if you can specify CMake v2.8.12 as the minimum, you can use the target_include_directories command instead.
In messages/CMakeLists.txt after the add_library call, you'd simply do:
target_include_directories(messages PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
Then any other target which depends on messages automatically has the appropriate "messages" include dirs added to its own - you don't need to explicitly call include_directories at all.