所以我在 VSCode 设置中遵循了 this 关于 CMake 的教程,一切都很顺利,直到我尝试构建项目。我选择了 urct64/bin/g++.exe 的预设。构建过程因错误退出并打印:
[main] Building folder: C:/Users/maxka/VSC/cmake_learn/out/build/ucrt64
[build] Starting build
[proc] Executing command: chcp
[proc] Executing command: C:\Cmake\bin\cmake.EXE --build C:/Users/maxka/VSC/cmake_learn/out/build/ucrt64 --parallel 10 --target cmake_learn --
[build] [ 50%] Building CXX object CMakeFiles/cmake_learn.dir/main.cpp.obj
[build] [100%] Linking CXX executable cmake_learn.exe
[build] collect2.exe: error: ld returned 116 exit status
[build] mingw32-make[3]: *** [CMakeFiles\cmake_learn.dir\build.make:98: cmake_learn.exe] Error 1
[build] mingw32-make[3]: *** Deleting file 'cmake_learn.exe'
[build] mingw32-make[2]: *** [CMakeFiles\Makefile2:838: CMakeFiles/cmake_learn.dir/all] Error 2
[build] mingw32-make[1]: *** [CMakeFiles\Makefile2:845: CMakeFiles/cmake_learn.dir/rule] Error 2
[build] mingw32-make: *** [Makefile:497: cmake_learn] Error 2
[proc] The command: C:\Cmake\bin\cmake.EXE --build C:/Users/maxka/VSC/cmake_learn/out/build/ucrt64 --parallel 10 --target cmake_learn -- exited with code: 2
[driver] Build completed: 00:00:01.153
[build] Build finished with exit code 2
我尝试运行的 .cpp 文件是:
#include <iostream>
// #include <stdio.h>
#include <vector>
#include <string>
using namespace std;
int main(){
// printf("Hello world\n");
cout << "Hello world\n" << endl;
return 0;
}
当使用
stdio.h
和 printf
而不是 iostream
和 cout
时,它可以正常构建和工作。
此外,当将配置预设从 urct64/bin/g++ 更改为 mingw64/bin/g++ 时,它的构建没有问题,但 .exe 在启动后立即退出 - 似乎它在第一个
iostream
函数调用时中断,因为如果两者printf
和 cout
未注释,printf
有效,但 cout
无效。如果重新排列这些,首先调用 cout
,则两者都不起作用。
CMakeLists 与教程中使用的相同:
cmake_minimum_required(VERSION 3.5.0)
project(cmake_learn VERSION 0.1.0 LANGUAGES C CXX)
include(CTest)
enable_testing()
add_executable(cmake_learn main.cpp)
set_property(TARGET cmake_learn PROPERTY CXX_STANDARD 17)
看起来确实存在 dll 兼容性问题,因为我下载的 MSYS 软件包杂乱无章。我重新安装了 MSYS 和我需要的所有软件包,现在它可以工作了。