【问题标题】:vs code c++ breakpoint cannot work in macvs code c++断点不能在mac中工作
【发布时间】:2017-05-31 05:37:40
【问题描述】:

我的操作系统版本是 sierra 10.12.1,vs 代码版本是 1.8.1。我在 vs 代码中安装了 c++ 插件。然后我创建了一个 c++ 项目。 有我的 c++ 源文件。

my_simple.cc

int main(int argc, char const *argv[])
{
    printf("%s\n", "******begin******");
    int a = 1;
    int b = a;
    printf("%s\n", "******end******");
    return 0;
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "preLaunchTask": "pre_compile",
            "showDisplayString": true,
            "name": "my_debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${file}.o",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": false,
            "osx": {
                "MIMode": "lldb"
            }
        }
    ]
}

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "g++",
    "isShellCommand": true,
    "args": [
    ],
    "showOutput": "always",
    "echoCommand": true,
    "suppressTaskName": true,
    "tasks": [
        {
            "taskName": "pre_compile",
            "args": [
                "${file}",
                "-o${file}.o"
            ],
            "isBuildCommand": true
        }
    ]
}

当我向 my_simple.cc 添加一些断点然后按 f5 编译并运行它时。断点没有按预期工作。请帮我找出我的代码中的错误。谢谢

【问题讨论】:

  • 你在哪里设置断点?编译器可能正在优化不存在的变量(您从不使用它们)。
  • 请注意,编译器可以将您的函数优化为:int main(int argc, char const *argv[]) { fputs("******begin******\n******end******\n",stdout); return 0 } - 但这种攻击程度是不寻常的。
  • 我尝试添加'printf("%i\n", b);',然后设置断点。但是断点还是不行。

标签: c++ macos debugging visual-studio-code


【解决方案1】:

1) 使用以下命令创建新的 CMakeList.txt:

cmake_minimum_required(VERSION 3.0)
project(FirstProgram)
set(SOURCE Hello.cpp)
add_executable(${PROJECT_NAME} ${SOURCE})

2)Task.json:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "sh",
    "isShellCommand": true,
    "args": ["-c"],
    "showOutput": "always",
    "suppressTaskName": true,
    "options": {
        "cwd": "${workspaceRoot}/build"
    },
    "tasks": [
        {
            "taskName": "cmake",
            "args": ["cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug .."]     
        },
        {
            "taskName": "make",
            "args": ["make -j 8"],
            "isBuildCommand": true            
        }
    ]
}

3) 启动.json:

{
    "version": "0.2.0",
    "configurations": [
        {

            "showDisplayString": true,
            "name": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/Build/FirstProgram",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb"
        }
    ]
}

4) 任务运行 --> cmake 然后:make

【讨论】:

    猜你喜欢
    • 2021-08-12
    • 2020-07-11
    • 1970-01-01
    • 2020-02-17
    • 2022-01-09
    • 1970-01-01
    • 2021-05-08
    • 2018-11-21
    • 2021-12-23
    相关资源
    最近更新 更多