Search code examples
c++makefileincludemingw-w64include-path

Include third-party header and library using makefile and VS Code


In a university course I am attending, we are starting to work with OpenGL. The professor made us download GLAD and GLFW, but we didn't see how to include and make everything work in VS Code using the makefile (we use the makefile and the mingw-w64 compiler on Windows 10 64 bit). I'm writing a simple code following an OpenGL guide:

#include "glad/glad.h"
#include "glfw3.h"

int main(int argc, char* argv[])
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    return 0;
}

The GLAD header file is located in:

C:\libs\Glad\include\glad

The GLFW library is located in:

C:\libs\GLFW\glfw-3.3.2.bin.WIN64\lib-mingw-w64

and in this directory there are 3 files:

glfw3.dll

libglfw3.a

libglfw3dll.a

In the GLFW directory there is also an "include" folder:

C:\libs\GLFW\glfw-3.3.2.bin.WIN64\include\GLFW

with two header files:

glfw3.h

glfw3native.h

The first thing I did in my C ++ project in VS Code, was to modify the "c_cpp_properties.json" file, including the relative paths of the files that I included in the .cpp. I also included the path to the library, although in the end, in the code, I only included the header file:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:/libs/Glad/include/**",
                "C:/libs/GLFW/glfw-3.3.2.bin.WIN64/include/GLFW/",
                "C:/libs/GLFW/glfw-3.3.2.bin.WIN64/lib-mingw-w64/"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\MinGW-64\\mingw64\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++20",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

At this point, for the makefile, I copied and pasted a makefile that showed us the professor, simply for compiling the .cpp, linking the .o and the final result, the .exe. We have never seen what to write in a makefile in such a situation (I'm pretty sure the problem is in the makefile) and in addition we have not studied the makefile long enough to be able to go it alone. This is the makefile:

CC = g++
DEBUG = -g -std=c++2a
CFLAGS = -Wall -c $(DEBUG)
LFLAGS = -Wall $(DEBUG)
ODIR = obj
OBJS = $(ODIR)/First_Window.o

LINK_TARGET = First_Window.exe

all : $(LINK_TARGET)
    echo All done

$(LINK_TARGET) : $(OBJS)
    $(CC) $(LFLAGS) $^ -o $@

$(ODIR)/%.o : %.cpp
    $(CC) $(CFLAGS) $< -o $@

Compiling from terminal:

g++ -Wall -c -g -std=c++2a First_Window.cpp -o obj/First_Window.o
First_Window.cpp:1:10: fatal error: glad/glad.h: No such file or directory
 #include "glad/glad.h"
          ^~~~~~~~~~~~~
compilation terminated.
mingw32-make: *** [makefile:17: obj/First_Window.o] Error 1

All the guides I have found, even in the same book that I am following, refer to VS Community, but we have used VS Code since the beginning of the course. What should I modify/add to solve the problem?

Thank you in advance


Solution

  • Oh I just saw where the glad folder is, so try this to get past the first error:

    CC = g++
    DEBUG = -g -std=c++2a
    CFLAGS = -Wall -c $(DEBUG)
    LFLAGS = -Wall $(DEBUG)
    ODIR = obj
    OBJS = $(ODIR)/First_Window.o
    INCLUDE = -IC:\libs\Glad\include
    
    LINK_TARGET = First_Window.exe
    
    all : $(LINK_TARGET)
        echo All done
    
    $(LINK_TARGET) : $(OBJS)
        $(CC) $(LFLAGS) $^ -o $@
    
    $(ODIR)/%.o : %.cpp
        $(CC) $(INCLUDE) $(CFLAGS) $< -o $@
    

    Where I added the include path for the glad folder. You may also add any other header paths:

    INCLUDE = -IC:\libs\Glad\include -IC:\libs\GLFW\glfw-3.3.2.bin.WIN64\include
    

    As I mentioned in my comments you will probably get a linker error when you get to the link line. Use the linker options -l and -L:

    LIBS = -LC:\libs\GLFW\glfw-3.3.2.bin.WIN64\lib-mingw-w64 -lglfw3 -llibglfw3 -llibglfw3dll
    

    Then add that to your link line:

    $(LINK_TARGET) : $(OBJS)
        $(CC) $(LFLAGS) $(LIBS) $^ -o $@
    

    Note: for windows I am not 100% sure if you need to omit the "lib" in the library names so it might be this:

    LIBS = -LC:\libs\GLFW\glfw-3.3.2.bin.WIN64\lib-mingw-w64 -lglfw3 -lglfw3 -lglfw3dll
    

    But then it looks like you have a mix of static libs and dynamic libs - so I am not really sure which specific libs you want to link to so probably you can try them one at a time until you find the libs you need. I am going to guess that the final line you really want is just:

    LIBS = -LC:\libs\GLFW\glfw-3.3.2.bin.WIN64\lib-mingw-w64 -lglfw3