use vscode mingw cmake on windows

记住mingw去官网下面的MingW-W64-builds, 下这个版本
x86_64-13.1.0-release-posix-seh-ucrt-rt_v11-rev1.7z
posix很重要, win32版本没线程支持,用到会报错

可以测试下是否能正常运行

#include <thread>
#include <mutex>
#include <condition_variable>
#include <iostream>

std::thread second_workthread;
std::mutex mtx;
std::condition_variable objectDetectorRun;

task.json
安好mingw, cmake

{
    "version": "2.0.0",
    "windows": {
        "options": {
            "shell": {
                "executable": "powershell"
            }
        }
    },
    "type": "shell",
    "options": {
        "cwd": "${workspaceRoot}"
    },
    "tasks": [
        {
            "label": "cmake",
            "linux": {
                "command": "cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug .."
            },
            "windows": {
                "command": "cmake -G 'MinGW Makefiles' .."
            },
            "type": "shell",
            "dependsOn": "_makebuildfolder",
            "options": {
                "cwd": "${workspaceRoot}/build"
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "panel": "shared"
            },
            "problemMatcher": [],
            "group": "build"
        },
        {
            "label": "make",
            "linux": {
                "command": "make -j 8"
            },
            "windows": {
                "command": "mingw32-make -j 4"
            },
            "options": {
                "cwd": "${workspaceRoot}/build"
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "panel": "shared"
            },
            "isBuildCommand": true,
            "problemMatcher": [],
            "group": "build"
        },
        {
            "label": "_makebuildfolder",
            "type": "shell",
            "linux": {
                "command": "mkdir -p ${workspaceFolder}/build"
            },
            "windows": {
                "command": "mkdir -Force ${workspaceFolder}/build"
            },
            "problemMatcher": [],
            "group": "build"
        },
        {
            "label": "copy_raw",
            "type": "shell",
            "linux": {
                "command": "mkdir -p ${workspaceFolder}/build"
            },
            "windows": {
                "command": "Copy ${workspaceFolder}/*.raw ${workspaceFolder}/build "
            }
        },
        {
            "label": "run",
            "args": ["32"],
            "windows": {
                "command": "${command:cmake.launchTargetPath}"
            },
            "options": {
                "cwd": "${workspaceRoot}/build"
            },
        },
        {
            "label": "runwithbuild",
            "args": ["32"],
            "windows": {
                "command": "${command:cmake.launchTargetPath}"
            },
            "dependsOrder": "sequence", //tj : make sure exe deps in order
            "dependsOn": ["cmake", "make", "copy_raw"],
            "options": {
                "cwd": "${workspaceRoot}/build"
            },
        }
    ]
}

对于opencv, 要么自己用Mingw编译, 要么下载现存的, 我这里下的现成的https://github.com/huihut/OpenCV-MinGW-Build

cmake_minimum_required( VERSION 3.5 )
project( main )

set(CMAKE_CXX_FLAGS "-std=c++14")
set (OpenCV_DIR C:\\Software\\OpenCV-MinGW-Build)
find_package(OpenCV REQUIRED)

include_directories(${OpenCV_INCLUDE_DIRS})


add_executable( main main.cpp )
target_link_libraries( main ${Pangolin_LIBRARIES} )

注意把opencv的bin加入Path,不然找不到opencv的dll. 程序运行和调试都会有错, 特别是调试会出现During startup program exited with code 0xc0000135. 就是DLL找不到的问题

debug的话

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "targetArchitecture": "x86",
            "program": "${command:cmake.launchTargetPath}",
            // "program": "${workspaceFolder}/build/main.exe",
            // "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": ["32"],//左边启动debug, 从这里pick up args
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/build",
            "environment": [
                {
                    "name": "PATH",
                    "value": "${env:Path};C:\\Workspace\\v-nova\\build"
                    //这里设置错了,容易出现Unable to start debugging. Unexpected GDB output from command “-exec-run“
                }
            ],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/Software/mingw64/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
        }
    ]
}

需要配置用 cmake:configure with cmake debugger

在这里插入图片描述

注意最下面的状态栏, 如果没有设置点进去设置
另外 cmake可能会让把

C:\Software\OpenCV-MinGW-Build\x64\mingw\bin

加入环境变量

如果传参在launch.json 不work的话, 在setting.json里面加上

从状态栏启动debug的话, 设置 args要在setting.json里面

{
    "cmake.configureOnOpen": false,
    "files.associations": {
        "ostream": "cpp",
        "mutex": "cpp",
        "array": "cpp",
        "string": "cpp",
        "string_view": "cpp"
    },
    "C_Cpp.default.compilerPath": "C:\\Software\\mingw64\\bin\\g++.exe",
    "cmake.debugConfig": {
        "args": [
            "32"//下面启动debug从这里Pick up args
        ]
    }
}

从左边启动debug的话, args在launch.json里面设置

https://github.com/microsoft/vscode-cmake-tools/issues/121

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于在 Windows 上安装 MinGW-w64(MinGW64)编译环境并与 Visual Studio Code(VSCode)一起使用,您可以按照以下步骤进行操作: 1. 下载 MinGW-w64 安装程序: - 访问 MinGW-w64 官方网站:http://mingw-w64.org/doku.php/download - 在页面中找到“Downloads”部分,选择适合您系统的版本(32位或64位)的安装程序下载。 2. 运行 MinGW-w64 安装程序: - 打开下载的安装程序,选择 "Next" 进入安装向导。 - 在 "Installation Folder" 页面上,选择要安装 MinGW-w64 的目录。建议选择一个简单的路径,例如 `C:\mingw-w64`。 - 在 "Version" 页面上,选择所需的工具链架构(x86 或 x86_64)和线程模型(posix 或 win32)。一般情况下,选择默认选项即可。 - 接下来,您可以选择需要的组件。至少需要选择 "C Compiler" 和 "C++ Compiler"。您还可以选择其他组件,例如 "MSYS2 runtime"(提供了类似于 Linux 的 shell 环境)。 - 点击 "Next" 继续安装。 - 在 "Installation Status" 页面上,等待安装程序完成所需的下载和安装操作。 - 完成后,点击 "Finish" 完成安装。 3. 配置环境变量: - 右键点击您的计算机图标,选择 "属性"。 - 在左侧面板中,点击 "高级系统设置"。 - 在 "系统属性" 窗口中,点击 "环境变量"。 - 在 "系统变量" 部分,找到名为 "Path" 的变量,并点击 "编辑"。 - 在 "编辑环境变量" 窗口中,点击 "新建" 并添加 MinGW-w64 安装目录的路径(例如 `C:\mingw-w64\mingw64\bin`)。 - 确认所有窗口并保存更改。 4. 配置 VSCode: - 打开 VSCode,安装 "C/C++" 扩展。您可以在扩展商店中搜索并找到它。 - 安装完成后,点击 VSCode 左侧的扩展图标(四方块),找到并点击 "C/C++" 扩展。 - 在右侧面板中,找到 "Edit in settings.json" 并点击。这将打开 VSCode 的设置文件。 - 在设置文件中,找到 `"compilerPath"` 选项,并将其设置为 MinGW-w64 的 C 编译器路径(例如 `C:\\mingw-w64\\mingw64\\bin\\gcc.exe`)。 - 保存文件,并关闭设置窗口。 现在,您应该已经成功安装了 MinGW-w64 并将其与 VSCode 集成。您可以在 VSCode 中编写和编译 C/C++ 代码了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值