How to solve different problems with the compiler for #cmake#?

dkaola 注册会员
2023-02-26 18:19

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:

  1. 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.

  2. Then, set the corresponding compiler version in CMakeLists.txt, as follows:

     set(CMAKE_C_COMPILER "cl.exe") # 设置c编译器 
     set(CMAKE_CXX_COMPILER "cl.exe") # 设置c++编译器 
     set(CMAKE_GENERATOR "Visual Studio 2022") # 设置生成器 
     set(CMAKE_C_COMPILER "MinGW Makefiles") # 设置c编译器 
     set(CMAKE_CXX_COMPILER "MinGW Makefiles") # 设置c++编译器 
  3. Then, set the corresponding C++ standard in CMakeLists.txt, as follows:

    set(CMAKE_CXX_STANDARD 11) # 设置c++标准为11
  4. 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目录加入头文件搜索路径 
  5. 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.