-1

I have been struggling to set the language version to C++17 in my CMake project, and I have a suspicion that I might be overlooking something simple. Unfortunately, my CMakeLists.txt file has become quite convoluted in the process. I have been working on this problem for quite some time, and despite my efforts, I haven't been able to achieve the desired version change. As a newcomer to CMake, I must admit that I am not entirely familiar with all the intricacies and best practices.

In my quest to find a solution, I have extensively researched online forums and read numerous posts related to this topic. I have attempted various approaches and combinations of settings, but none of them seem to work. At this point, I am starting to doubt my understanding of CMake and its usage.

I humbly request assistance from the community to help me identify any potential oversights or mistakes in my code. I would greatly appreciate it if someone could review the following CMakeLists.txt file and provide guidance on how to correctly set the C++ version to 17:

Im on 64bit Windows 10 Visual Studio 2019 (community)

CmakeList

cmake_minimum_required (VERSION 3.16)

project ("OpenCv")
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    message("Using GCC compiler")
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    message("Using Clang compiler")
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
    message("Using MSVC compiler")
else ()
    message("Unknown compiler: ${CMAKE_CXX_COMPILER_ID}")
endif ()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

include (CTest)
enable_testing()

find_package(OpenCV REQUIRED)

include_directories(${OpenCV_INCLUDE_DIRS})

# Add source to this project's executable.
add_executable (OpenCv "OpenCv.cpp" "OpenCv.h" "FrameBuffer.cpp" "FrameBuffer.h" "Mats.h")
target_compile_definitions(OpenCv PRIVATE _ALLOW_RTCc_IN_STL)

if(MSVC)
    target_compile_options(OpenCv PRIVATE "/std:c++17")
else()
    target_compile_features(OpenCv PRIVATE cxx_std_17)
endif()

target_link_libraries(OpenCv ${OpenCV_LIBS})

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(cpack)

CmakeSettings

{
  "configurations": [
    {
      "name": "x64-Debug",
      "generator": "Ninja",
      "configurationType": "Debug",
      "inheritEnvironments": [ "msvc_x64_x64" ],
      "buildRoot": "${projectDir}\\out\\build\\${name}",
      "installRoot": "${projectDir}\\out\\install\\${name}",
      "cmakeCommandArgs": "",
      "buildCommandArgs": "-v",
      "ctestCommandArgs": "",
      "variables": [
        {
          "name": "CMAKE_CXX_FLAGS",
          "value": "/DWIN32 /D_WINDOWS /W3 /GR /EHsc /std:c++17",
          "type": "STRING"
        },
        {
          "name": "CMAKE_CXX_FLAGS_DEBUG",
          "value": "/MDd /Zi /Ob0 /Od /RTC1/std:c++17",
          "type": "STRING"
        }
      ]
    }
  ]
}

I added some messages to confirmed that it is running the MSVC and it is, I have tried playing around with alot of suggested combinations of things however I am just hoping to get lucky.

When I dealte the build and let it full build I see this in output

1> [CMake] -- The C compiler identification is MSVC 19.25.28612.0
1> [CMake] -- The CXX compiler identification is MSVC 19.25.28612.0

I have also noticed this witch tells me my addations to settings and list are doing osmthing?

"cmd.exe" /c ""E:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe"  -G "Ninja" -DCMAKE_INSTALL_PREFIX:PATH="X:\Code\OpenCv\OpenCv\out\install\x64-Debug" -DCMAKE_CXX_FLAGS:STRING="/DWIN32 /D_WINDOWS /W3 /GR /EHsc /std:c++17" -DCMAKE_CXX_FLAGS_DEBUG:STRING="/MDd /Zi /Ob0 /Od /RTC1/std:c++17" -DCMAKE_C_COMPILER:FILEPATH="E:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/HostX64/x64/cl.exe" -DCMAKE_CXX_COMPILER:FILEPATH="E:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/HostX64/x64/cl.exe"  -DCMAKE_BUILD_TYPE="Debug" -DCMAKE_MAKE_PROGRAM="E:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe" "X:\Code\OpenCv\OpenCv" 2>&1"

I am testing to see what version I am in with this

    if (__cplusplus == 202101L) std::cout << "C++23";
    else if (__cplusplus == 202002L) std::cout << "C++20";
    else if (__cplusplus == 201703L) std::cout << "C++17";
    else if (__cplusplus == 201402L) std::cout << "C++14";
    else if (__cplusplus == 201103L) std::cout << "C++11";
    else if (__cplusplus == 199711L) std::cout << "C++98";
    else std::cout << "pre-standard C++." << __cplusplus;
    std::cout << "\n";

Output is c++98 Not sure where I have messed up. Any help would be lovely.

1 Answers1

0

Microsoft C Compiler (MSVC) by default always defines __cplusplus as 199711L whatever the /std command line option, for compatibility reasons, Microsoft says.

To have the compiler properly defining __cplusplus, you need to add the command line switch /Zc:__cplusplus

prapin
  • 6,395
  • 5
  • 26
  • 44
  • Thanks so much, That was the problem for anyone who runs into problem if(MSVC) target_compile_options(OpenCv PRIVATE "/Zc:__cplusplus") target_compile_options(OpenCv PRIVATE "/std:c++17") else() target_compile_features(OpenCv PRIVATE cxx_std_17) endif() – Honey Hiccups Jun 28 '23 at 22:18