2

my os version is sierra 10.12.1 and vs code version is 1.8.1. I installed c++ plugin in vs code. And then I created a c++ project. There was my c++ source file.

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
        }
    ]
}

When I add some breakpoints to my_simple.cc and then press f5 to compile and run it. The breakpoints did not work as expected. Please help me find the mistake in my code. Thanks

3
  • Where do you set the breakpoints? The compiler may be optimizing the variable out of existence (you never use them). Commented Jan 16, 2017 at 11:35
  • Note that the compiler can optimize your function to: int main(int argc, char const *argv[]) { fputs("******begin******\n******end******\n",stdout); return 0 } - but that level of aggression is unusual. Commented Jan 16, 2017 at 11:36
  • I try to add 'printf("%i\n", b);', and then set the breakpoint. But the breakpoint still cannot work. Commented Jan 16, 2017 at 14:57

1 Answer 1

1

1) create new CMakeList.txt with:

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) launch.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) Task Run --> cmake and then: make

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.