"TutorialConfig.h: No such file or directory"

I’m currently doing the Cmake tutorial and I’m stuck at Step1 Exercise 3 but I think I have configured the CMakeLists.txt correctly and do the “cmake …build .”. The “TutorialConfig.h” file is inside the Step1_build directory, and still the error is not locating the “TutorialConfig.h”. Here is my CMakeLists.txt code:

# TODO 1: Set the minimum required version of CMake to be 3.10
cmake_minimum_required(VERSION 3.10)

# TODO 2: Create a project named Tutorial
project(Tutorial VERSION 1.0)

# TODO 7: Set the project version number as 1.0 in the above project command

# TODO 6: Set the variable CMAKE_CXX_STANDARD to 11
#         and the variable CMAKE_CXX_STANDARD_REQUIRED to True
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# TODO 8: Use configure_file to configure and copy TutorialConfig.h.in to
#         TutorialConfig.h
configure_file(TutorialConfig.h.in TutorialConfig.h)

# TODO 3: Add an executable called Tutorial to the project
# Hint: Be sure to specify the source file as tutorial.cxx
add_executable(Tutorial tutorial.cxx)

# TODO 9: Use target_include_directories to include ${PROJECT_BINARY_DIR}
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")

tutorial.cxx code:

// A simple program that computes the square root of a number
#include <cmath>
 // TODO 5: Remove this line
#include <iostream>
#include <string>
#include "TutorialConfig.h"
// TODO 11: Include TutorialConfig.h


int main(int argc, char* argv[])
{
  if (argc < 2) {
    // TODO 12: Create a print statement using Tutorial_VERSION_MAJOR
    //          and Tutorial_VERSION_MINOR
    std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "."
              << Tutorial_VERSION_MINOR << std::endl;
    std::cout << "Usage: " << argv[0] << " number" << std::endl;
    return 1;
  }

  // convert input to double
  // TODO 4: Replace atof(argv[1]) with std::stod(argv[1])
  const double inputValue = std::stod(argv[1]);

  // calculate square root
  const double outputValue = sqrt(inputValue);
  std::cout << "The square root of " << inputValue << " is " << outputValue
            << std::endl;
  return 0;
}

error:

Starting build...
/usr/bin/aarch64-linux-gnu-g++ -fdiagnostics-color=always -g /home/ubuntu/Documents/cmake-3.27.0-rc5-tutorial-source/Step1/tutorial.cxx -o /home/ubuntu/Documents/cmake-3.27.0-rc5-tutorial-source/Step1/tutorial
/home/ubuntu/Documents/cmake-3.27.0-rc5-tutorial-source/Step1/tutorial.cxx:6:10: fatal error: TutorialConfig.h: No such file or directory
    6 | #include "TutorialConfig.h"
      |          ^~~~~~~~~~~~~~~~~~
compilation terminated.

Build finished with error(s).

 *  The terminal process terminated with exit code: -1. 
 *  Terminal will be reused by tasks, press any key to close it. 

That line:

configure_file(TutorialConfig.h.in TutorialConfig.h)

means that there a file TutorialConfig.h.in (a kind of a “template”) in the project folder, which CMake will “configure” into a final TutorialConfig.h. You said that “TutorialConfig.h file is inside the Step1_build directory”, so probably for some reason ${PROJECT_BINARY_DIR} points to a different path, because this line:

target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")

says that this is where headers live, and if ${PROJECT_BINARY_DIR} path was the same as your “Step1_build directory”, then it should have worked, so apparently it isn’t.

It could be also that the tutorial you are using (you haven’t provided a link to it) isn’t correct, because I’d expect header configuration to look like this:

configure_file(TutorialConfig.h.in "${PROJECT_BINARY_DIR}/TutorialConfig.h")

and as a matter of fact, this project does (almost) exactly that.

1 Like

But the tricky part is when I type the “#include <TutorialConfig.h>” manually it can detect by the intellisense (I’m using VS Code).

Where should I put the Step1_build in my directory? so I have this directory structure:

├── CMAKE-3.27.0-RCSTUTORIAL-SOURCE
├── Step1
├── Step2
├── Step3
├── Step4
└── Step5

Should I put the Step1_build in this:

├── CMAKE-3.27.0-RCSTUTORIAL-SOURCE
└── Step1\
   ├── Step1_build
   ├── CMakeLists.txt
   ├── tutorial.cxx
   ├── TutorialConfig.h.in
├── Step2
├── Step3
├── Step4
└── Step5

Or like this (this is my directory structure right now):

├── CMAKE-3.27.0-RCSTUTORIAL-SOURCE
├── Step1
├── Step1_build
├── Step2
├── Step3
├── Step4
└── Step5

The part about IntelliSense I didn’t get, but that doesn’t matter that much anyway.

If every Step* folder is a CMake project, then I would put build folders as subfolders of those, like in your first option. You also didn’t show how exactly you are running CMake configuration and build: from which folder and with what parameters.

I also see that now you did <TutorialConfig.h>, while in the first post it is "TutorialConfig.h". This can also make a difference.

1 Like

Hey, thank you for the patient. I think I figure it out where the root of the problem. I do the cmake and build in the terminal using cmake ../ and make , but I’m using vs code which uses ninja as build. When I try to run the executable file in the terminal, it execute without error, but when I run the executable by clicking the run button in vc code IDE it errors saying no such file. I think the problem is in the IDE or C/C++ and CMake Tools extension.

1 Like