I'm trying to follow the official tutorial for CMake
for adding a version number and configured header file. I have two directories:
Step1
Step1_build
Step1
contains CMakeLists.txt
, TutorialConfig.h.in
and tutorial.cxx
. Contents of each file follow:
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
# set the project name
project(Tutorial VERSION 1.0)
# configure a header file to pass the version number to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)
# add directory to list of paths to search for include files
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
)
# add the executable
add_executable(Tutorial tutorial.cxx)
TutorialConfig.h.in
// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
tutorial.cxx
#include "TutorialConfig.h"
#include <iostream>
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cout << argv[0] << "Version" << Tutorial_VERSION_MAJOR << "."
<< Tutorial_VERSION_MINOR << std::endl;
return 1;
}
std::cout << "argc=" << argc << std::endl;
std::cout << "argv[1]=" << argv[1] << std::endl;
}
I then try to build using cmake -S Step1 -B Step1_build
. But I get an error:
CMake Error at CMakeLists.txt:10 (target_include_directories) Cannot specify include directories for target "Tutorial" which is not built by this project
I've checked the instructions again and again, and I'm not sure what I'm doing wrong. Why won't the project compile?
CodePudding user response:
In CMake targets need to be defined before any modification of their properties (like include directories), so this line:
# add the executable
add_executable(Tutorial tutorial.cxx)
should be moved before the call to target_include_directories
.