Narmondur
Narmondur

Reputation: 121

problems with highly specific cmake + qtCreator use case and unit tests

My troubles with CMake keep compounding D:

What I have in mind is to create my project so that when I build it, it runs unit tests and if unit tests fail, it cleans up after itself. Otherwise, if they succeed, the build spits the runnable executable into the run/ directory.

My project directory contains subdirectories sources, headers, build, run, and tst/ut and thus far I have cmakelists at project root as well as inside sources and headers directories

CMakeLists.txt at the project root


project(TaskFour LANGUAGES CXX)

enable_testing()

set(BIN ${CMAKE_PROJECT_NAME})

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core REQUIRED)



add_library(${BIN}_lib)
add_executable(${BIN}_run)

include_directories(sources headers tst/ut)

cmakelists at sources/


set(SRC
    main.cpp)

target_sources(${BIN} PUBLIC ${SRC})

and headers


#contents of this list are empty by design for now
set(SRC
    "")

target_sources(${BIN} PUBLIC ${SRC})

With these CMake list files, QtCreator fails to see anything beyond the top-level CMake list file. It doesn't acknowledge the existence of the subdirectories in the project directory, the other two cmakeLists.txt files nor the main.cpp that very much exists inside the sources directory.

I have attempted to use the add_subdirectory command and that seemed to help as that made all the subdirectories show up. However, I quickly realized that QtCreator seemingly treated every subdirectory like its own project, and trying to add files into these newfangled "projects" resulted in a simple "failed to add one or more files to project foo" message.

I have understood that the point of CMake list files is to have several per project, each governing their own subsection of the larger project, and the top CMake list file gathering it all together. Before I did my CMake file as one big file at the root of the project, and I've understood that to not be good. I also understand that dividing headers and sources like I do is not exactly the most common practice but I hope you forgive me and help me get this thing working.

So, my actual question is, how do I do this? What am I currently doing wrong in my top-level cmakelists file to cause qtCreator to not find the files in the project? How do I write the CMake lists so that it would allow me to use the QTest framework, maybe even build the skeleton from QtCreator's project wizard (as currently the option to add the autotest project as a subproject is greyed out)?

Any insight is appreciated, and feel free to go into detail about CMake if you feel like it. This is an important piece of software development that I feel I've been missing out on.

Upvotes: 1

Views: 324

Answers (1)

rdowell
rdowell

Reputation: 729

There's a couple problems here with how you've organized your CMake file. This should at least get you started on the right path

  1. You're calling add_library(${BIN}_lib) and add_executable(${BIN}_run) but then in your child CMake file you're calling target_sources(${BIN} ...) - target names are evaluated as strings - there is no target named BIN (or the value you set to BIN in this case), and adding sources to a target named BIN would not propagate those sources to targets named BIN_lib and BIN_run
  2. include_directories is for adding entries to the C++ compiler's include search path, not for adding additional directories containing sources/other CMake files. For that, as you seem to have already figured out, you need add_subdirectories(...)

Upvotes: 1

Related Questions