Miger
Miger

Reputation: 1245

OpenGL C++ with g++ compiler, not able to find GLFW on Windows 10 (no Visual Studio)

I'm trying to setup an OpenGL API from Scratch using C++ with g++ compiler. Currently I'm trying to get a simple windowing system using GLFW but when I compile the program it doesn't seem to find the GFLW directory.

I'm trying to do this from the very scratch, so I'm not going to use any IDE like Visual Studio, everything I do is written with notepad++ and commands using git bash. I want to do it like this and I know this is a lot of extra work but this is what I want to achieve.

My Project Folder Looks like this:

-TestProject
    -main.cpp
    -GFLW
        -gflw3.h

Inside my main.cpp file I've copy pasted the code from the GFLW documentation:

#include <GLFW/glfw3.h>

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

To compile the program I use git bash with this command:

g++ main.cpp -o WindowApp.exe

This should now create an exe which enables me to open a window, nothing more and nothing less. Obviously I'm doing something wrong, for one I get the error message that the directory GFLW couldn't be found. So that's one problem.

Essentially I want to follow this tutorial here ChernoProject but without having to use Visual Studio or any other IDE. I know he is using different files than me and that he does something called Linking however I don't understand how this adapts to my minimalist setup here.

Upvotes: 2

Views: 3054

Answers (1)

VVV
VVV

Reputation: 519

You're including GLFW/glfw3.h relative to compiler search path (with angle brackets) but the header is in your project's directory tree. Compiler doesn't search for system headers (included with angle brackets) in your project tree unless you specifically tell it to do so, by passing -Iinclude_directory argument. So in your case you need either to include the header like this: #include "GLFW/glfw3.h" (recommended), or add -I. argument to tell the compiler to search for system headers in the current directory.

Also regarding the linking - that would be the next problem after you resolve the header issue. Your compile command looks like this: g++ main.cpp -o WindowApp.exe and does not include any libs to link to. If you're using MinGW, it must include following arguments for static linking to OpenGL and GLFW3: -lmingw32 -lglfw3 -lopengl32 -lgdi32 -luser32.

For example, my preferred setup for this project would be:

-ProjectDirectory
    -bin
        -glfw3.dll (if you want to use dll)
    -include
        -GLFW
            -glfw3.h
            -glfw3native.h
    -lib
        -libglfw3.a
        -libglfw3dll.a (if you want to use dll)
    -src
        -main.cpp
    -build.bat

With the following contents of build.bat (change "Your actual path to g++" to the path to g++ on your system, or just set it to "g++" if it's in your path variable):

@ECHO OFF

REM Path to g++
SET G="Your actual path to g++"

SET OBJECTS=
REM Recursive every .cpp file in ./src
FOR /R "./src" %%a IN (*.cpp) DO (
    CALL SET OBJECTS=%%OBJECTS%% "%%a"
)

@ECHO ON

%G% %OBJECTS% -obin/a.exe -Iinclude -Llib -lmingw32 -lglfw3 -lopengl32 -lgdi32 -luser32

Upvotes: 4

Related Questions