LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-28-2005, 09:59 PM   #1
cyb0rg777
Member
 
Registered: Dec 2004
Location: ~
Distribution: Debian
Posts: 143
Blog Entries: 8

Rep: Reputation: 15
basic make file


I have 6 source files. Right now they are included as .h files.My make file looks like this.
Code:
all: mygame

mygame:mygame.o  
	g++ -g `sdl-config --libs` mygame.o -o mygame -lSDL_image -lSDL_mixer

mygame.o:mygame.cpp
	g++ -g -c `sdl-config --cflags` mygame.cpp
This works fine until I edit one of my .h files then I have to resave my main file to recompile.This is what I want to do.
Code:
all: mygame

mygame:mygame.o  scroll.o character.o utilities.o sprites.o
	g++ -g `sdl-config --libs` mygame.o  scroll.o character.o utilities.o sprites.o -o mygame -lSDL_image -lSDL_mixer

mygame.o:mygame.cpp
	g++ -g -c `sdl-config --cflags` mygame.cpp
scroll.o:scroll.cpp
	g++ -g -c `sdl-config --cflags` scroll.cpp
character.o:character.cpp
	g++ -g -c `sdl-config --cflags` character.cpp
utilities.o:utilities.cpp
	g++ -g -c `sdl-config --cflags` utilities.cpp
sprites.o:sprites.cpp
	g++ -g -c `sdl-config --cflags` sprites.cpp
But when I run this I get unresolved reference.Either I'm doing something wrong or I'm missing the point .What good is linking .cpp files if you can't access the functions and data types?
Please dumb it down for me.
 
Old 09-29-2005, 12:38 AM   #2
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Quote:
This works fine until I edit one of my .h files then I have to resave my main file to recompile.
What you should do is make your .h files part of the dependency list...

Code:
all: mygame

mygame:mygame.o  
g++ -g `sdl-config --libs` mygame.o -o mygame -lSDL_image -lSDL_mixer

mygame.o:mygame.cpp includefile1.h includefile2.h
g++ -g -c `sdl-config --cflags` mygame.cpp
As for your problem separating out the pieces, do any of your modules depend on any of your other modules? Be sure to include them in the dependency list if they do, because if (for example) character.o depends on sprite.o, trying to make character.o will fail if sprite.o is not yet built. Beyond that, I don't do enough C++ to help you without more info about the program and the errors you're getting.
 
Old 09-29-2005, 02:48 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
you can use makedepend to build the dependency list for you.
also did you know make has default actions for .c and .cpp files?
no need to expressly define them.

have a look at this - it's a make file I concocted for compiling and linking several C
files into a single target. Simple and effective.

It searches and creates a new dependency file each time. So you never have to touch
it even if you create new source files. Simply change *.c to *.cpp, add the link libraries and it would
work. (I think, maybe )

Code:
TARGET=dummy_name
# ---------------------------------
# Standard makefile template
# just specify the target name above
# and the make file should sort it
# all out (hopefully)
# ---------------------------------

INCLUDEFILE = include.mk

# compiler  options
# ============
# CFLAGS = -g -Wall


# this should appear above the 'include' directive below
# calls for the target and then the cleanup
# =========================================
all: ${TARGET} cleanup 


-include ${INCLUDEFILE}

# ${INCLUDEFILE} should not exist when it's included.
# As it does not exist it is treated as a target.
# Therefore it is created anew each time,
# by the appropriate rule, below
# Which finds .c and .h files and calls makedepend for them
# =========================================================

${INCLUDEFILE}:
 @echo "### creating ${INCLUDEFILE}"
 @echo "### creating ${INCLUDEFILE}" > ${INCLUDEFILE}
 -@ echo  *.c|xargs echo SRCFILES = >> ${INCLUDEFILE}
 -@ echo  *.h|xargs echo HFILES = >> ${INCLUDEFILE}
 @  makedepend  -f${INCLUDEFILE} *.c


# (SRCFILES defined in INCLUDEFILE)
# =================================
OBJFILES = ${SRCFILES:.c=.o}


# this is the working target
# ==========================

${TARGET}: ${OBJFILES} 
 ${CC} -o $@ ${OBJFILES}

help: print_help cleanup

print_help:
 echo  OBJFILES = ${OBJFILES}
 echo  SRCFILES = ${SRCFILES}
 cat ${INCLUDEFILE}
 

clean:cleanup
 -@ rm -f  *.o 

# we need to delete the INCLUDEFILE otherwise
# new source and header files won't be picked up
# next time
# =========
cleanup:
 @echo "### deleting ${INCLUDEFILE}"
 -@ rm -f  ${INCLUDEFILE}
 
Old 09-29-2005, 06:46 AM   #4
cyb0rg777
Member
 
Registered: Dec 2004
Location: ~
Distribution: Debian
Posts: 143

Original Poster
Blog Entries: 8

Rep: Reputation: 15
All of my modules depend on one another.sprite.o depends on character.o for example.I guess what I'm missing is when to use .h and when to use modules.I will use CroMagnons option for now and save bigearsbillys example for later .Thanks to you both.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Make kernel with basic commands itz2000 Linux - General 6 11-21-2005 10:44 AM
Basic things to do to make sure a server is secure? htmlcoder Linux - Security 1 03-21-2005 05:41 AM
I accidentally deleted make file in /usr/local/bin, now cannot use make command.... Niceman2005 Linux - Software 2 11-17-2004 07:55 PM
Some basic File Manipulation commands sureshp1980 Linux - General 2 09-24-2003 08:36 PM
basic file editing dartania Linux - Newbie 1 09-21-2003 02:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration