Search code examples
c++openglmingwglfwglew

How to link GLEW and GLFW and OpenGL to MingW's g++


I am doing a course on OpenGL.

So I have run my project in Visual Studio 2019 and it works.

What I needed to do in Visual Studio was:

  1. Add GLEW's and GLFW's additional include directories. (when you download the libraries it is simply the location of the include file)
  2. Add GLEW's and GLFW's additional library directories. (lib folder locations)
  3. Add the additional dependencies. (opengl32.lib, glew32.lib, glfw3.lib)
  4. Copy and paste GLEW.dll into the project files, next to main.cpp.

And I run my code and it works.

But I don't want to use Visual Studio 2019, I would much rather work in Notepad++ or VSCode, what I really want is to just make a project out of pure notepad and compile it with the new Windows Terminal.

My question: How can I do all of the things I did in visual studio 2019 from Power Shell?

I heard that MingW comes with its own power shell or command prompt, I imagine I need to find it and give it some commands to link everything up before I start coding.

Where do I find MingW's power shell equivalent and what commands do I need to type?

I have been stuck on this for ages now and could not find any information about it online. (except for what -L and -l does, but these are not power shell commands)


Solution

  • The MingGW and mingw-w64 compilers are Windows commandline tools. They are Windows ports of (some of) the GCC compilers. tdm-gcc is yet another choice.

    Commandine tools for any operating system don't need their own shell. You can run them in any shell you've got on that operating system. On Windows today you have a choice of at least PowerShell and the old CMD shell.

    The MinGW project provides, as well as its GCC toolchain, a minimal unix-like environment for Windows called MSYS, which includes a shell. You don't need MSYS to run the compiler.

    As long as the GCC tools can be located in the value of the PATH environment variable that is operative in the shell at compiletime, then you run the compiler at the shell prompt:

    >gcc [options...]
    >g++ [options...]
    

    the same way it is run on any operating system. If you want to work with a GCC toolchain then the question:

    How can I do all of the things I did in visual studio 2019 from Power Shell?

    is simply the question, How do you run GCC? That's a question of sweeping generality. You need to study relevant books and documentation

    Very sketchily, if you want to compile and link a C++ program that has source files main.cpp and other.cpp and depends on libraries foo and bar that have their C++ APIs defined in header files and are implemented in DLLs, you will do it with commands of the following form:

    To compile the source files to object files:

    >g++ -c -o main.obj main.cpp -I/path/to/foo/header/files -I/path/to/bar/header/files [any other compilation options....]
    >g++ -c -o other.obj other.cpp -I/path/to/foo/header/files -I/path/to/bar/header/files [any other compilation options....]
    

    To link the object files and libraries to make an excutable program:

    >g++ -o prog main.obj other.obj -L/path/to/foo_dll -L/maybe/a/different/path/to/bar_dll -lfoo -lbar [any other linkage options...]
    

    And if all that is successful then the program will be prog.exe and you can run it:

    >prog
    

    just like you ran g++, provided that foo.dll and bar.dll can be found at runtime by the OS loader's DLL search protocol

    As I think you appreciate, in real life nobody builds programs by typing the all the commands in a shell except for instructional purposes. They use a build system or an IDE to automate it. But it is true that building programs though the medium of a build system or IDE presents fewer difficulties if you do it with a basic grasp of how the tools behind it work.