ubuntu+VS code+launch.json+task.json


1.ubuntu->vs code

1.  通過官方PPA安裝Ubuntu make

sudo add-apt-repository ppa:ubuntu-desktop/ubuntu-make
    sudo apt-get update
    sudo apt-get install ubuntu-make
2. 使用命令安裝visual studio code

umake ide visual-studio-code

3.使用文件安裝:網站下載deb:https://code.visualstudio.com
sudo dpkg -i code_1.38.1-1568209190_amd64.deb

 

2.vs code配置

tasks可以被用來做編譯,而launch用來執行編譯好的文件。

2.1 launch.json——告訴VS Code如何執行啟動任務

// launch.json

// https://code.visualstudio.com/Docs/editor/debugging#_launch-configurations// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team        
// ${file}: the current opened file                     
// ${fileBasename}: the current opened file's basename 
// ${fileDirname}: the current opened file's dirname    
// ${fileExtname}: the current opened file's extension  
// ${cwd}: the current working directory of the spawned process
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch", //這個應該是F1中出現的名字
            "preLaunchTask": "Build",  //在launch之前運行的任務名,這個名字一定要跟tasks.json中的任務名字大小寫一致
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe", //需要運行的是當前打開文件的目錄中,名字和當前文件相同,但擴展名為exe的程序
            "args": [],
            "stopAtEntry": false, // 選為true則會在打開控制台后停滯,暫時不執行程序
            "cwd": "${workspaceFolder}", // 當前工作路徑:當前文件所在的工作空間
            "environment": [],
            "externalConsole": true,  // 是否使用外部控制台,選false的話,我的vscode會出現錯誤
            "MIMode": "gdb",
            "miDebuggerPath": "c:/MinGW/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }]
}

 

2.2 task.json——告訴launch或者編譯器需要執行什么操作

// tasks.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build",  // 任務的名字叫Build,注意是大小寫區分的,等會在launch中調用這個名字
            "type": "shell",  // 任務執行的是shell命令,也可以是
            "command": "g++", // 命令是g++
            "args": [
                "'-Wall'",
                "'-std=c++17'",  //使用c++17標准編譯
                "'${file}'", //當前文件名
                "-o", //對象名,不進行編譯優化
                "'${fileBasenameNoExtension}.exe'",  //當前文件名(去掉擴展名)
            ],
          // 所以以上部分,就是在shell中執行(假設文件名為filename.cpp)
          // g++ filename.cpp -o filename.exe
            "group": { 
                "kind": "build",
                "isDefault": true   
                // 任務分組,因為是tasks而不是task,意味着可以連着執行很多任務
                // 在build組的任務們,可以通過在Command Palette(F1) 輸入run build task來運行
                // 當然,如果任務分組是test,你就可以用run test task來運行 
            },
            "problemMatcher": [
                "$gcc" // 使用gcc捕獲錯誤
            ],
        }
    ]
}

 

3.gcc/g++

GCC:GNU Compiler Collection(GUN 編譯器集合),它可以編譯C、C++、JAV、Fortran、Pascal、Object-C、Ada等語言。

gcc是GCC中的GUN C Compiler(C 編譯器)

g++是GCC中的GUN C++ Compiler(C++編譯器)

3.1 寫一個Main.cpp

3.2 輸入> g++ -g -c Main.cpp,文件空間下會出現 Main.o。-c只編譯生成目標文件並沒有形成可執行文件。(-g:添加gdb調試選項。)

3.3輸入 g++ -o Main.out Main.o ,文件空間下將會出現 Main.out(可執行文件)。 -o 編譯加鏈接直接生成了可執行文件 .out

所以上面的命令也可以簡化為: g++ -o Main.out Main.cpp

 

4.make/makefile/cmake/CMakeLists.txt

  • make工具通過調用makefile文件中的命令便可以對大型程序進行編譯,而makefile文件中就包含了調用gcc去編譯多個源文件的命令。
  • 通過cmake我們就可以快速創建出不同平台的makefile文件。如果程序是跨平台,換個平台makefile又要重新修改,這會很麻煩,所以就出現了cmake這個工具。

為了編譯一個大型程序,你首先編寫CMakeLists.txt。然后,通過cmake命令就可以生成makefile文件。然后通過make命令就可以使用這個makefile文件,其調用gcc從而生成可執行文件。

 

https://blog.csdn.net/lwwl12/article/details/78189382

 

 

參考:https://www.jianshu.com/p/776a5fb57fbb

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM