0

I've been trying to get a glfw app running on Linux Mint and having a bear of a time of it. I'm sure I'm missing something obvious but I've looked all over the internet. For what it's worth I've used glfw before on Windows with Visual Studio.

Here is my mindblowing code:

#include <stdio.h>
#include "GLFW/glfw3.h"

int main(){
    printf("hello world!\n");
    if (!glfwInit()){
        // Initialization failed
    }
    glfwTerminate();

    return 0;
}

Yep just trying to init glfw.

My bash script:

#!/bin/bash
gcc -Wall -Wextra -g -v -Iinclude -Llib -o morg src/main.c

So when I run that bash script I get this error:

/home/[username]/Documents/morg/src/main.c:6: undefined reference to `glfwInit'
/home/[username]/Documents/morg/src/main.c:9: undefined reference to `glfwTerminate'

Fine, so I'm probably missing some dependencies. Let's try that. I find this SO page and experimentally change the bash script to this:

#!/bin/bash
gcc -Wall -Wextra -g -v -Iinclude -Llib -lglfw3 -lGL -o morg src/main.c

I get a new error /usr/bin/ld: cannot find -lGL. I look that up. I find this where the programmer ended up having a broken symbolic link that they fixed by running sudo apt-get install --reinstall libgl1-mesa-dev. Of course this didn't work for me either. libGL is on my system, I can see it in my files and other OpenGL programs work fine.

Further I have gotten conflicting information about what dependencies glfw requires. For example I came across g++ Test.cpp -lglfw -lGL -lm -lX11 -lpthread -lXi -lXrandr -ldl. The actual glfw docs don't mention any of this from what I can tell. Does anyone have a reference, documentation, a guide, or tutorial to using glfw on GNU Linux. I found this guide which predictably didn't work either.

Is this this hard for everyone else? I am not so slowly losing my mind.

Riley
  • 21
  • 5

1 Answers1

1

Here is a list of things you need to do on a fresh/barebones Linux Mint 20 (Ulyana) installation in order to compile a GLFW/OpenGL program (just tested it with a fresh Linux Mint 20 ISO installation on VMWare Workstation Player 16):

Update system dependencies (just for good measure):

sudo apt update && sudo apt upgrade -y

Install necessary dependencies:

sudo apt install -y build-essential \
                    libglfw3 \
                    libglfw3-dev

Compile (use -lglfw and specify linked libraries AFTER your src/main.c!):

gcc -Wall -Wextra -g -v -Iinclude -Llib src/main.c -lglfw -lGL -o morg

So, there are two errors you did:

  • the GLFW library installed with the libglfw3 package is called libglfw on Linux Mint, and not libglfw3 (see /usr/lib/x86_64-linux-gnu/libglfw.so)
  • any compilation unit or archive that you use to compile with GCC MUST BE specified AFTER symbols from that file have been referenced in previous compilation units or archives. So, you MUST put src/main.c BEFORE you specify -lglfw and -lGL.
httpdigest
  • 5,411
  • 2
  • 14
  • 28