Part of the answer references GPT, GPT_Pro better solve the problem
To solve the problem of Cmake using a different compiler, you can refer to the following steps:
-
First, determine the version of your current compiler. If it is Visual Studio, the general version will be 2022, 2021, etc. If you use MinGW, you need to look at its specific version number.
-
Then, set the corresponding compiler version in CMakeLists.txt, as follows:
if(MSVC) set(CMAKE_C_COMPILER "cl.exe") # 设置c编译器 set(CMAKE_CXX_COMPILER "cl.exe") # 设置c++编译器 set(CMAKE_GENERATOR "Visual Studio 2022") # 设置生成器 elseif(MINGW) set(CMAKE_C_COMPILER "MinGW Makefiles") # 设置c编译器 set(CMAKE_CXX_COMPILER "MinGW Makefiles") # 设置c++编译器 endif()
-
Then, set the corresponding C++ standard in CMakeLists.txt, as follows:
set(CMAKE_CXX_STANDARD 11) # 设置c++标准为11
-
Next, add the related dependency libraries in CMakeLists.txt as follows:
target_link_libraries(demo PUBLIC addition) # 添加addition库 target_link_libraries(demo PUBLIC subtract) # 添加subtract库 target_include_directories(demo PUBLIC Vcalc) # 将Vcalc目录加入头文件搜索路径
-
Finally, run the cmake command to regenerate the makefile as follows:
cmake .. -G "Generator Name" # 重新生成makefile文件
The above steps are a way to resolve errors that occur when cmake uses different compilers. After setting up the different compilers, standards, dependent libraries, and regenerating the makefile, you can compile the code properly.
If the answer is helpful, please accept it.