Search code examples
windowsopenglg++cygwinglfw

How to Compile and Link an OpenGL/GLFW Program with Cygwin on Windows?


I struggled with finding documentation on compiling a basic OpenGL/GLFW example Windows using Cygwin-g++ so I decided to post an answer to my own question.

  • OS: Windows (10)
  • Compiler: Cygwin g++ (x86_64-pc-cygwin)
  • Code: GLFW Documentation's Example Code (basic working example shown below. Won't create a window but should output text if everything builds/links correctly):

    #define GLFW_DLL
    #include <GLFW/glfw3.h>
    #include <iostream>
    
    int main(void) {
        std::cout << "Code works" << std::endl;
        if (!glfwInit())
            return -1;
        glfwTerminate();
        return 0;
    }

  • Compilation: g++ -Wall -Iinclude main.cpp -o main.exe -L<folder glfw3.dll is in> -lglfw3 -lopengl32 -lgdi32

Issues

  • Code would compile and build but no output window would appear
  • My current linking is fine, but different combinations would result in undefined reference errors.

Solution

  • alternative solution building the GLFW for Cygwin

    Download glfw-3.3.4.zip and then

    $ unzip glfw-3.3.4.zip
    $ cd glfw-3.3.4
    $ ccmake .
    

    set BUILD_SHARED_LIBS to ON. Configure and generate

    $ make
    $ make install
    
    Install the project...
    -- Install configuration: ""
    -- Up-to-date: /usr/local/include/GLFW
    -- Up-to-date: /usr/local/include/GLFW/glfw3.h
    -- Up-to-date: /usr/local/include/GLFW/glfw3native.h
    -- Up-to-date: /usr/local/lib/cmake/glfw3/glfw3Config.cmake
    -- Up-to-date: /usr/local/lib/cmake/glfw3/glfw3ConfigVersion.cmake
    -- Up-to-date: /usr/local/lib/cmake/glfw3/glfw3Targets.cmake
    -- Up-to-date: /usr/local/lib/cmake/glfw3/glfw3Targets-noconfig.cmake
    -- Up-to-date: /usr/local/lib/pkgconfig/glfw3.pc
    -- Up-to-date: /usr/local/lib/libglfw.dll.a
    -- Up-to-date: /usr/local/bin/cygglfw-3.dll
    

    So you will have a proper Cygwin build installed under /usr/local
    after that, running under X server

    $ g++ main.cpp -o main -lglfw -L/usr/local/lib
    
    $ ./main.exe 
    Code works