Error when dynamically and statically linking glfw to a project via gfortran and mingw

I am trying to create a windows gui application with gfortran and glfw but I have errors when trying to link the dll or the a file for glfw mingw.

I have 2 fortran files, glfw.f90 and Test.f90.

glfw.f90:

module glfw

    ! Functions
    interface
        function glfwInit() result(init_val) bind(C, name='glfwInit')
            use ISO_C_BINDING

            integer(kind=c_int) :: init_val

        end function     

        function glfwCreateWindow(width, height, title, monitor, share) result(window) bind(C, name='glfwCreateWindow')
            use ISO_C_BINDING

            integer(kind=c_int), intent(in) :: width
            integer(kind=c_int), intent(in) :: height
            character(kind=c_char), intent(in) :: title
            integer(kind=c_long), intent(in) :: monitor
            integer(kind=c_long), intent(in) :: share
            integer(kind=c_long) :: window

        end function

        function glfwWindowShouldClose(window) result(shouldClose) bind(C, name='glfwWindowShouldClose')
            use ISO_C_BINDING
            
            integer(kind=c_long), intent(in) :: window
            integer(kind=c_int) :: shouldClose

        end function
    end interface
    
    ! Subroutines
    interface
        subroutine glfwTerminate() bind(C, name='glfwTerminate')
            use ISO_C_BINDING
        end subroutine

        subroutine glfwMakeContextCurrent(window) bind(C, name='glfwMakeContextCurrent')
            use ISO_C_BINDING

            integer(kind=c_long), intent(in) :: window

        end subroutine

        subroutine glfwPollEvents() bind(C, name='glfwPollEvents')
            use ISO_C_BINDING
        end subroutine
    end interface

end module glfw

Test.f90:

program Test

    use glfw

    use ISO_C_BINDING

    implicit none

    integer(kind=c_long) :: window

    integer(kind=c_int) :: null = 0

    if (glfwInit().eq.null) then
        call exit(-1)
    endif

    window = glfwCreateWindow(640, 480, 'Test', 0, 0)
    if (window.eq.null) then
        call glfwTerminate()
        call exit(-1)
    endif

    call glfwMakeContextCurrent(window)

    do
        call glfwPollEvents()
    end do

    call glfwTerminate()

    call sleep(10)

    call exit(0)
end program Test

And when I try compiling the files into an object or exe I get this error (doesnt matter whether it is dynamic or static I get the same error):

libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x127): undefined reference to `CreateDCW@16'
libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x160): undefined reference to `GetDeviceCaps@8'
libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x179): undefined reference to `GetDeviceCaps@8'
libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x1ca): undefined reference to `GetDeviceCaps@8'
libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x24b): undefined reference to `GetDeviceCaps@8'
libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x291): undefined reference to `DeleteDC@4'
libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x9ee): undefined reference to `GetDeviceCaps@8'
libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0xa07): undefined reference to `GetDeviceCaps@8'
libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0xe9f): undefined reference to `CreateDCW@16'
libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0xeba): undefined reference to `GetDeviceGammaRamp@8'
libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0xec8): undefined reference to `DeleteDC@4'
libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x100e): undefined reference to `CreateDCW@16'
libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x1029): undefined reference to `SetDeviceGammaRamp@8'
libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x1037): undefined reference to `DeleteDC@4'
libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x1f3): undefined reference to `CreateDIBSection@24'
libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x262): undefined reference to `CreateBitmap@20'
libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x28d): undefined reference to `DeleteObject@4'
libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x37b): undefined reference to `DeleteObject@4'
libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0x389): undefined reference to `DeleteObject@4'
libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0xb55): undefined reference to `CreateRectRgn@16'
libglfw3.a(win32_window.c.obj):win32_window.c:(.text+0xc2c): undefined reference to `DeleteObject@4'
libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x6c4): undefined reference to `DescribePixelFormat@16'
libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0xbbd): undefined reference to `DescribePixelFormat@16'
libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0xf46): undefined reference to `SwapBuffers@4'
libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x1254): undefined reference to `ChoosePixelFormat@8'
libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x126f): undefined reference to `SetPixelFormat@12'
libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x1592): undefined reference to `DescribePixelFormat@16'
libglfw3.a(wgl_context.c.obj):wgl_context.c:(.text+0x15d9): undefined reference to `SetPixelFormat@12'
collect2.exe: error: ld returned 1 exit status

And this is the error I get when I just try and turn glfw.f90 into an object (doesn't matter whether it is dynamic or static):

libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status

The errors go away when I add -mwindows to the gfortran command but the exe created doesnt run, I double click to open it and nothing happens.



Read more here: https://stackoverflow.com/questions/67010688/error-when-dynamically-and-statically-linking-glfw-to-a-project-via-gfortran-and

Content Attribution

This content was originally published by chickenfan at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: