Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup Visual Studio Code with OpenGL?

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.

like image 337
Matthew Avatar asked Jan 10 '17 20:01

Matthew


People also ask

How do I add OpenGL code to Visual Studio?

In Visual Studio, right-click on your project, and select "Open Folder in File Explorer...". A folder will appear that contains the files for your project. Drag the openGL files from the folder you created in step 1, into this folder.

Can I run OpenGL in Visual Studio Code?

Windows comes with OpenGL, and Visual Studio comes with the OpenGL libraries, but neither of them comes with GLUT. Get the newest version of GLUT here: GLUT 3.7. 6 for Windows.

How do I run OpenGL in C++?

1.2 Writing Your First OpenGL ProgramCreate a new C++ project: Select "File" menu ⇒ New ⇒ Project... ⇒ C/C++ ⇒ C++ Project ⇒ Next. In "Project name", enter " Hello " ⇒ In "Project type", select "Executable", "Empty Project" ⇒ In "Toolchain", select "Cygwin GCC" or "MinGW GCC" (depending on your setup) ⇒ Next ⇒ Finish.


1 Answers

OpenGl & C/C++ & VSCode

Came across the same problem. Note that there are two issues here:

  1. How to setup the launch.json and tasks.json files?
    • Start with the official VSCode documentation on C++ programming with VSCode
    • The tasks documentation explains how to setup and run tasks in greater detail.
  2. 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:

  1. Fix your include file names
    • Not everything is in <gl/*> (e.g. Mac has <OpenGL/*> for part of it, and <GLUT/*> for glut files)
  2. In order to run, add 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
  3. Don't forget: Add those 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! :)

More Details

The above is just a shorter version of my overall journey, detailed below:

  1. I also started with the official VSCode instructions on C++ programming with VSCode (which you also mentioned).
  2. Then I followed their link at the bottom: Using Clang in Visual Studio Code (the GCC setup is very similar).
  3. Followed this Github Gist on the differences between Mac and non-Mac OpenGl compilation. There are two major differences:
    1. The APPLE paths are slightly different; not everything is in <gl/*> (e.g. <OpenGL/*> for core files and <GLUT/*> for glut files).
    2. You have to add OpenGL and other libraries via the framework flag.
  4. Once you took care of that:
    1. Add those frameworks to your C/C++ extension settings as well
    2. (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)
  5. If you have not done so before, you probably want to brew install glew and brew install glfw and/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:

  1. Compile OpenGl on Linux
  2. Compile OpenGl with GCC on Windows

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.

like image 113
Domi Avatar answered Oct 19 '22 01:10

Domi