Issue
When I put only a CMakeLists.txt inside a subfolder, it does not work. But, when I add another CMakeLists.txt on the root folder with add_subdirectory
command, it works. Why does it need a CMakeLists.txt on the root folder? Can I have only one CMakeLists.txt inside a subfolder which works?
The system that I used:
- OS: Windows 10
- VS Code Version: VS Code 1.83.1
- CMake version: 3.27.6
- CMake Tools (vs code extension): 1.15.31
This is the structure of my C++ project
my_project/
-- my_project/
---- CMakeLists.txt
---- src/
------ main.cpp
This is my main.cpp
// main.cpp
#include <iostream>
int main(){
std::cout << "Bismillah hello world\n" << std::endl;
return 0;
}
And this is my CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(my_project)
add_executable(my_project_main src/main.cpp)
When I pressed Ctrl + Shift + P and then choose CMake: Configure, it asked me for the location of the CMakeLists.txt. I chose the file above but after that it showed
"Configure failed. Would you like to attempt to configure with the CMake Debugger?"
I press the button "Debug" but it still showed me the same message.
However, when I make another CMakeLists.txt on the root folder like this:
cmake_minimum_required(VERSION 3.10)
project(root_of_my_project)
add_subdirectory(my_project)
It worked. Why is this?
Solution
There was a historically related-sounding issue ticket: "Locating" a CMakeLists.txt file does not fully activate cmake-tools #2066. It's purportedly fixed, but apparently (according to the asker) the workaround stated in the question post solves the problem stated in the question post here: Close and reopen the workspace / close and reopen VS Code.
Answered By - starball Answer Checked By - Senaida (WPSolving Volunteer)