Visual Studio 2022未显示任何CMake目标

huangapple go评论73阅读模式
英文:

Visual Studio 2022 not showing any CMake Targets

问题

我有一个大型项目出现了这个问题,但我用一个快速拼凑的例子重新现了一下。Visual Studio 使用 CMake 正确生成一切,然后我去工具栏选择 Build -> Build All,它将成功编译和链接一切,我可以从 PowerShell 运行生成的可执行文件... CMake 的输出显示为:

Visual Studio 2022未显示任何CMake目标

根据 Visual Studio CMake 文档 我应该看到一行附加的内容:Target info extraction done.。我认为这是我的问题的核心,但我不确定为什么它没有发生,因为其他一切似乎都正常工作。

如果我尝试从 Visual Studio 本身运行(或调试)任何内容,它只是说我需要选择一个 "Startup Item",但没有可用的项:

Visual Studio 2022未显示任何CMake目标

查看 Visual Studio CMake 文档 这里,我应该能够简单地转到解决方案资源管理器中的 CMake Targets View,并看到所有我的目标,但我没有看到任何目标:

Visual Studio 2022未显示任何CMake目标

虽然我应该在这个非常简单的例子中看到一个名为 hello 的目标,因为我有以下的 CMake 结构:

CMakeLsits.txt 文件:

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;
}

我为此烦恼,因为我问过的人只是重申了 Visual Studio 文档中的内容,然而对于这个简单的项目,也不是我一直在处理的较大的实际项目,任何 CMake 目标都没有出现。这意味着我无法使用 Visual Studio 的任何调试功能,因为我无法选择启动项。

我做错了什么吗?Visual Studio 明显至少认识到 CMakeLists.txt,因为它会自动生成一切,并且如果我手动转到 Build -&gt; Build All,它将构建项目。这让我更加困惑,不明白为什么我会遇到这个问题。

我甚至尝试了从头开始重新安装 Visual Studio,但没有什么帮助。

英文:

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 -&gt; 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:

Visual Studio 2022未显示任何CMake目标

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:

Visual Studio 2022未显示任何CMake目标

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:

Visual Studio 2022未显示任何CMake目标

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 &lt;iostream&gt;

int main() 
{
	std::cout &lt;&lt; &quot;hello world!\n&quot;;
	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 -&gt; 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.

答案1

得分: 2

在阅读了我的所有CMakeLists.txt之后,我意识到问题出在我包含了这行代码set(CMAKE_BUILD_TYPE Release)上。最初,在将我的代码从Linux移植到Windows时,我包含了这行代码,试图强制Visual Studio构建一个发布版本(因为起初我不了解Visual Studio的工作方式)。我错误地将它留在那里,所以即使Visual Studio配置为编译调试版本,CMake配置也不会导出任何具有CMAKE_BUILD_TYPE设置为RELEASE的目标(事实上不应该是这样的)。

移除这行代码解决了我所有的问题。我看到这种问题在互联网上多次出现,我想知道也许这就是问题所在。现在我明白这是一个由于我对Visual Studio的误解而导致的糟糕问题,但我会将它保留,并希望这个答案能帮助那些刚开始使用Visual Studio并可能遇到类似问题的人。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月19日 06:15:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定