C , C++, C#

[C++] VS 2017/C++14 에서 VS 2022/C++20 마이그레이션 에러 정리

vhxpffltm 2024. 2. 20. 14:41
반응형

현재 Windows 환경에서 Visual Studio 2017 / C++14 피처를 사용하는 프로젝트이다.

 

이 프로젝트의 컴파일러와 피처를 업데이트하면서 발생하게 된 에러를 정리해본다.

 

업데이트 이유는 최신 VS IDE에서 sanitizer도 지원하고 최신 C++ 피처 사용등이 가능하기 때문이다.

(덕분에 템플릿 관련쪽을 concepts으로 바꾸는 작업도 공부할 수 있었다.)

 

이제 하나씩 정리해보자.

 

1.c2872 'byte' ambiguous symbol

컴파일시 이런 에러가 발생하는데... byte 키워드가 이상하단다.. 컴파일러의 헤더파일에서 발생한다.

 

https://developercommunity.visualstudio.com/t/error-c2872-byte-ambiguous-symbol/93889

 

위와 같이 관련내용을 확인할 수 있다.

필자는 CMakeLists.txt에 _HAS_STD_BYTE=0 의 옵션을 추가하여 해결하였다.

 

2. __cplusplus 매크로에 대한 사용

컴파일중 #if.. 로 이루어져 있는데 __cplusplus 매크로를 사용하고 있다. 문제는 전처리시 값이 이상하게 되어 있는지 VS에서 C++ 피처 옵션에 따라 별 반응이 없다.

 

https://stackoverflow.com/questions/74367383/cplusplus-apparently-not-set-correctly-in-visual-studio-2022-when-building-for

 

__cplusplus apparently not set correctly in Visual Studio 2022 when building for C++17?

My code has the following test, to protect code that only works in C++11 or newer. It is evaluating to 0 despite cl being invoked with /std:c++17 . #if __cplusplus >= 201103 I am seeing this in

stackoverflow.com

 

위 링크대로 VS의 버그로 되어있으니 /Zc:__cplusplus 옵션을 명시적으로 활성화 하였다.

 

 

3.c2440 'initializing' cannot convert from

이는 VS 19 이상 그리고 C++20 이상이면 자동으로 활성화되며 기존의 const char* 타입이 const char8_t* 타입으로 변한다. 그로인한 문제이다.

 

https://learn.microsoft.com/ko-kr/cpp/error-messages/compiler-errors-1/compiler-error-c2440?view=msvc-170

 

컴파일러 오류 C2440

컴파일러 오류 C2440을 발생시키는 형식 변환 오류에 대해 알아봅니다.

learn.microsoft.com

 

위 MSVC 문서에 상세히 설명되어있다.

 

 

4. C++20: aggregate initialization error

갑자기 특정 생성자가 있으면서 return {...} 구문에서 컴파일 에러가 발생했다. 집계 생성자 초기화가 안되던거 같은데... 아래 내용을 확인하면 좋다.

 

https://stackoverflow.com/questions/57271400/why-does-aggregate-initialization-not-work-anymore-since-c20-if-a-constructor

 

Why does aggregate initialization not work anymore since C++20 if a constructor is explicitly defaulted or deleted?

I'm migrating a C++ Visual Studio Project from VS2017 to VS2019. I'm getting an error now, that didn't occur before, that can be reproduced with these few lines of code: struct Foo { Foo() =

stackoverflow.com

 

위 링크대로 해당 클래스의 생성자쪽을 상황에 맞게 수정하여 해결하였다.

 

5. ETC

이정도만 수정하여 컴파일 및 빌드까지는 완료되었다.

 

문데는 컴파일러가 업데이트 되고, sianitizer를 사용하려고 했는데.. 아직 CMake의 설정이 제대로 되지 않아 작업이 좀 더 필요하다.

또한, 템플릿 꼬리표 분배 방식이 필요할 수 있어 작업하다 이거보단 c++20인데 concepts을 사용하면 더 보기 좋을거 같아 작업중이다.

문제는 concpets을 사용하다 보니 라이브러리 빌드는 되는데.. test 바이너리가 빌드에 실패한다.. 고쳐야한다..

 

관련해서 살펴본 내용은 아래와 같다.

 

https://learn.microsoft.com/en-us/cpp/sanitizers/error-container-overflow?view=msvc-170

 

Error: container-overflow

Source examples and live debug screenshots for container overflow errors.

learn.microsoft.com

 

 

https://cppmagister.tistory.com/26

 

C++ 템플릿(Template): 꼬리표 분배(Tag Dispatching) 기법

SFINAE 기법을 사용하면, 새로운 분기가 추가되면 기존 함수들의 템플릿 명세를 수정해야 합니다. 꼬리표 분배 방법은 템플릿 명세로 구분하는 것을, 함수의 인자로 구분하여 사용하는 방법입니

cppmagister.tistory.com

 

 

반응형