Ld Does Not Link Opengl on Linux

ld does not link OpenGL on Linux

It really is a FAQ (and has been asked and answered many times here). Order of arguments matters when linking. Libraries should go last (and in the good order). You should run

gcc -Wall -g light.c -lGL -lGLU -lglut -o light 

OpenGL library not linking

You need to add also GL and GLU libraries, try adding them the same way.

Ld undefined reference to OpenGL

It's hard to diagnose your problem without the minimal reproducible example. But what I see is that your system has both libGL and libOpenGL. This may mean that libGL is just a proxy for libglvnd and doesn't contain any of the GL API functions.

But you shouldn't rush to link directly to libOpenGL. Different systems may be configured differently. Instead, the correct way of locating OpenGL objects with CMake is to use find_package(OpenGL) and then include OpenGL::GL in your target_link_libraries.

Example dummy project:

  • CMakeLists.txt
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project(demo)
find_package(OpenGL)
add_executable(prog test.cpp)
target_link_libraries(prog OpenGL::GL)
  • test.cpp
#include <GL/gl.h>

int main()
{
glLoadIdentity();
}

OpenGL program failing to link?

Try moving the libgasandbox.a before all the -l options on the command line. So your command would look like this:

g++ -g -O2 -o chap1ex1 chap1ex1.o ../../libgasandbox/libgasandbox.a -lGL -lGLU -lglut

Order of arguments does matter for static linking, as described in this answer: things that depend on a library must come before that library. libgasandbox evidently depends on GLU, so putting it earlier should solve that error.

You might also need to move -lGL to the very end, if GLU or glut depend on it (I'm not sure whether they do).

Opengl linux undefined reference to basic functions

The order in which you specify the objects you want to link to (including static and dynamic libraries) can matter.

Try with:

g++ Driver.cpp -lGL -lGLU -lglut  -o a

(Not sure about the order of the libs, but that looks ok.)

The idea when you build your command line is that if a requires a symbol from b, b must appear after a in the command line.

The link order problem happens (or not) with GCC/ld for shared libraries depending on (most likely among other things - I'm no expert here) whether the --as-needed link flag is set or not. (See for instance the before-last item in Gentoo's as-needed transition guide.)

The linking process eliminates un-needed symbols ASAP when --as-needed is active, which causes problems if the link order is not "correct". This is done to reduce the number of un-necessary dependencies present in final executables.

This doesn't happen (or less so) if --as-needed is not active - all symbols are kept in that case, and link order doesn't matter as much (more or less - again, I'm no expert.)

Since different distributions use different defaults for that flag, the behavior of GCC might seem inconsistent, but that's just an impression.

I get linker errors when trying to compile my opengl project using gcc

I suggest to compile with a command similar to

gcc -Wall -Wextra -g $(pkg-config --cflags opengl glfw3) main.c \
$(pkg-config --libs opengl glfw3) -o prog

Of course (before trying that) you need to install OpenGL related libraries, probably using (as root) the following command

aptitude install libopengl-dev libglfw3-dev

Be sure to read the documentation of GCC and the documentation of GDB and the documentation of binutils and the documentation of pkg-config.

PS. You could get inspiration from the source code of existing open source projects, such as GNU emacs or RefPerSys.

NB. You could contact me by email : basile@starynkevitch.net (home) or basile.starynkevitch@cea.fr (office, at CEA, LIST ....) near Paris in France.

OpenGL on Arch Linux - Linker Error: Undefined Reference

Your compilation is not complete, you only try to compile one file, whereas there is several.

You can use theses command to compile :

# compile the C file
gcc -c -Wall glad.c -I lib/include/

# compile the c++ files
g++ -c -Wall -I lib/include EBO.cpp
g++ -c -Wall -I lib/include main.cpp
g++ -c -Wall -I lib/include shaderClass.cpp
g++ -c -Wall -I lib/include VAO.cpp
g++ -c -Wall -I lib/include VBO.cpp

# link the object files
g++ -o demo EBO.o glad.o main.o shaderClass.o VAO.o VBO.o -lglfw -ldl

Or you could use some Makefile:

CFLAGS= -C -Wall  -I lib/include
CXXFLAGS= -C -Wall -I lib/include
LDFLAGS= -lglfw -ldl

CC=gcc
CXX=g++

all: demo

.PHONY: clean

clean:
rm *.o

glad.o: glad.c
$(CC) $(CFLAGS) $< -c -o $@

%.o: %.c
$(CXX) $(CXXFLAGS) $< -c -o $@

demo: EBO.o glad.o main.o shaderClass.o VAO.o VBO.o
$(CXX) $^ -o $@ $(LDFLAGS)

and type make to compile

Error when linking OpenGL program

Looks like you need to link to the math library explicitly with -lm.



Related Topics



Leave a reply



Submit