Visual Studio Code에서 gcc로 compile 시 optimization 기능 강제로 끄기

Visual Studio Code에서 보통 gcc나 g++로 C/C++을 compile 하기 때문에 본 글 하단에 task.json을 많이 이용할 것이다.

그런데 작년인가? 간단한 'Hello World' 프로그램을 compile하여 OllyDBG나 IDA Pro로 reverse engineering 연습을 하려다 보니 자꾸 entry point도 못잡고 symbol도 다 날라가버리는 현상을 경험했다.

아무래도 optimization이 자동으로 켜져서 compile이 되었던 것 같은데..

그러다 반년이 지난 지금에서야 optimization을 강제로 끄는 compile 방법을 알아냈는데, 기존에 올렸던 task.json에서 아래 붉은 부분만 추가하면 된다.

지니넷에 따르면
-O0: 기본값으로 최적화를 수행하지 않는다. (여태껏 아무 설정도 안했는데 왜 나는...)
-O 또는 -O1: 코드 크기와 실행 시간을 줄이는 것을 제외한 최적화는 실행하지 않는다.
-O2: 메모리 공간과 속도를 희생하지 않는 범위내의 모든 최적화를 수행한다.
        loop unrolling과 function inlining에 대한 최적화를 수행하지 않는다.
-O3: -O2 최적화에 인라인 함수와 레지스터에 대한 최적화를 추가로 수행한다.
-Os: -O2 최적화 기능을 사용하지만, 코드 크기를 증가시키는 최적화는 생략한다.

{
    "version": "2.0.0",
    "runner": "terminal",
    "type": "shell",
    "echoCommand": true,
    "presentation" : { "reveal": "always" },
    "tasks": [
          // C++ compile
          {
            "label": "save and compile for C++",
            "command": "g++",
            "args": [
                "-O0",
                "${file}",
                "-g",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "group": "build",

            // Show errors on editors while compiling
            // See https://code.visualstudio.com/docs/editor/tasks#_defining-a-problem-matcher

            "problemMatcher": {
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    // The regular expression.
                   // Example to match: helloWorld.c:5:3: warning: implicit declaration of function 'prinft'
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        },
        // C compile
        {
            "label": "save and compile for C",
            "command": "gcc",
            "args": [
                "-O0",
                "${file}",
                "-g",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "group": "build",

            // Show errors on editors while compiling
            // See https://code.visualstudio.com/docs/editor/tasks#_defining-a-problem-matcher

            "problemMatcher": {
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    // The regular expression.
                   // Example to match: helloWorld.c:5:3: warning: implicit declaration of function 'prinft'
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        },
        // Excute the binary (Ubuntu)
        /*
        {
            "label": "execute",
            "command": "cd ${fileDirname} && ./${fileBasenameNoExtension}",
            "group": "test"
        }
        */
        // Excute binary (Windows)
        {
            "label": "execute",
            "command": "cmd",
            "group": "test",
            "args": [
                "/C",
                "${fileDirname}\\${fileBasenameNoExtension}"
            ]
        }
    ]
}


Comments