VSCode's variable substitution doesn't seen to work in CMakeLists.txt (Absolute paths works)

Hey!
I am a beginner learning how to use CMake in my VSCode C++ projects and I ran into some issues trying to figure out how to get CMake to include headers. Mainly, ${workspaceFolder} doesn’t seem to be substituting the correct path to my CMakeLists.txt file.

However if instead I used my full path D:\\Code\\Projects\\TestProject\\include instead of ${workspaceFolder}\\include, my code compiles and everything is fine.

Is there anything I did wrongly?

Here is my project file structure:

TestProject
|.vscode
|build
||autogenerated stuff here
|include
||headers
|||myPrint.h
|||printME.h
|src
||functionality_test.cpp
||myPrint.cpp
||printME.cpp
|CMakeLists.txt

CMakeLists.txt:

cmake_minimum_required(VERSION 3.0.0)
project(functionality_test VERSION 0.1.0)

include(CTest)
enable_testing()

add_executable(functionality_test 
src/functionality_test.cpp
src/myPrint.cpp
src/printME.cpp
)

#target_include_directories(functionality_test PUBLIC D:\\Code\\Projects\\TestProject\\include)
#The above allows me to compile without errors. The line below spits errors
target_include_directories(functionality_test PUBLIC "${workspaceFolder}\\include")

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

functionality_test.cpp:

#include <iostream>
#include <fstream>
#include "headers/myPrint.h" <--- Error squiggles here: (cannot open source file "headers/myPrint.h" C/C++(1696)) & (headers/myPrint.h: No such file or directory)
#include "headers/printME.h" <--- Error squiggles here: (cannot open source file "headers/printME.h")
int main()
{
    ...
    ...
    ...

    printME();

    return 0;
}

myPrint.cpp:

#include <string>
#include <iostream>
void myPrint(const std::string &s){
    std::cout << s << std::endl;
}

printME.cpp:

#include "headers/myPrint.h" <---(cannot open source file "headers/myPrint.h" C/C++(1696)) & (headers/myPrint.h: No such file or directory)


void printME(){
    myPrint("ME");
}

myPrint.h:

#pragma once
#include <string>
void myPrint(const std::string &s);

printME.h:

#pragma once
void printME();

CMake doesn’t understand VSCode variables, so you’ll have to have VSCode pass it to CMake in some way. However, you should instead use ${CMAKE_CURRENT_SOURCE_DIR} as that is (I believe) the CMake variable that does what you’re looking for here.

1 Like

Ah I see, I wrongly assumed that CMake understood VSCode variables. Thanks so much for the swift reply!