Search code examples
c++cmakevisual-studio-2022target

Visual Studio 2022 not showing any CMake Targets


I was having this issue with a much larger project but I reproduced it with a quickly throw together example. Visual Studio correctly generates everything using CMake, and if I then go to the toolbar and select Build -> Build All it will successfully compile and link everything, and I can run the resulting executables from powershell... The Output from CMake is shown to be this:

enter image description here

According to the Visual Studio CMake documentation I should see an additional line stating: Target info extraction done.. I believe that is the core of my issue, but I am unsure why it is not occurring as everything else seems to work fine.

If I try to run (or debug) anything from Visual Studio itself, it simply says I need to select a "Startup Item", but none are available:

enter image description here

Looking at Visual Studio CMake documentation here, I should be able to simply go to the CMake Targets View in the solution explorer, and see all of my targets, but I do not see any:

enter image description here

While I should see (in this very simply example) a target for hello, given I have the following CMake structure:

Root CMakeLsits.txt file:

cmake_minimum_required(VERSION 3.9)
project(vira LANGUAGES CXX VERSION 0.9)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) 
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)

add_subdirectory(src)

src/CMakeLists.txt:

add_executable(hello hello.cpp)

src/hello.cpp:

#include <iostream>

int main() 
{
    std::cout << "hello world!\n";
    return 0;
}

I have been driven crazy by this as the people I've asked about it have simply reiterated what the Visual Studio documentation says, yet neither for this simple project, nor the larger actual project I've been working on, do any CMake targets ever show up. Meaning I'm unable to use any of the debugging features of Visual Studio, since I cannot select a startup item.

Am I doing anything incorrectly? Visual Studio clearly at least recognizes the CMakeLists.txt as it automatically generates everything, and will Build the project if I manually go to Build -> Build All. That has me even more confused as to why I'm experiencing this.

I have attempted even reinstalling Visual Studio from scratch, yet nothing has helped.


Solution

  • After reading through all of my CMakeLists.txt I realized that the issue was that I was including the line set(CMAKE_BUILD_TYPE Release). I had originally included that when I was first porting my code from Linux to Windows, and was trying to force Visual Studio to build a release version (as I was unsure how Viusual Studio worked at first). I had mistakenly left it in, and so even though Visual Studio was configured to compile a Debug version, the CMake configuration wouldn't export any targets with the CMAKE_BUILD_TYPE set to RELEASE (which, it shouldn't have been anyways).

    Removing that line has resolved all of my issues. I have seen this sort of a question pop-up quite a few times on the internet and I'm wondering if perhaps this was the issue. I recognize now that this was a poor question, due to my own misunderstanding of Visual Studio, but I will leave it up with this answer in hopes that it will help others who may just be starting out with Visual Studio and may run into a similar issue.