Wersin
Wersin

Reputation: 11

How to include SDL in your program (Linux). undefined reference to SDL_Init()

Im trying everything to run this test Program and keep getting this error messages:

g++ objects/src_files/display.o objects/src_files/main.o -o program -L/usr/local/lib
objects/src_files/main.o: In function `main':
main.cpp:(.text+0x16): undefined reference to `SDL_Init'
collect2: error: ld returned 1 exit status
make: *** [program] Error 1

this is how my test program looks like:

#include <iostream>
#include <string>
#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <glm/glm.hpp>
#include <display.hpp>

using namespace std;

int main(int argc, char **argv)
{
        SDL_Init(SDL_INIT_EVERYTHING);
        glm::vec3 my_v(0, 0, 0);
        string s  = "This is a Test";
        cout << s << endl;


        return 0;
}

and this is my MAKEFILE:

APP = program

CC = g++

SRCDIR  = src_files
OBJDIR  = objects

H_DIR = header_files
INC_DIR = include
LIB_DIR = /usr/local/lib

SRCS    := $(shell find $(SRCDIR) -name '*.cpp')
SRCDIRS := $(shell find . -name '*.cpp' -exec dirname {} \; | uniq)
OBJS    := $(patsubst %.cpp,$(OBJDIR)/%.o,$(SRCS))

CFLAGS  = -Wall -pedantic -ansi -lSDL
LDFLAGS = 

#update here
_H = $(HD_DIR)/display.hpp 
SDL_H = $(INC_DIR)/SDL.h
MAIN_H = $(_H) $(SDL_H)

all: $(APP)

$(APP) : buildrepo $(OBJS)
    $(CC) $(OBJS) $(LDFLAGS) -o $@

$(OBJDIR)/%.o: %.cpp
    $(CC) $(CFLAGS) -I$(INC_DIR) -I$(H_DIR) -c $< -o $@

#update here
$(OBJ_DIR)/main.o: $(MAIN_H)
$(OBJ_DIR)/display.o: $(_H) $(SDL_H)

clean:
    $(RM) $(OBJS)

distclean: clean
    $(RM) $(APP)

buildrepo:
    @$(call make-repo)

define make-repo
   for dir in $(SRCDIRS); \
   do \
    mkdir -p $(OBJDIR)/$$dir; \
   done
endef

I run the MAKEFILE with: make -f MAKEFILE What else can I try?

Thanks

Upvotes: 1

Views: 1318

Answers (1)

GreenScape
GreenScape

Reputation: 7745

In your Makefile change

CFLAGS  = -Wall -pedantic -ansi -lSDL
LDFLAGS = 

To

CFLAGS  = -Wall -pedantic -ansi 
LDFLAGS = -lSDL

-lSDL is actually a linker flag, not compiler.

Upvotes: 2

Related Questions