VSCode配置OpenGL開發環境


  首先需要配置好VSCode的C++開發環境,這個網上有參考教程,這里不展開講了。MinGW的版本信息是:MinGW-W64-builds-4.3.5,CMake的版本是3.15.3。

  最近有時間,想再把OpenGL的教程撿起來再看看。之前用VisualStudio已經配置好了OpenGL的開發環境,后來在使用VSCode的過程中,使用到了Code Run這個插件之后,發現挺適合用來運行片段代碼,運行OpenGL的一些示例代碼。之前已經配置了VSCode的C++運行環境,覺得再配置一個OpenGL的開發環境應該不會太難,后來發現還是有些地方需要注意的。

  首先還是用之前VisualStudio配置環境需要用到的資源:GLFW,GLAD,可以參考之前的博客

1、配置GLFW

  改一下CMake,選擇MinGW生成:

 

 

   點擊Configure之后,可能會有錯誤,類似:sh.exe was found in your PATH,here: xxx Git/bin/sh.exe,這個是因為把Git配置到了環境變量中,導致沖突了。可以修改一下glfw源代碼目錄下的CMakeLists.txt文件,在里面加上 set(DCMAKE="CMAKE_SH-NOTFOUND") 就可以正常生成了。然后再勾選 BUILD_SHARED_LIBS ,點擊Generate生成。

 

  之后進入build目錄,打開控制台,執行mingw32-make命令,沒有錯誤的話可以在build/src目錄下得到glfw3.dll,libglfw3dll.a兩個文件。

2、配置GLAD

  glad還是用之前的資源,不過這里是將glad編譯成了靜態庫,之前VisualStudio里面是沒有編譯的。在glad的目錄(里面有include、src兩個目錄),執行以下命令:

gcc .\src\glad.c -c -I. \include\
ar -rc libglad.a glad.o

  就能在glad目錄下得到libglad.a 和 glad.o 兩個文件。在這里我再創建了一個目錄prebuild,把這兩個文件放在這個目錄下了。

  在這里我的目錄是這樣的:

 

   external主要是放庫文件、頭文件,opengl_study/tutorials里面是放每一章教程里面的示例代碼文件,opengl_study/exercises里面是放每一章后面的練習代碼文件,其余是配套的一些代碼。

3、配置命令

  在VSCode里面有兩種方式運行C++代碼:一種是按F5,可以斷點調試;另外一種是右鍵-Run Code。后者就是利用了CodeRun插件實現的。使用兩種方法,需要針對兩種方法配置好命令。當然這兩種方式都是以配置好了C++運行環境為前提的。

3.1、配置調試命令

  按F5運行,主要是要配置三個文件,.vscode/c_cpp_properties.json 、.vscode/launch.json 和 .vscode/tasks.json:

 3.1.1 .vscode/c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [ 
                "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include/**",
                "${workspaceFolder}/**",
                "${workspaceFolder}/external/glad/include/**",
                "${workspaceFolder}/external/glm/**",
                "${workspaceFolder}/external/glfw/include/**",
                "${workspaceFolder}/external/assimp/include/**"
            ],
            "browse": {
                "path": [
                    "${workspaceFolder}",
                    "${workspaceFolder}/external/glfw/prebuild",
                    "${workspaceFolder}/external/assimp/prebuild"
                ]
            },
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE",
                "_VSCODE"
            ],
            "compilerPath": "D:/mingw64/bin/gcc.exe",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

  這個文件主要是配置包含目錄,includePath,加上 "${workspaceFolder}/**" 之后基本上目錄下的文件都能找到了,這里提一下,${workspaceFolder}這個指的是VSCode當前打開的目錄。compilerPath填mingw的目錄。

3.1.2 .vscode/launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(gdb) Launch", 
      "type": "cppdbg", 
      "request": "launch",
      "program": "${workspaceFolder}/run/${fileBasenameNoExtension}.exe", 
      "args": [], 
      "stopAtEntry": true,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": true, 
      "internalConsoleOptions": "neverOpen",
      "MIMode": "gdb",
      "miDebuggerPath": "D:/mingw64/bin/gdb.exe",
      "setupCommands": [ 
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": false
        }
      ],
      "preLaunchTask": "C/C++: g++.exe build active file" 
    }
    
  ]
}

  這里注意 program 字段要與生成的exe路徑對應,這個生成的exe路徑是在后面的tasks.json文件里面配置的。另外就是preLaunchTask要與tasks.json里面定義的task的label對應上。

3.1.3 .vscode/tasks.json

{
    "version": "2.0.0",
    "command": "g++",
    "args": [
        "-g",
        "${file}",
        "-o",
        "${workspaceFolder}/run/${fileBasenameNoExtension}.exe"
    ],
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": [
            "relative",
            "${workspaceRoot}"
        ],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    },
    "group": {
        "kind": "build",
        "isDefault": true
    },
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++.exe build active file",
            "command": "D:\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${workspaceFolder}/run/${fileBasenameNoExtension}.exe",
                "-I${workspaceFolder}/external/glad/include/",
                "-I${workspaceFolder}/external/glm",
                "-I${workspaceFolder}/external/glfw/include",
                "-I${workspaceFolder}/external/assimp/include",
                "-L${workspaceFolder}/external/glad/prebuild",
                "-L${workspaceFolder}/external/glfw/prebuild",
                "-L${workspaceFolder}/external/assimp/prebuild",
                "-lglfw3dll",
                "-lglad",
                "-D_VSCODE"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

  這里主要是tasks里面的配置,args要填寫好對應的命令。這里展開講一下gcc/g++編譯時會用上的命令:-I(大寫i)加后面的路徑,表示引入的頭文件目錄;-L(大寫l)加后面的路徑,表示引入的庫文件目錄;-D加后面的參數,表示預定義宏/用戶自定義宏;-l(小寫l)后面加的參數,表示引入的靜態庫文件。不過這里有個疑問就是:明明生成的文件是libglfw3dll.a,但是用的時候得glfw3dll,如果加上了lib還會報錯說找不到。

  配置好這三個文件之后,按F5就能跑起來了。

3.2 配置右鍵Run Code命令

  這里主要是配置好 .vscode/setting文件就可以了,打開這個文件,找到code-runner.executorMap這個屬性,修改cpp的值:

"cpp": "cd $dir && g++ $fileName -o $workspaceRoot/run/$fileNameWithoutExt.exe -I $workspaceRoot/assimp/include -I $workspaceRoot/external/glm -I $workspaceRoot/external/glad/include -I $workspaceRoot/external/glfw/include -L $workspaceRoot/external/glad/prebuild -L $workspaceRoot/external/glfw/prebuild -L $workspaceRoot/external/assimp/prebuild -lglfw3dll -L assimpd -lglad -D _VSCODE && $workspaceRoot/run/$fileNameWithoutExt",

  其實就是把之前配置tasks.json文件里面的gcc/g++命令重新寫一下。

  都配置好之后,就可以愉快的用VSCode來敲代碼啦!


免責聲明!

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



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