Using a Find*.cmake file with a header only library - c++

I am creating a header only library (Library A) that includes another library (Library B) using its FindB.cmake file.
I include the FindB.cmake file by having it in a cmake directory and doing:
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
find_package(B REQUIRED)
target_link_libraries(A INTERFACE B::B)
The tests on Library A all work fine so B is included successfully in A. To make B available to projects that want to include A, I have in my AConfig.cmake.in :
include(CMakeFindDependencyMacro)
find_dependency(B REQUIRED)
But when I try to make an application (App C) that uses Library A (with find_package(A REQUIRED)), I get an error that cmake cannot find the FindB.cmake file. The error goes away if I put the FindB.cmake file in the App C project and include it like I did with Library A, but I don't want people using Library A to need the FindB.cmake file.
Is there a way to make all of the location information from FindB.cmake carry over from Library A to App C without needing to include the actual FindB.cmake file?

#Tsyvarev had the answer.
I added this to install the FindB.cmake:
install(FILES "${PROJECT_SOURCE_DIR}/cmake/FindB.cmake"
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/A/cmake
)
Then in my AConfig.cmake.in, I edited it with the following:
set(CMAKE_MODULE_PATH #CMAKE_INSTALL_DATAROOTDIR#/A/cmake #CMAKE_MODULE_PATH#)
find_dependency(B REQUIRED)
I can now add Library A to App C with find_package(A REQUIRED) and everything transfers over.
Thanks again #Tsyvarev

Related

Cmake Top level project has two dependent projects, one child is also dependent on the other child

I have Project A at the top.
Project A requires library B and library C.
Library B also requires library C by itself.
So, in short. How can I link libraries B and C up to A by just linking B?
I have it working now by individually linking all the libraries, but I feel like there is redundancy I could get rid of.
This is part of the CMAKE for Project A at the top:
find_package(libB REQUIRED)
include_directories(${libB_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${libB_LIBRARY})
find_package(libC REQUIRED)
include_directories(${libC_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${libC_LIBRARY})
But also within libB I have this in its CMAKE:
find_package(libC REQUIRED)
include_directories(${libC_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${libC_LIBRARY})
I feel like there is a better way to do this, please let me know. Thank you for any help.
You can use target_include_directories (documentation) to specify include directories for your target instead of specifying them for all targets of the current directory using include_directories. Among other things, this gives the ability to provide visibility specifiers to your includes dirs, which control whether your include dirs will affect only current target (PRIVATE), only those targets which link to your target (INTERFACE) or both (PUBLIC). The same specifiers are used similarly for target_link_libraries.
So, you can use PUBLIC to avoid duplicating your lib C includes and libraries, and since it is the default specifier, you can omit it. Then, parts of your CMakeLists may look like this:
In project A CMakeLists.txt:
find_package(libB REQUIRED)
target_include_directories(${PROJECT_NAME} ${libB_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${libB_LIBRARY})
In lib B CMakeLists.txt:
find_package(libC REQUIRED)
target_include_directories(${PROJECT_NAME} ${libC_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${libC_LIBRARY})
If you, e.g., want only lib B to link to lib C, but leave public 'inheritance' of include directories, you could change the last line of lib B CMakeLists.txt excerpt:
target_link_libraries(${PROJECT_NAME} PRIVATE ${libC_LIBRARY})

CMake: Can we specify include directories for a specific set of files not forming an executable or lib?

If I have this source tree:
C:\app:
src:
CMakeLists.txt
main.cpp --> #include "acme/header_only_lib/api.h"
D:\3rdparty\acme\header_only_lib:
api.h --> #include "detail.h"
detail.h
Without using symlink tricks, and without adding files to the 3rdparty folders, if I must retain #include "acme/header_only_lib/api.h" in main.cpp, how should I specify the include directories in CMake such that api.h can see detail.h, without adding global include directory of D:\3rdparty\acme\header_only_lib? The header-only-lib is not an executable nor library target, and its code is not modifiable by me. I also don't want to pollute my global include directories by adding D:\3rdparty\acme\header_only_lib because the filenames inside there are too common and will easily clash with other libraries/future code.
Is there a CMake mechanism where I can say:
Only for D:\3rdparty\acme\header_only_lib\api.h, add D:\3rdparty\acme\header_only_lib as the include directory?
To add a directory to the global list of include directories, you use e.g.
include_directories( ${CMAKE_SOURCE_DIR}/3rdparty )
Note that you should not hardcode absolute paths (like D:\) into your CMakeLists.txt as that makes it impossible to build your project in any other location. CMake offers variables like ${CMAKE_SOURCE_DIR} and ${CMAKE_BINARY_DIR} for just that purpose.
If you want to add a directory to the list of include directories for a specific part of your build only, you use e.g.
target_include_directories( app PRIVATE ${CMAKE_SOURCE_DIR}/3rdparty )
This adds the directory only for the target (executable / library) app.
Note that, if your acme/header_only_lib is supposed to be installed alongside with the app binaries, this approach won't work, as your acme headers would need to "see" each other on the client's machine, which is unlikely if they reside in the acme/header_only_lib subdirectory but address each other without subdirs. That would require your client (who isn't using your CMakeLists.txt for his builds) to add acme/header_only_lib to the include paths for your header lib to work -- you should not do that.
Use include_directories or target_include_directories:
include_directories("D:\3rdparty")
OR with target_include_directories if you want to make this change only for the main target:
add_executable(main main.cpp)
target_include_directories(main PUBLIC "D:\3rdparty")
Then you can just #include "acme/header_only_lib/api.h" or #include <acme/header_only_lib/api.h>
Header only lib
If you want to do this for header-only-lib only, you need to do this in it's CMake file. First add the library as INTERFACE with no source files:
add_library(header-only INTERFACE)
Then include directories for it:
target_include_directories(header-only INTERFACE include-dir-for-header-only)
Then link it to the main target
find_library(HeaderOnly
NAMES header-only
HINTS "D:\3rdparty\path-to-lib"
)
target_link_libraries(main header-only)

Trying to use Boost with CMake in CLion [duplicate]

I am new to cmake and know this question has been asked before, but still cannot find what I am doing wrong. I have an external library with folders /include and lib. The /include folder contains all the headers (.h) and the /lib folder contains all the source (.c) files.
In my project I have this CMakeList.txt file:
cmake_minimum_required(VERSION 3.7)
project(FirstAttempt)
set(CMAKE_CXX_STANDARD 11)
set (EXTRA_LIBS "D:\\libtrading")
include_directories(${EXTRA_LIBS}/include)
link_directories(${EXTRA_LIBS}/include)
set(SOURCE_FILES main.cpp)
add_executable(FirstAttempt ${SOURCE_FILES})
target_link_libraries (FirstAttempt ${EXTRA_LIBS}/lib)
I know that I have to use target_link_libraries to link the source files of the library to my project, but certainly something is missing, but what? I am still receiving the error undefined reference to xxxxxx.
The library I am trying to include in my project is https://github.com/libtrading/libtrading.
Well, I'll try.
First, seems that you are calling link_directories() on the folder which contains header files, while it should be used in order to specify the path where to search libraries for.
Second, target_link_libraries() takes the absolute path of the shared/static library file as the second argument, while you are passing the directory path (well, it seems so).
target_link_libraries() doesn't link to the "source files of the library", - it links to the compiled shared/static library blob.
And, I would also recommend you to save the name of the executable to the variable so that you wouldn't be able to mistype the target name, like so:
set(TARGET FirstAttempt)
add_executable(${TARGET})
As you know, we need all source files to compile. So we need to point out Cmake know where/what are source files.
I think you should add all sources files like this
file(GLOB SOURCES_FILES "lib/*.c" main.cpp)
to add all .c files.
Or, you can add all lib/*.c files separately
file(SOURCES_FILES_LIBS "lib/*.c")
set(SOURCES_FILES main.cpp)
add_executable(FirstAttempt ${SOURCES_FILES_LIBS} ${SOURCES_FILES})

cmake - undefined reference to

I am new to cmake and know this question has been asked before, but still cannot find what I am doing wrong. I have an external library with folders /include and lib. The /include folder contains all the headers (.h) and the /lib folder contains all the source (.c) files.
In my project I have this CMakeList.txt file:
cmake_minimum_required(VERSION 3.7)
project(FirstAttempt)
set(CMAKE_CXX_STANDARD 11)
set (EXTRA_LIBS "D:\\libtrading")
include_directories(${EXTRA_LIBS}/include)
link_directories(${EXTRA_LIBS}/include)
set(SOURCE_FILES main.cpp)
add_executable(FirstAttempt ${SOURCE_FILES})
target_link_libraries (FirstAttempt ${EXTRA_LIBS}/lib)
I know that I have to use target_link_libraries to link the source files of the library to my project, but certainly something is missing, but what? I am still receiving the error undefined reference to xxxxxx.
The library I am trying to include in my project is https://github.com/libtrading/libtrading.
Well, I'll try.
First, seems that you are calling link_directories() on the folder which contains header files, while it should be used in order to specify the path where to search libraries for.
Second, target_link_libraries() takes the absolute path of the shared/static library file as the second argument, while you are passing the directory path (well, it seems so).
target_link_libraries() doesn't link to the "source files of the library", - it links to the compiled shared/static library blob.
And, I would also recommend you to save the name of the executable to the variable so that you wouldn't be able to mistype the target name, like so:
set(TARGET FirstAttempt)
add_executable(${TARGET})
As you know, we need all source files to compile. So we need to point out Cmake know where/what are source files.
I think you should add all sources files like this
file(GLOB SOURCES_FILES "lib/*.c" main.cpp)
to add all .c files.
Or, you can add all lib/*.c files separately
file(SOURCES_FILES_LIBS "lib/*.c")
set(SOURCES_FILES main.cpp)
add_executable(FirstAttempt ${SOURCES_FILES_LIBS} ${SOURCES_FILES})

CMake - Weird Include Directories

I have created a project which consists of a main.cpp file and another class (named Physical) that is broken up into a Physical.hpp file and Physical.cpp file.
The project file structure looks like this:
main.cpp
header/Physical.hpp
src/Physical.cpp
The project compiles fine, but only if I include Physical.hpp in different ways depending on whether or not I'm including from the Physical.cpp or main.cpp file.
From the main file, I have to use:
#include "header/Physical.hpp"
Whereas, from the Physical.cpp file I have to use:
#include "../header/Physical.hpp"
Why is there this discrepancy? I expect it has something to do with my cmake configuration file, although I'm really not sure. Below is my cmake config file.
cmake_minimum_required(VERSION 2.8.4)
project(gravity_simulator ${PROJECT_BINARY_DIR})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp src/Physical.cpp)
include_directories("${PROJECT_BINARY_DIR}")
add_executable(gravity_simulator ${SOURCE_FILES})
# Detect and add SFML
set(CMAKE_MODULE_PATH "/usr/share/SFML/cmake/Modules/" ${CMAKE_MODULE_PATH})
#Find any version 2.X of SFML
#See the FindSFML.cmake file for additional details and instructions
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(gravity_simulator ${SFML_LIBRARIES})
endif()
Everything works as expected.
For your main.cpp file the header is located at header/Physical.hpp while for your Physical.cpp the header is located at ../header/Physical.hpp.
The mistake here is, that you think the relative path would always start from a specific directly, while instead every source file (*.cpp) gets compiled independently and thus the header search is happening for each file independently.
Personally if I don't write a library, I simply put all my source and header files in a src directory and everything will be fine. However if I'm writing a library, I use the <header.hpp> style and add the header directory to the include directory (include_directories("header")). The whole reason why one usually splits up things into header and source files is, so when one ships a library one can easily just ship the headers and doesn't have manually filter them out between the source files.
tl;dr When writing an application put everything a source directory. When writing a library, use the <> for the inclusion add the header directory to the include directories.