2

Before posting this question, I have read lots of threads published on StackOverflow. And I still did not find an answer that works. I have seen that some people saty message is a duplicate, but it is not.

I have checked https://cmake.org/cmake/help/latest/prop_tgt/CXX_STANDARD.html#prop_tgt:CXX_STANDARD But i want to apply that to the special case of MSVC, as MSVC is a special case.

I tried that in the out folder : set_target_properties(myTarget PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO )

For example, I have read:

But these do not apply to my problem exactly.

Indeed, I recently uninstalled Visual Studio Community 2017, and I have installed Visual Studio Community 2019.

I wanted to compile a code coming from an idea of Bartłomiej Filipek.

The code is below. I used CMake. To the default CMake code, I have added the following:

if (MSVC_VERSION GREATER_EQUAL "1900")
    include(CheckCXXCompilerFlag)
    CHECK_CXX_COMPILER_FLAG("/std:c++latest" _cpp_latest_flag_supported)
    if (_cpp_latest_flag_supported)
        add_compile_options("/std:c++latest")
    endif()
endif()

And I have the following compile error:

Erreur  C2039   'align_val_t' : not a member of 'std'   

So, it means that C++17 is not taken into account.

The version of CMake which is the CMake file by default is indicated by:

cmake_minimum_required (VERSION 3.8)

But on some pages of StackOverflow say that flag is taken into account since CMake VERSION 3.10

However, VSC 19 proposes CMake 3.8

That's quite strange if it is the latest version.

So, how can I definitely solve the problem please?

#include <cstdint>
#include <iostream>
#include <malloc.h>
#include <cassert>
#include <vector>
#include <new>

void* operator new(std::size_t size, std::align_val_t align) {
//#ifdef _MSC_VER
#if defined(_WIN32) || defined(__CYGWIN__)
auto ptr = _aligned_malloc(size, static_cast<std::size_t>(align));
std::cout << "wind" << '\n';
#else
auto ptr = aligned_alloc(static_cast<std::size_t>(align), size);
#endif

if (!ptr)
    throw std::bad_alloc{};

std::cout << "new: " << size << ", align: "
    << static_cast<std::size_t>(align)
    << ", ptr: " << ptr << '\n';

return ptr;
}

void operator delete(void* ptr, std::size_t size, std::align_val_t align) 
{
std::cout << "avec size delete: " << size << ", align: "
    << static_cast<std::size_t>(align)
    << ", ptr : " << ptr << '\n';
//#ifdef _MSC_VER
#if defined(_WIN32) || defined(__CYGWIN__)
_aligned_free(ptr);
#else
free(ptr);
#endif
}

void operator delete(void* ptr, std::align_val_t align) {
std::cout << "delete: align: "
    << static_cast<std::size_t>(align)
    << ", ptr : " << ptr << '\n';
//#ifdef _MSC_VER
#if defined(_WIN32) || defined(__CYGWIN__)
_aligned_free(ptr);
#else
free(ptr);
#endif
}

//And here’s some test code:

class alignas(32) Vec3dAVX {
double x, y, z;
};

int main() {

std::cout << "std::vector\n";
std::vector<Vec3dAVX> vec;
vec.push_back({});
vec.push_back({});
vec.push_back({});
assert(reinterpret_cast<uintptr_t>(vec.data()) % alignof(Vec3dAVX) == 0);
}

The CMake code:

# CMakeList.txt : fichier projet CMake de niveau supérieur, effectuez une configuration globale
# et incluez les sous-projets ici.
#
cmake_minimum_required (VERSION 3.8)

project ("lameduck")

 #Incluez les sous-projets.
add_subdirectory ("lameduck")

if (MSVC_VERSION GREATER_EQUAL "1900")
    include(CheckCXXCompilerFlag)
    CHECK_CXX_COMPILER_FLAG("/std:c++latest" _cpp_latest_flag_supported)
    if (_cpp_latest_flag_supported)
        add_compile_options("/std:c++latest")
    endif()
endif()
Dev
  • 463
  • 3
  • 12
  • You could refer [here](https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_STANDARD.html) and [here](https://cmake.org/cmake/help/latest/prop_tgt/CXX_STANDARD.html#prop_tgt:CXX_STANDARD) for C++ Standard specification in a generic way for cross platform project. – AmeyaVS Sep 09 '19 at 11:16
  • 1
    If you create libraries/executables under `lameduck` subdirectory, then they are not affected by `add_compile_options` issued **after** stepping into the directory (with `add_subdirectory()` call). You need firstly adjust the flags and then step into the subdirectory. – Tsyvarev Sep 09 '19 at 12:25
  • Possible duplicate of [Adding global compile flags in CMake](https://stackoverflow.com/questions/52679999/adding-global-compile-flags-in-cmake) – Kevin Sep 09 '19 at 12:49
  • You have updated the text in the question, but what about updating the **code**? Where exactly in `CMakeLists.txt` do you create an executable or a library? (You have some comments in the `CMakeLists.txt`, but they are non-English...). My previous comment was just a *guessing* about the executable/library location, but on Stack Overflow we prefer to understand the code without guesses. – Tsyvarev Sep 09 '19 at 21:41

0 Answers0