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.
-
Have you considered to use CMake or similar to make life a bit easier? I personally like CMake and that makes it super easy to make it work with vscode and/or what ever other editor that might be needed later on. – grenangen Jan 10 '17 at 21:04
-
What are you trying to do? What have you tried? Did you follow a tutorial? – BeyelerStudios Jan 10 '17 at 21:22
-
so far I have just followed https://code.visualstudio.com/docs/languages/cpp. @grenangen is there a good tutorial that you might know of for using cmake and vs code? – Matthew Jan 11 '17 at 14:13
-
1@Matthew I would suggest starting with http://derekmolloy.ie/hello-world-introductions-to-cmake/ for CMake only, and you can look at http://stackoverflow.com/questions/30269449/how-do-i-set-up-vscode-to-compile-c-code and to hook up CMake into the build from VSCode you can for example look at http://stackoverflow.com/questions/30269449/how-do-i-set-up-vscode-to-compile-c-code - Search for "If your project has a CMake configuration it's pretty straight forward to setup VSCode" to see a concrete example. – grenangen Jan 11 '17 at 21:38
3 Answers
OpenGl & C/C++ & VSCode
Came across the same problem. Note that there are two issues here:
- How to setup the
launch.jsonandtasks.jsonfiles?- Start with the official VSCode documentation on C++ programming with VSCode
- The tasks documentation explains how to setup and run
tasksin greater detail.
- What commands to use to compile the code?
- This is where it gets tricky. Compilation depends on the OS and environment. The part below mostly focuses on that:
MAC
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.
TL;DR
As this Github Gist and this StackOverflow post both suggest:
- Fix your include file names
- Not everything is in
<gl/*>(e.g. Mac has<OpenGL/*>for part of it, and<GLUT/*>for glut files)
- Not everything is in
- In order to run, add
frameworkto your build arguments (to include external libraries), e.g...gcc -o ex1 ex1.c -framework GLUT -framework OpenGL -Wno-deprecatedcc <your_file.c> -framework GLUT -framework OpenGL
- Don't forget: Add those
frameworksto your C/C++ extension settings and/ortasks.jsonas 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! :)
More Details
The above is just a shorter version of my overall journey, detailed below:
- I also started with the official VSCode instructions on C++ programming with VSCode (which you also mentioned).
- Then I followed their link at the bottom: Using Clang in Visual Studio Code (the GCC setup is very similar).
- Followed this Github Gist on the differences between Mac and non-Mac OpenGl compilation. There are two major differences:
- The APPLE paths are slightly different; not everything is in
<gl/*>(e.g.<OpenGL/*>for core files and<GLUT/*>for glut files). - You have to add
OpenGLand other libraries via theframeworkflag.
- The APPLE paths are slightly different; not everything is in
- Once you took care of that:
- Add those
frameworksto your C/C++ extension settings as well - (When just getting started, you might want to cut down on the deprecation warnings; as OpenGL went through major changes in the past decade, using
-Wno-deprecated)
- Add those
- If you have not done so before, you probably want to
brew install glewandbrew install glfwand/or whatever other external libraries you use.
What about Windows & Linux?
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:
Cross-platform-ish?
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).
Run+Debug at the click of a button
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.
[Deprecated] VSCode "Code Runner" Extension
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.
- 18,735
- 12
- 77
- 106
-
1
-
-framework OpenGL doesn't work with clang, neither does -lGL or -lOpenGL – Petruza Jun 14 '20 at 05:59
-
@Petruza, [the post I linked](https://stackoverflow.com/questions/2568067/using-gcc-mingw-to-compile-opengl-on-windows) uses `-lopengl32`. – Domi Jun 08 '21 at 04:20
Ubuntu Solution here:
Had the same Issue, what I had to do was just to:
Install
sudo apt-get install libglfw3-dev libgl1-mesa-dev libglu1-mesa-dev freeglut3-dev libglew-dev
As mentioned Here
Then
- (Open vscode) > File > Open Folder > "Select your c++'s project-folder"
- Open your main
.cppfile that you want to compile / (start with) - Then to produce
tasks.jsonpress "F5" > Select "C++ (GDB/LLDB)" > select g++ compiler > stop compiling process - Edit
tasks.jsonby 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"
}
- Save the file and go back to your main
.cppfile
✔️ Press "F5" again and it should Run now.
- 747
- 1
- 6
- 19