Reputation: 850
I am trying to setup visual studio code for opengl development. I already have it working for plain c/c++ development I am now trying to add in opengl development to the mix. I know how to setup opengl on other platforms (i.e. Eclipse, Xcode, Visual Studio, CodeBlocks). The root of my problem is more how to setup dependencies in visual studio code. My best guess would be running a task in the task.json file. Right now it is just filled with the code to compile the project every time I run the program.
Upvotes: 10
Views: 69460
Reputation: 11
OpenGl_c++_GLFW_GLAD_VScode_Linux_mint(Ubuntu/Debian) SETUP
requirements:
Part 1:system configuration.
**1)**linux-mint comes pre_installed with c/c++ compiler.
**2)**download vscode for linux_mint with c++ extentions and support (includes syntax highlighting).
TIP:power user files are installed in /usr/ and local user files are stored in /usr/local/, any can be used here.
**3)**open terminal and type the below command that installs the GLFW library along with opengl mesa libraries .
sudo apt-get install libglfw3-dev libgl1-mesa-dev libglu1-mesa-dev freeglut3-dev libglew-dev
check if glfw is installed and where on your mint/ubuntu using this :
pkg-config --cflags glfw3
**4)**download glad from website https://glad.dav1d.de/ now in the website choose :
then hit generate to download the glad zip. now unzip the glad.zip file to anywhere on your system and open it to find Include and scr files .
open scr file then copy glad.c file to your project folder containing the cpp file.
open the include file then copy the glad and khr files to usr/include like this ---> /include/glad/glad.h and /include/KHR/KHR.h to /usr/include which can be done with the below command:
cp -r PathToYourUnzippedGladFolder/include/glad /usr/include
cp -r PathToYourUnzippedGladFolder/include/KHR /usr/include
or
cp -r PathToYourUnzippedGladFolder/include/glad /usr/local/include
cp -r PathToYourUnzippedGladFolder/include/KHR /usr/local/include
by default linux may/MAY NOT allow permission to read and write these glad include files so to grant permision and to AVOID PERMISSION DENIED ERRORS dring compilation use this command.(go to the /use/include or /usr/local/include folder where you have pasted the glad and KHR include files)
chmod 755 myfolder //example `chmod 755 glad` , `chmod 755 KHR`
Part 2: VScode configuration.
**5)**make a folder for opengl project/coding and open vscode in that folder then create a cpp file name it anything. lets paste a simple triangle opengl code from learnopengl which has, glfw and glad used .
**6)**just open Tasks.json file for some editing thats all.just copy this in it.
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-lGLU",
"-lglfw",
"-lGLEW",
"-lGL",
"-lglut" ,
"${workspaceFolder}/glad.c"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
remember to add "${workspaceFolder}/glad.c"
in tasks.json file in args as shown above or just copy paste the above tasks.json code .
2 screenshots : my tasks.json screenshot my glfw glad location in usr include
Upvotes: 0
Reputation: 2154
Had the same Issue, what I had to do was just to:
sudo apt-get install libglfw3-dev libgl1-mesa-dev libglu1-mesa-dev freeglut3-dev libglew-dev
As mentioned Here
.cpp
file that you want to compile / (start with)tasks.json
press "F5" > Select "C++ (GDB/LLDB)" > select g++ compiler > stop compiling processtasks.json
by adding the extra arguments (as mentioned by @crazypicklerick) like:{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-lGLU",
"-lglfw",
"-lGLEW",
"-lGL",
"-lglut"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
.cpp
fileUpvotes: 3
Reputation: 39
in your build task add theses arguments if using gcc/gdb -lGLU -lGL -lglut
Upvotes: 1
Reputation: 24508
Came across the same problem. Note that there are two issues here:
launch.json
and tasks.json
files?
tasks
in greater detail.The main issue for me was that I am on MAC and many explanations were only targeted at Linux and Windows (which don't have the framework
parameter) or they explain how to set it all up in the Xcode GUI.
As this Github Gist and this StackOverflow post both suggest:
<gl/*>
(e.g. Mac has <OpenGL/*>
for part of it, and <GLUT/*>
for glut files)framework
to your build arguments (to include external libraries), e.g...
gcc -o ex1 ex1.c -framework GLUT -framework OpenGL -Wno-deprecated
cc <your_file.c> -framework GLUT -framework OpenGL
frameworks
to your C/C++ extension settings and/or tasks.json
as well(and again: it would be very similar when using clang
or clang++
)
Integrate that insight with the official documentation on C/C++ tasks
, and it's all done! :)
The above is just a shorter version of my overall journey, detailed below:
<gl/*>
(e.g. <OpenGL/*>
for core files and <GLUT/*>
for glut files).OpenGL
and other libraries via the framework
flag. frameworks
to your C/C++ extension settings as well-Wno-deprecated
)brew install glew
and brew install glfw
and/or whatever other external libraries you use.The VSCode setup is mostly the same. Make sure that, once you have setup the C/C++ extension correctly, to look at the documentation for your environment, which are at the bottom of the official "C/C++ for Visual Studio Code" documentation page.
However, the bigger problem is usually how to get it to compile. I don't have recent examples or experiences of compiling on non-MAC systems; but here are some relevant references:
Apparently cross-platform development still might cause some headaches, according to this (as of yet) open github issue #1083.
I also found an example by someone taking a jab at OpenGl cross-platform compilation using VSCode here. You might want to check out their entire .vscode/
folder (if not their entire project setup).
These days, it's very easy to add any amount of Launch (Run and/or Debug) configurations to the built-in Launch + Debugger UI by following these instructions.
Before the debugger UI was a thing (or at least before I noticed it), you could install the Code Runner extension, you will get a neat little "Run Code" button at the top right (or Command+Alt+N
).
This works fine if you just want to use it to run a single executable. If you want to run individual files individually, it gets a bit harder. I have not tried it, but as they suggest, for that, you could use a special file naming pattern, i.e. a glob pattern (e.g. *.gl.cpp
) and use the code-runner.executorMapByGlob
setting to have different compile+run parameters based on file names.
Upvotes: 11