使用printf()函数输出中文,会发现是乱码,在CmakeLists.txt文件的末尾添加如下代码即可:
IF (WIN32)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fexec-charset=GBK")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fexec-charset=GBK")
ENDIF ()
完整CmakeLists.txt内容如下:
cmake_minimum_required(VERSION 3.20)
project(Hello)
set(CMAKE_CXX_STANDARD 14)
add_executable(Hello main.cpp)
IF (WIN32)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fexec-charset=GBK")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fexec-charset=GBK")
ENDIF ()