简体   繁体   中英

How to “make” an SDL project on linux?

Makefiles are quite confusing to me. I am attempting to "make" my project that I have worked on in Windows. The confusing part is actually constructing the make file from scratch. I would am trying to also link to the SDL2 library, and that is in a '.a' format.

Here is my code for the make file so far, I have tried multiple versions, and this is the latest:

CXX = gcc
OUT = Engine
SRC =Software-Rendering/src/
SDL_INCLUDE_DIR =Software-Rendering/lib/SDL2/include/SDL/
LIB_DIR =Software-Rendering/lib/SDL2/x86/linuxLib/

SDL = -l${LIB_DIR}libSDL -l${LIB_DIR}/libSDL2main

CPP_FILES =Bitmap.cpp Main.cpp Vector3.cpp Window.cpp
H_FILES =Bitmap.h ErrorReport.h Vector3.h Window.h

O_FILES = Bitmap.o ErrorReport.o Main.o Vector3.o Window.o

all: $(OUT)

$(OUT): $(O_FILES)
    $(CXX) -o $@ $^ ${SDL}

#Making all of the object files down
$(O_FILES): $(H_FILES)
    $(CXX) -c $(CPP_FILES)
#Make sure we can easily clean up the directory
clean:
    rm -f Engine ${O_FILES}

clean_obj:
    rm -f ${O_FILES}

I decided to put the ".a" files in a special directoy in my project so whenever someone clones my repository on github all of the files for compiling and linking are already there.

Why isn't this working and how can I make it work?

Your library linking directive are wrong -- -l prefixes lib to the name you specify, and then searches through the libdir path set by the -L options. So what you want is something like:

SDL = -L$(LIB_DIR) -lSDL -lSDL2main

You can make it clearer/more standard by using the standard varnames for libraries:

LDFLAGS = -L$(LIB_DIR)
LDLIBS = -lSDL -lSDL2main

$(OUT): $(O_FILES)
        $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@

Also, get rid of the explicit command to compile source files -- the default built in rule is fine and easier to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM