我是Cmake的新手(尽管在这里也不重要,但对于C ++也是新的),并且使用CMAKE在Visual Studio中遇到了问题。
我创建了一个目录,假设它称为 project
,并放入一个简单的项目,具有以下结构:
Project/
build/ <empty>
src/
main.cpp
CMakeLists.txt
CMakePresets.json
内部这些文件只是最基本的默认代码:
-
cmakelists.txt < cmakelists.txt < /代码>:
cmake_minimum_required(版本3.8)
项目(项目)
SET(CMAKE_CXX_STANDARD 20)
set(cmake_cxx_standard_required true)
add_executable(project src/main.cpp)
cmakepresets.json
(此代码只是生成的默认值):
{
“版本”:3,
“ configurePresets”:[
{
“名称”:“ Windows-Base”,
“隐藏”:是的,
“发电机”:“忍者”,
“ binarydir”:“ $ {sourcedir}/out/build/$ {prestname}”,
“ installdir”:“ $ {sourcedir}/out/install/install/$ {prestname}”,
“ cachevariables”:{
“ cmake_c_compiler”:“ cl.exe”,
“ cmake_cxx_compiler”:“ cl.exe”
},,
“健康)状况”: {
“类型”:“等于”,
“ lhs”:“ $ {hostsystemname}”,
“ RHS”:“ Windows”
}
},,
{
“名称”:“ x64-debug”,
“ displayname”:“ x64调试”,
“继承”:“ Windows-Base”,
“建筑学”: {
“ value”:“ x64”,
“策略”:“外部”
},,
“ cachevariables”:{
“ cmake_build_type”:“调试”
}
},,
{
“名称”:“ x64释放”,
“ displayName”:“ x64版本”,
“继承”:“ x64-debug”,
“ cachevariables”:{
“ cmake_build_type”:“释放”
}
},,
{
“名称”:“ x86-debug”,
“ displayname”:“ x86调试”,
“继承”:“ Windows-Base”,
“建筑学”: {
“ value”:“ x86”,
“策略”:“外部”
},,
“ cachevariables”:{
“ cmake_build_type”:“调试”
}
},,
{
“名称”:“ x86释放”,
“ displayName”:“ x86版本”,
“继承”:“ x86-debug”,
“ cachevariables”:{
“ cmake_build_type”:“释放”
}
}
这是给出的
}
src/main.cpp
:
#include&lt; iostream&gt;
int main(){
std :: cout&lt;&lt; “你好世界!” &lt;&lt; std :: endl;
返回0;
}
然后,我使用Cmake创建了一个Visual Studio解决方案:
C:\...\Project\build> cmake ..
这可以正常工作而没有任何错误,Visual Studio可以打开解决方案。它也可以正确构建项目...
但是它无法运行其构建的可执行文件。成功构建项目后,它将可执行文件写入 c:\ ... \ project \ build \ debug \ debug \ project.exe
,但它试图打开 c:\ ... \ project \ build \ x64 \ debug \ all_build
而不是错误弹出。
我认为这里有两件事是错误的:
- 可执行文件应写入
c:\ ... \ project \ build \ x64 \ debug
文件夹中,而不仅仅是 c:\ ... \ project \ build \ debug
文件夹。每当我以前使用Visual Studio时,这就是它的工作方式,这是它正在尝试搜索的文件夹。
- 它应该搜索可执行的可执行文件
project.exe.exe
,而不是一个称为 all_build
。
当我从命令行手动运行 project.exe
时,它可以正常工作。但是我似乎无法使Visual Studio正确运行。
我在这里做错了什么,我该如何工作?
I'm very new to CMake (and new to C++ too, although that shouldn't matter here), and I am having a problem using CMake with Visual studio.
I have created a directory, let's say it's called Project
, and put in it a simple project with the following structure:
Project/
build/ <empty>
src/
main.cpp
CMakeLists.txt
CMakePresets.json
Inside these files is just the most basic, default code:
CMakeLists.txt
:
cmake_minimum_required (VERSION 3.8)
project (Project)
set (CMAKE_CXX_STANDARD 20)
set (CMAKE_CXX_STANDARD_REQUIRED True)
add_executable (Project src/main.cpp)
CMakePresets.json
(this code is just the default that was generated):
{
"version": 3,
"configurePresets": [
{
"name": "windows-base",
"hidden": true,
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Windows"
}
},
{
"name": "x64-debug",
"displayName": "x64 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x64-release",
"displayName": "x64 Release",
"inherits": "x64-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "x86-debug",
"displayName": "x86 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x86",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x86-release",
"displayName": "x86 Release",
"inherits": "x86-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
}
]
}
src/main.cpp
:
#include <iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}
Then, I have used CMake to create a Visual Studio solution:
C:\...\Project\build> cmake ..
This has worked fine without any errors, and Visual Studio can open the solution. It can also build the project correctly...
But it cannot run the executable which it has built. After successfully building the project, it has written the executable to C:\...\Project\build\Debug\Project.exe
, but it tries to open C:\...\Project\build\x64\Debug\ALL_BUILD
instead, and I get an error popup.
I gather that there are two things wrong here:
- The executable file should be written within the
C:\...\Project\build\x64\Debug
folder, not just the C:\...\Project\build\Debug
folder. This is how it has worked whenever I have used Visual Studio before, and this is the folder it is trying to search in.
- It should be searching for an executable called
Project.exe
, not one called ALL_BUILD
.
When I run Project.exe
manually from the command line, it works fine. But I cannot seem to make Visual Studio run it correctly.
What have I done wrong here, and how can I get this to work?
发布评论
评论(2)
默认项目设置为
all_build
以更改VS Generator的默认值,请使用以下cmake语句:add_executable
命令之后。Default project is set to
ALL_BUILD
to change the default for the VS generators use the following CMake statement:Anywhere after the
add_executable
command.您在解决方案资源管理器中看到了几个项目。这些项目是构建目标。默认目标是
all_build
,它构建所有配置的目标,例如cmake -build。
做到了。将所需的目标设置为解决方案资源管理器中的启动项目。这将指出调试器可执行的操作。
You see several projects in the solution explorer. These projects are build targets. The default target is
ALL_BUILD
, that builds all configured targets, likecmake --build .
does it.Set required target as the startup project in the solution explorer. This will point the debugger what executable to run.