Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a header-only library with cmake?

How to make a project in cmake that collects all c++ files into one header?

I have this project structure.

/
  project/
     folder1/
         file.cpp
         file.hpp
     folder2/
         ...etc
     CMakeLists.txt
  tests/
     test.cpp
     CMakeLists.txt
CMakeList.txt

root cmakelists.txt

cmake_minimum_required (VERSION 3.8)

project ("CMakeProject"
    LANGUAGES C CXX)

set(CMAKE_EXECUTABLE_SUFFIX ".exe")

include(GNUInstallDirs)

add_subdirectory ("project")


option(ENABLE_TESTING OFF)

if (ENABLE_TESTING)
    enable_testing()
    add_subdirectory("tests")
endif()

CMakeLists.txt in project

cmake_minimum_required (VERSION 3.8)

file(GLOB projectSRC
    "*/*.cpp"
    "*/*.hpp"
    "*.cpp"
    "*.hpp"
)

add_library(project INTERFACE)

message(STATUS "CMake inatall directory: " ${CMAKE_INSTALL_INCLUDEDIR})
target_include_directories(project 
    INTERFACE 
        $<BUILD_INTERFACE:${PROJECT_INCLUDE_DIR}>
        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

and test cmakelist.txt

cmake_minimum_required (VERSION 3.8)

# install Catch2 testing library
# (https://github.com/catchorg/Catch2/blob/master/docs/cmake-integration.md#installing-catch2-from-git-repository or use packet manager)
find_package(Catch2 REQUIRED)

file(GLOB testSRC
    "*.cpp"
)

add_executable(tests ${testSRC})

target_link_libraries(tests
    Catch2::Catch2
    project)

include(CTest)
include(Catch)
catch_discover_tests(tests)

How to generate one header and use it (in tests or other projects) or make this library able to have templates? The first is better.

like image 781
Wootiae Avatar asked Mar 09 '20 16:03

Wootiae


People also ask

How do I create a header only library in CMake?

Creating a Header-Only CMake Target By specifying INTERFACE as the second parameter to add_library , we are no longer allowed to provide source files since the library is not meant to generate any build output.

Do you include header files in CMake?

With CMake, adding header include directories to your C++ project is as easy as using your head in football! Heading those C++ include directories is easy with CMake. As you are probably aware, you can include other source files in C++ with the #include pre-processor directive.

What is Interface library CMake?

Interface Libraries add_library(<name> INTERFACE) Creates an Interface Library. An INTERFACE library target does not compile sources and does not produce a library artifact on disk. However, it may have properties set on it and it may be installed and exported.

What is the use of CMakeLists txt?

CMakeLists. txt file contains a set of directives and instructions describing the project's source files and targets (executable, library, or both). When you create a new project, CLion generates CMakeLists. txt file automatically and places it in the project root directory.


Video Answer


1 Answers

How to make a header-only library with cmake?

Like this:

add_library(project INTERFACE)
target_include_directories(project INTERFACE .)

Then in the target that uses the library:

target_link_libraries(dependee
    PUBLIC/INTERFACE/PRIVATE # pick one
    project)

and include the header like this:

#include <project/folder1/file.hpp>
like image 108
eerorika Avatar answered Oct 14 '22 02:10

eerorika