2

I am developing a header-only library that uses C++17 features. The project structure is like this:

 - project
   - README.md
   - LICENSE.md
   - CMakeLists.txt
   - include
     - proj_library.h
   - test
     - unittest
       - CMakeLists.txt
       - main.cpp
       - test.cpp

The unit tests use doctest unit testing framework, which I currently do not include in the project.

How should my main CMakeLists.txt file and the unit test CMakeLists.txt files look like to:

  • make clients use C++17 (or later) language standard as the library uses std::optional and the like
  • enable generating nice IDE project (I am using Visual Studio 2017)
  • enable the equivalent of -Wall -Wextra -pedantic options in as compiler-agnostic way as possible (I plan compilation using at least GCC, Clang, and VS 2017)

So far I have this, which I based on several open-source projects and articles:

Main CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)

project("lib-headeronly")

add_library(${PROJECT_NAME} INTERFACE)

target_include_directories(${PROJECT_NAME} INTERFACE include/)

target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17)

enable_testing()
add_subdirectory(test/unittest)

Unit test CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)

include(CTest)

add_executable(unittest)
target_sources(unittest
    PRIVATE
    main.cpp
    test.cpp)

target_include_directories(unittest
    PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>/include
    $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>)

target_compile_features(unittest PRIVATE cxx_std_17)

add_test(NAME unittest COMMAND unittest)

Currently, while the library is under development, I do not need fancy targets for installation and the like. I just need facilities for building unit tests on several platforms.

Any suggestions?

Krzysiek Karbowiak
  • 1,655
  • 1
  • 9
  • 17
  • You could hide the unittest stuff with something like `if(MY_LIB_ENABLE_UNITTEST)` so that people using your library are not immediately cluttered with your tests. – Moeren Sep 13 '19 at 08:14
  • "Any suggestions?" - Suggestions for **what**? You already have `CMakeLists.txt` which is probably working. Just continue with it. "Modern CMake" ("target-oriented", as far as I understand that phrase) provides good approaches for **specific tasks**. But it doesn't specify "silver bullet" for the *whole project*'s organization. So it don't see how Stack Overflow may help you at the given stage. If you want to ask for suggestions about **improving** the **working code**, then ask on [codereview.se]. – Tsyvarev Sep 13 '19 at 08:32
  • Any suggestions to reach the three goals that I listed. Yes, the current project works in the sense that it generates makefiles and VS projects that I can build, but I have no idea whether I use CMake constructs that are considered modern and good practice. – Krzysiek Karbowiak Sep 13 '19 at 08:42
  • 1
    Your goals (specifying C++ standard, generating VS project and compiler-agnostic way for debug configuration) are quite independent one from another, but on Stack Overflow we want a question to be concentrating on a **specific problem**. There are several SO questions about your 1 and 3 goals. Have you seen them? E.g. [that question](https://stackoverflow.com/questions/45955272/modern-way-to-set-compiler-flags-in-cross-platform-cmake-project) for the first goal and [that question](https://stackoverflow.com/questions/44960715/how-to-enable-stdc17-in-vs2017-with-cmake) for the third one. – Tsyvarev Sep 13 '19 at 09:36
  • Thanks for the links, I will have a look. I thought my problem was specific enough being related to a header-only library. – Krzysiek Karbowiak Sep 13 '19 at 11:03

0 Answers0