Using a makefile to compile a program using SDL

This is a discussion on Using a makefile to compile a program using SDL within the Game Programming forums, part of the General Programming Boards category; Hello I am trying to learn SDL from this tutorial ( http://lazyfooproductions.com/SDL_tu...on02/index.php ) but I am having some troubles compiling ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    6

    Question Using a makefile to compile a program using SDL

    Hello

    I am trying to learn SDL from this tutorial ( http://lazyfooproductions.com/SDL_tu...on02/index.php ) but I am having some troubles compiling with make.

    If i just use: "g++ -lSDL main.cpp -o play" the program works fine.

    But if I instead use this makefile the program quits with a Segmentation fault

    Code:
    CXX = g++
    
    LDFLAGS = lSDL
    
    OUTPUT = play
    
    sources = main.cpp 
    
    OBJS = ${sources:.cpp=.o}
    
    ${OUTPUT}:${OBJS}
    	${CXX} -${LDFLAGS} ${OBJS} -o ${OUTPUT}
    If I edit my makefile to this:
    Code:
    CXX = g++
    
    LDFLAGS = lSDL
    
    OUTPUT = play
    
    sources = main.cpp 
    
    OBJS = ${sources:.cpp=.o}
    
    ${OUTPUT}:${OBJS}
    	${CXX} -${LDFLAGS} ${sources} -o ${OUTPUT} #using sources instead of OBJS
    the program runs with no problems. So I think it has something to do with the *.o file but I have no idea what.

    Does anyone know why the program gives a seg-fault when it is compiled using *.o files?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    Do you have a log of the commands executed during the make process?

    And is it make which crashes, or the IDE, or the program you managed to create?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    It is the program that crashes. It compiles with no problems. It makes no difference if I run make from within my IDE (emacs) ot just in a terminal. What I think is wired is that it only happends when I use .o files.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    Does

    g++ -c main.cpp
    g++ -lSDL main.o -o play

    also crash?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    OK now I am confused.
    Your command works fine and after I ran it my makefile suddenly started working. The manual says that the -c flag compiles the file without linking it.
    How come running it suddenly made my makefile work?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > How come running it suddenly made my makefile work?
    The only thing I can think of is there is something messed up with file time stamps, and you were compiling with an older .o file than you expected.

    I also noticed with the OBJS form, that since you don't directly specify the name of the source file, it goes looking through the implicit rules looking for something to create a .o file with.

    On my system, I saw
    cc -c -o play.o play.c
    rather than
    g++ -c -o play.o play.cpp

    So if you have a main.c and a main.cpp, the implicit rule favours the C source over the C++ source.

    This is what I mean by a makefile log
    Code:
    $ make -f Makefile
    g++    -c -o play.o play.cpp
    g++ -lm play.o -o play
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    I am a Little confused. In the line "sources = main.cpp" I would say that I specify the source file.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    Your makefile is correct.

    Check your directory, do you have play.c AND play.cpp

    Post your makefile log.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    I have never had main.c. Emacs creates a file named main.cpp~ but it has never given any problems before. And even if it is there make works fine now.

    Code:
    make -f Makefile
    g++    -c -o main.o main.cpp
    g++ -lSDL main.o  -o play

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    6
    I don't understand why but it seems to be working just fine now.

    Salem >> Thank you very much for your time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help newbie compile program!
    By candysandra88 in forum C Programming
    Replies: 7
    Last Post: 12-13-2008, 11:27 PM
  2. Compile Public Domain Pocket PC C Program
    By m1l in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 07-20-2007, 05:02 AM
  3. C program compile but not doing the calculations
    By abs.emailverify in forum C Programming
    Replies: 8
    Last Post: 11-08-2006, 08:43 AM
  4. Replies: 5
    Last Post: 11-27-2005, 09:50 PM
  5. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21