0

I am compiling a program that uses GLFW, during compilation the linker gives me undefined references to functions originating from the math.h library.

I added the -lm option to my linker, and I still get the same error after recompiling. I'm not sure what I'm doing wrong.

I have tried putting -lm at the end as described in Undefined reference to `sqrt` despite linking to math library, However this does not fix my issue.

I'm using these linker flags in this order.

-lglfw -lGLEW -lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor -ldl -lGL -lGLU -lpthread -lm

Output:

gcc -lglfw -lGLEW -lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor -ldl -lGL -lGLU -lpthread -lm -Ilib/glad/include/glad -Ilib/glfw/include/GLFW -Wall -g -Werror -Wextra  -c -o obj/main.o src/main.c
gcc -lglfw -lGLEW -lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor -ldl -lGL -lGLU -lpthread -lm -Ilib/glad/include/glad -Ilib/glfw/include/GLFW -Wall -g -Werror -Wextra  -c -o obj/window.o src/window.c
gcc -Wall -g -Werror -Wextra  -o bin/main obj/main.o obj/window.o lib/glad/bin/glad.o lib/glfw/build/src/libglfw3.a
/usr/bin/ld: lib/glfw/build/src/libglfw3.a(monitor.c.o): in function `glfwSetGamma':
monitor.c:(.text+0x1330): undefined reference to `powf'
/usr/bin/ld: lib/glfw/build/src/libglfw3.a(null_monitor.c.o): in function `_glfwGetGammaRampNull':
null_monitor.c:(.text+0x3ea): undefined reference to `powf'
/usr/bin/ld: lib/glfw/build/src/libglfw3.a(x11_monitor.c.o): in function `calculateRefreshRate':
x11_monitor.c:(.text+0xf4): undefined reference to `round'
collect2: error: ld returned 1 exit status
make: *** [Makefile:31: bin/main] Error 1

Edit: I have attached my Makefile for further clarification.


CC=gcc
CFLAGS=-Wall -g -Werror -Wextra
LDFLAGS=-lglfw -lGLEW -lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor -ldl -lGL -lGLU -lpthread -lm
# LDFLAGS CONTINUED
LDFLAGS += -Ilib/glad/include/glad -Ilib/glfw/include/GLFW
CCOBJFLAGS=$(CFLAGS) -c

# directories
BIN_PATH=bin
OBJ_PATH=obj
SRC_PATH=src

# source
SRC=$(foreach x, $(SRC_PATH), $(wildcard $(addprefix $(x)/*,.c*)))
OBJ=$(addprefix $(OBJ_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC)))))
LIBS=lib/glad/bin/glad.o lib/glfw/build/src/libglfw3.a

# specify target
TARGET_NAME=main
TARGET=$(BIN_PATH)/$(TARGET_NAME)

# cleaning rules
CLEAN_LIST=$(TARGET) $(OBJ)

# make section
default: makedir all


$(TARGET): $(OBJ)
    $(CC) $(CFLAGS) -o $@ $(OBJ) $(LIBS)

$(OBJ_PATH)/%.o: $(SRC_PATH)/%.c*
    $(CC) $(LDFLAGS) $(CCOBJFLAGS) -o $@ $<

run: all
    $(TARGET)

libs:
    cd lib/glfw && cmake -S . -B build && cd build/ && make                     # GLFW
    cd lib/glad && $(CC) -o bin/glad.o -Iinclude -c src/glad.c && ar rcs libglad.a bin/glad.o   # GLAD
    
# phony
.PHONY: makedir
    @mkdir -p $(BIN_PATH) $(OBJ_PATH)

.PHONY: all
all: $(TARGET)

.PHONY: clean
clean:
    @echo CLEAN $(CLEAN_LIST)
    @rm -f $(CLEAN_LIST)

Any feedback on the makefile is appreciated.

  • your gcc command line references `lib/glfw/build/src/libglfw3.a` – jdigital Dec 25 '22 at 03:06
  • When I remove the static library `lib/glfw/build/src/libglfw3.a` it gives me undefined references to GLFW functions. – brokenlocks Dec 25 '22 at 03:28
  • 4
    Have you tried to add -lm to the very end of the command line where you are trying to link your object files and the static library? – Avi Berger Dec 25 '22 at 04:05
  • 1
    You have the linker flags on the compile command lines (where they're ignored) and are missing them on the link command line. You also have your compile options (like -g and -Wall) on the link command where they'll be ignored, and not on the compile command lines where they should be. – Chris Dodd Dec 25 '22 at 08:21

0 Answers0