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()