Hello, 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).
For example, if my entire main() function is
int main(int, char**)
{
auto number{9};
return 0;
}
````
I get the following 3 errors when attempting a build:
-declaration of variable 'number' with deduced type 'auto' requires an initializer
-expected ';' at end of declaration
-'auto' type specifier is a C++11 extension [-Wc++11-extensions]
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.
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!
### Expected:
1. Build trivial c++11 program
2. Target builds
### Apparent Behavior:
CMake Tools not recognizing c++11 or higher features with Clang 10.0.0
### CMake Tools Log
[build] Starting build
[proc] Executing command: /Applications/CMake.app/Contents/bin/cmake --build "/Users/jonzacks/Documents/Visual Studio Code/JRPG/build" --config Debug --target all -- -j 6
[build] [ 50%] Building CXX object CMakeFiles/JRPG.dir/main.cpp.o
[build] /Users/jonzacks/Documents/Visual Studio Code/JRPG/main.cpp:5:5: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
[build] auto number{9};
[build] ^
[build] /Users/jonzacks/Documents/Visual Studio Code/JRPG/main.cpp:5:10: error: declaration of variable 'number' with deduced type 'auto' requires an initializer
[build] auto number{9};
[build] ^
[build] /Users/jonzacks/Documents/Visual Studio Code/JRPG/main.cpp:5:16: error: expected ';' at end of declaration
[build] auto number{9};
[build] ^
[build] ;
[build] 1 warning and 2 errors generated.
[build] make[2]: *** [CMakeFiles/JRPG.dir/main.cpp.o] Error 1
[build] make[1]: *** [CMakeFiles/JRPG.dir/all] Error 2
[build] make: *** [all] Error 2
[build] Build finished with exit code 2
2019-12-29T17:54:39.999Z [debug] [kit] Reading kits file /Users/jonzacks/.local/share/CMakeTools/cmake-tools-kits.json
2019-12-29T17:54:39.999Z [debug] [kit] Not reading non-existent kits file: /Users/jonzacks/Documents/Visual Studio Code/JRPG/.vscode/cmake-kits.json
2019-12-29T17:54:40.037Z [info] [kit] Successfully loaded 1 kits from /Users/jonzacks/.local/share/CMakeTools/cmake-tools-kits.json
2019-12-29T17:54:40.043Z [debug] [main] Safe constructing new CMakeTools instance
2019-12-29T17:54:40.044Z [debug] [variant] Constructing VariantManager
2019-12-29T17:54:40.046Z [debug] [main] Constructing new CMakeTools instance
2019-12-29T17:54:40.047Z [debug] [main] Starting CMakeTools second-phase init
2019-12-29T17:54:40.094Z [info] [variant] Loaded new set of variants
2019-12-29T17:54:40.104Z [debug] [main] CMakeTools instance initialization complete.
2019-12-29T17:54:40.104Z [debug] [main] Injecting new Kit into CMake driver
2019-12-29T17:54:40.115Z [debug] [kit] Reading kits file /Users/jonzacks/.local/share/CMakeTools/cmake-tools-kits.json
2019-12-29T17:54:40.142Z [info] [kit] Successfully loaded 1 kits from /Users/jonzacks/.local/share/CMakeTools/cmake-tools-kits.json
2019-12-29T17:54:40.151Z [debug] [kit] Not reading non-existent kits file: /Users/jonzacks/Documents/Visual Studio Code/JRPG/.vscode/cmake-kits.json
2019-12-29T17:54:40.153Z [debug] [main] Injecting new Kit into CMake driver
2019-12-29T17:54:49.168Z [debug] [extension] Configuring workspace on open file:///Users/jonzacks/Documents/Visual%20Studio%20Code/JRPG
2019-12-29T17:54:49.170Z [debug] [main] Run configure
2019-12-29T17:54:49.170Z [debug] [main] Saving open files before configure/build
2019-12-29T17:54:49.238Z [debug] [main] Starting new CMake driver
2019-12-29T17:54:49.238Z [debug] [main] Starting CMake driver
2019-12-29T17:54:49.240Z [debug] [driver] CMakeDriver Kit set to Clang 10.0.0
2019-12-29T17:54:49.241Z [debug] [driver] Trying to detect generator supported by system
2019-12-29T17:54:49.251Z [debug] [driver] Command version test return code ENOENT
2019-12-29T17:54:49.256Z [debug] [driver] Command version test return code ENOENT
2019-12-29T17:54:49.272Z [debug] [driver] Command version test return code 0
2019-12-29T17:54:49.273Z [debug] [driver] Run _refreshExpansions
2019-12-29T17:54:49.273Z [debug] [cms-driver] Run doRefreshExpansions
2019-12-29T17:54:49.274Z [debug] [driver] Run _refreshExpansions cb
2019-12-29T17:54:49.284Z [debug] [cms-client] Started new CMake Server instance with PID 46794
2019-12-29T17:54:50.293Z [debug] [cache] Reading CMake cache file /Users/jonzacks/Documents/Visual Studio Code/JRPG/build/CMakeCache.txt
2019-12-29T17:54:50.297Z [debug] [cache] Parsing CMake cache string
2019-12-29T17:54:50.339Z [debug] [driver] Setting new variant , Disable optimizations - include debug information.
2019-12-29T17:54:50.339Z [debug] [driver] Run _refreshExpansions
2019-12-29T17:54:50.339Z [debug] [cms-driver] Run doRefreshExpansions
2019-12-29T17:54:50.340Z [debug] [driver] Run _refreshExpansions cb
2019-12-29T17:54:50.351Z [debug] [cms-client] Started new CMake Server instance with PID 46795
2019-12-29T17:54:51.358Z [debug] [cache] Reading CMake cache file /Users/jonzacks/Documents/Visual Studio Code/JRPG/build/CMakeCache.txt
2019-12-29T17:54:51.361Z [debug] [cache] Parsing CMake cache string
2019-12-29T17:54:51.437Z [debug] [driver] Start configure
2019-12-29T17:54:51.437Z [debug] [driver] Runnnig pre-configure checks and steps
2019-12-29T17:54:51.439Z [debug] [driver] Using compilers in Clang 10.0.0 for configure
2019-12-29T17:54:51.440Z [debug] [driver] Run _refreshExpansions
2019-12-29T17:54:51.440Z [debug] [cms-driver] Run doRefreshExpansions
2019-12-29T17:54:51.440Z [debug] [driver] Run _refreshExpansions cb
2019-12-29T17:54:51.547Z [info] [cmake] Configuring done
2019-12-29T17:54:51.549Z [info] [cmake] CMake Deprecation Warning:
2019-12-29T17:54:51.549Z [info] [cmake] The 'cmake-server(7)' is deprecated. Please port clients to use the
2019-12-29T17:54:51.549Z [info] [cmake] 'cmake-file-api(7)' instead.
2019-12-29T17:54:51.550Z [info] [cmake]
2019-12-29T17:54:51.550Z [info] [cmake]
2019-12-29T17:54:51.658Z [info] [cmake] Generating done
2019-12-29T17:54:51.786Z [debug] [cache] Reading CMake cache file /Users/jonzacks/Documents/Visual Studio Code/JRPG/build/CMakeCache.txt
2019-12-29T17:54:51.797Z [debug] [cache] Parsing CMake cache string
2019-12-29T17:55:09.451Z [debug] [extension] [8371] cmake.build started
2019-12-29T17:55:09.452Z [debug] [main] Run build
2019-12-29T17:55:09.471Z [debug] [main] Saving open files before configure/build
2019-12-29T17:55:09.526Z [info] [build] Starting build
2019-12-29T17:55:09.541Z [debug] [driver] Start build all
2019-12-29T17:55:09.542Z [debug] [driver] Runnnig pre-configure checks and steps
2019-12-29T17:55:09.544Z [info] [proc] Executing command: /Applications/CMake.app/Contents/bin/cmake --build "/Users/jonzacks/Documents/Visual Studio Code/JRPG/build" --config Debug --target all -- -j 6
2019-12-29T17:55:09.675Z [info] [build] [ 50%] Building CXX object CMakeFiles/JRPG.dir/main.cpp.o
2019-12-29T17:55:10.133Z [error] [build] /Users/jonzacks/Documents/Visual Studio Code/JRPG/main.cpp:5:5: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
2019-12-29T17:55:10.133Z [error] [build] auto number{9};
2019-12-29T17:55:10.133Z [error] [build] ^
2019-12-29T17:55:10.135Z [error] [build] /Users/jonzacks/Documents/Visual Studio Code/JRPG/main.cpp:5:10: error: declaration of variable 'number' with deduced type 'auto' requires an initializer
2019-12-29T17:55:10.136Z [error] [build] auto number{9};
2019-12-29T17:55:10.136Z [error] [build] ^
2019-12-29T17:55:10.136Z [error] [build] /Users/jonzacks/Documents/Visual Studio Code/JRPG/main.cpp:5:16: error: expected ';' at end of declaration
2019-12-29T17:55:10.136Z [error] [build] auto number{9};
2019-12-29T17:55:10.137Z [error] [build] ^
2019-12-29T17:55:10.137Z [error] [build] ;
2019-12-29T17:55:10.138Z [error] [build] 1 warning and 2 errors generated.
2019-12-29T17:55:10.142Z [error] [build] make[2]: * [CMakeFiles/JRPG.dir/main.cpp.o] Error 1
2019-12-29T17:55:10.143Z [error] [build] make[1]: [CMakeFiles/JRPG.dir/all] Error 2
2019-12-29T17:55:10.143Z [error] [build] make: ** [all] Error 2
2019-12-29T17:55:10.177Z [debug] [driver] Run _refreshExpansions
2019-12-29T17:55:10.178Z [debug] [cms-driver] Run doRefreshExpansions
2019-12-29T17:55:10.178Z [debug] [driver] Run _refreshExpansions cb
2019-12-29T17:55:10.189Z [info] [build] Build finished with exit code 2
2019-12-29T17:55:10.196Z [debug] [extension] [8371] cmake.build finished (returned 2)
2019-12-29T17:55:10.204Z [debug] [cache] Reading CMake cache file /Users/jonzacks/Documents/Visual Studio Code/JRPG/build/CMakeCache.txt
2019-12-29T17:55:10.206Z [debug] [cache] Parsing CMake cache string
```
I don't remember what Apple Clang v10.0.0 supports and don't have a machine with the older version of Clang readily available to check. Does clang work with this standard version when you try to compile a file in the terminal?
clang -std=c++17 foo.cpp
IntelliSense is independent from your build system so it can emulate C++17 even if you don't have a C++17 compiler available.
Hi Bob, I figured it out, and as I suspected it turned out to be a simple mistake and fix.
I don't know enough about CMake itself yet to know if this is really a bug, but I assume not.
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 CMake 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)
Glad you were able to figure it out. Thanks for the update!
Most helpful comment
Hi Bob, I figured it out, and as I suspected it turned out to be a simple mistake and fix.
I don't know enough about CMake itself yet to know if this is really a bug, but I assume not.
Originally, my CMakeLists.txt file looked like this:
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 CMake documentation. Thanks!