Will-Opare
Will-Opare

Reputation: 73

file format not recognized; treating as linker script

I am trying to compile and my project using this make file:

GLFLAGS=-lGL -lGLU -lX11 -lXxf86vm -lXrandr -lpthread -lxi
CC=g++

window.o: window.h window.cpp
    $(CC) -c $< -o $@ $(GLFLAGS)
main.o: window.h main.cpp
   $(CC) -c $< -o $@ $(GLFLAGS)
all: window.o main.o
   $(CC) $^ -o main

but I get this error:

/usr/bin/ld:window.o: file format not recognized; treating as linker script
/usr/bin/ld:window.o:1: syntax error
collect2: error: ld returned 1 exit status

both main.cpp and window.cpp are dependent on a class I made in window.h.

Upvotes: 1

Views: 8044

Answers (1)

Chnossos
Chnossos

Reputation: 10486

Your flags are not used in the right place. -l flags are linker flags and used only at link-time.

Using the right variables, and adding some to clarify, you can change your makefile to :

EXE     :=  main
SRC     :=  main.cpp window.cpp
OBJ     :=  $(SRC:.cpp=.o)
LDLIBS  :=  -lGL -lGLU -lX11 -lXxf86vm -lXrandr -lpthread -lxi

.PHONY: all

all:    $(EXE)

$(EXE): $(OBJ)
    $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@

If you need auto-dependencies generation, add this :

EXE     :=  main
SRC     :=  main.cpp window.cpp
OBJ     :=  $(SRC:.cpp=.o)
DEP     :=  $(OBJ:.o=.d)

LDLIBS      :=  -lGL -lGLU -lX11 -lXxf86vm -lXrandr -lpthread -lxi
CPPFLAGS    :=  -MMD -MP

.PHONY: all

all:    $(EXE)

$(EXE): $(OBJ)
    $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@

-include $(DEP)

Upvotes: 1

Related Questions