问题描述
我一般来说都是CMake的新手,所以希望我做错了一些简单的事情,但是我想尽办法找到解决办法.我什至无法使用CMake工具来编译具有c ++ 11(或更高功能)的普通程序.理想情况下,我想启用c ++ 17.
I'm new to CMake in general so hopefully there is something simple I'm doing wrong but I'm pulling my hair out trying to find the solution. I can't get CMake tools to compile even a trivial program with c++11 (or higher features). Ideally I'd like to enable c++17.
例如,如果我的整个main()函数是
For example, if my entire main() function is
int main(int, char**)
{
auto number{9};
return 0;
}
尝试构建时出现以下2条错误和1条警告:
I get the following 2 errors and 1 warning when attempting a build:
error: declaration of variable 'number' with deduced type 'auto' requires an initializer
error: expected ';' at end of declaration
warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
我的VSCode C ++设置设置为C ++ 17标准,并且编写代码时,我尝试 build 使用CMake工具扩展程序.
My VSCode C++ settings are set to the C++17 standard, and I don't get any Intellisense errors or warnings when writing the code, just when I try to build using the CMake Tools extension.
我还尝试将以下内容添加到我的CMakeLists.txt文件中:
I've also tried adding the following to my CMakeLists.txt file:
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
我想念什么?非常感谢!
What am I missing? Thanks very much!
平台和版本
操作系统:macOS High Sierra 10.13.6
Operating System: macOS High Sierra 10.13.6
CMake版本:3.16.2
CMake Version: 3.16.2
VSCode版本:1.41.1
VSCode Version: 1.41.1
CMake工具扩展版本:1.2.3
CMake Tools Extension Version: 1.2.3
编译器/工具链:Clang 10.0.0
Compiler/Toolchain: Clang 10.0.0
推荐答案
弄清楚了,我怀疑这是一个简单的错误和修复.
Figured it out, and as I suspected it turned out to be a simple mistake and fix.
最初,我的CMakeLists.txt文件如下所示:
Originally, my CMakeLists.txt file looked like this:
cmake_minimum_required(VERSION 3.0.0)
project(JRPG VERSION 0.1.0)
include(CTest)
enable_testing()
add_executable(JRPG main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
当我将底部的三行向上移动到项目名称和版本行(见下文)正下方时( ),构建工作按预期进行.我尚不清楚确切的原因,但我相信我会更深入地研究文档,以便尽快找出解决方案.谢谢!
When I moved those bottom three lines up directly underneath the project name and version line (see below), the build worked as expected. I don't yet know exactly why that worked but I'm sure I'll figure it out soon enough as I dive deeper into the documentation. Thanks!
cmake_minimum_required(VERSION 3.0.0)
project(JRPG VERSION 0.1.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
include(CTest)
enable_testing()
add_executable(JRPG main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
这篇关于如何获得CMake工具以使用C ++ 11(或更高版本)功能在Visual Studio Code中编译程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!