一个简单的helloworld.cpp,运用CMakeLists进行编译,一切都设置好了,
launch.json和tasks.json也配置好了;
手动进行编译的话,能够成功
mkdir build
cd build
cmake -G "MinGW Makefiles" ..
mingw32-make
一切正常
但是如果是配置好json文件,就始终不行:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/cmake_hello.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:/CppCompiler/mingw64/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set disassembly flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "Build"
}
]
}
tasks.json
{
"version": "2.0.0",
"options": {
"cwd": "${workspaceFolder}/build"
},
"tasks": [
{
"options": {
"cwd": "${workspaceFolder}/build"
},
"type": "cppbuild",
"label": "cmake",
"command": "cmake",
"args": ["-G", "\"MinGW Makefiles\"", ".."],
"group": "build",
"detail": "cmake -G \"MinGW Makefiles\" .."
},
{
"options": {
"cwd": "${workspaceFolder}/build",},
"type": "cppbuild",
"label": "make",
"command": "mingw32-make",
"args": [],
"group": "build",
"detail": "make"
},
{
"label": "Build",
"dependsOn": [
"cmake",
"make"
]
}
]
}
始终报错,不明原因;
原因是vscode的相对路径有问题,这个问题一直存在,虽然按照网上一的些方法做过修改,但是
.. 却不是上一层目录,而是工作目录的上一层目录,单个点 . 不代表该文件所在目录,而是工作目录;
以你为是设置问题,但是其实在其它文件下,单个点 . 又代表了当前目录;
甚至于你,同一个文件多次运行,相对路径出会现报错的情况,vscode它一会儿识别为当前目录,一会儿又识别为工作目录,乱七八糟,恶心死你;
所以,改成绝对路径,问题就好了;
{
"version": "2.0.0",
"options": {
"cwd": "${workspaceFolder}/build"
},
"tasks": [
{
"options": {
"cwd": "${workspaceFolder}/build"
},
"type": "cppbuild",
"label": "cmake",
"command": "cmake",
"args": ["-G", "\"MinGW Makefiles\"", "${workspaceFolder}"],
"group": "build",
"detail": "cmake -G \"MinGW Makefiles\" ${workspaceFolder}"
},
{
"options": {
"cwd": "${workspaceFolder}/build",},
"type": "cppbuild",
"label": "make",
"command": "mingw32-make",
"args": [],
"group": "build",
"detail": "make"
},
{
"label": "Build",
"dependsOn": [
"cmake",
"make"
]
}
]
}
另外,这个
"options": {
"cwd": "${workspaceFolder}/build"
},
要放在单个task里面,不然也是混乱的,会报错的。
如果一次运行只是成功生成了Makefile, 那就再运行一次,就又成功生成了可执行文件。