I am working on an OpenGL project which makes use of GLFW and OpenGL, however I cannot manage to successfully include GLFW in my project.
I am on Linux Mint, and am using C code compiled via terminal with clang version 10.0.0-4ubuntu1. IDE is Visual Studio Code.
Code:
#include "gui/window.h"
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
}
int windowMain() {
// Initialize GLFW
if (!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
// Create a window and OpenGL context
GLFWwindow* window = glfwCreateWindow(800, 600, "Basic OpenGL Window", NULL, NULL);
if (!window) {
fprintf(stderr, "Failed to create GLFW window\n");
glfwTerminate();
return -1;
}
// Set the window as the current context
glfwMakeContextCurrent(window);
// Set the key callback function
glfwSetKeyCallback(window, key_callback);
// Main loop
while (!glfwWindowShouldClose(window)) {
// Render here
// Swap front and back buffers
glfwSwapBuffers(window);
// Poll for and process events
glfwPollEvents();
}
// Terminate GLFW
glfwTerminate();
return 0;
}
Header file (gui/window.h):
#ifndef WINDOW_H
#define WINDOW_H
#include <stdio.h>
#include <GLFW/glfw3.h>
int windowMain();
#endif // WINDOW_H
IntelliSense fails to find GLFW/glfw3.h, and ld says the following:
/usr/bin/ld: build/gui/window.o: in function `key_callback':
/home/stv/work/c/crss/src/gui/window.c:5: undefined reference to `glfwSetWindowShouldClose'
/usr/bin/ld: build/gui/window.o: in function `windowMain':
/home/stv/work/c/crss/src/gui/window.c:12: undefined reference to `glfwInit'
/usr/bin/ld: /home/stv/work/c/crss/src/gui/window.c:18: undefined reference to `glfwCreateWindow'
/usr/bin/ld: /home/stv/work/c/crss/src/gui/window.c:21: undefined reference to `glfwTerminate'
/usr/bin/ld: /home/stv/work/c/crss/src/gui/window.c:26: undefined reference to `glfwMakeContextCurrent'
/usr/bin/ld: /home/stv/work/c/crss/src/gui/window.c:29: undefined reference to `glfwSetKeyCallback'
/usr/bin/ld: /home/stv/work/c/crss/src/gui/window.c:32: undefined reference to `glfwWindowShouldClose'
/usr/bin/ld: /home/stv/work/c/crss/src/gui/window.c:36: undefined reference to `glfwSwapBuffers'
/usr/bin/ld: /home/stv/work/c/crss/src/gui/window.c:39: undefined reference to `glfwPollEvents'
/usr/bin/ld: /home/stv/work/c/crss/src/gui/window.c:43: undefined reference to `glfwTerminate'
As far as I can tell, the linker cannot find the correct files to include, but I am pretty bad at configuring projects like this and have no idea how to fix this.
I installed glfw using the following commands:
sudo apt-get update
sudo apt-get install libglfw3 libglfw3-dev
and also tried messing around with different flags, the flags in my makefile (I use make to compile and run everything) are: -lGL -lglfw3 -lm -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl however clang also complains that these linker inputs are ‘unused’. I’m not quite sure how to correctly set up this kind of project, and since most online tutorials are c++, I’ve been having a hard time finding tutorials for OpenGL using generic C.
Thank you for your time.
>Solution :
You can use pkg-config to see which libraries you need to link for a given package:
$ pkg-config --libs glfw3
-lglfw
So you need to link with -lglfw.