LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-05-2006, 04:15 PM   #1
seriesx4
Member
 
Registered: Mar 2005
Location: Sweden
Posts: 64

Rep: Reputation: 15
Need help completing Makefile


Code:
CXXFLAGS = -O2 -pipe -march=pentium3 -fomit-frame-pointer -c
CXX = g++
SRCS = $(wildcard *.cpp)
OBJS = $(patsubst %.cpp,%.o,$(wildcard *.cpp))
HDRS = $(patsubst %.h,%.o,$(wildcard *.h))

mylib.a: $(OBJS)
	ar cr mylib.a ${OBJS}

clean:
	-rm *.o
	-rm *.a
Problem: When changing .h files the affected .cpp files won't be recompiled.

The .cpp files dependencies on .h files could obviously be pretty hard to resolve and probably includes writing rules "by hand" or using a tool to actually automate the task by scanning the .cpp files.

Is there an elegant solution to rewrite this makefile so that it will take care of all the .h and .cpp files in the directory? Either by using some - for me - unknown feature of make, or by using some other tool?

I don't want to write individual rules for all my files as it's error-prone and time-consuming.
 
Old 01-05-2006, 04:42 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I use KDevelop and the projects will manage the make files for me, add a file to the project it gets added to the make file.

graeme.
 
Old 01-05-2006, 04:53 PM   #3
seriesx4
Member
 
Registered: Mar 2005
Location: Sweden
Posts: 64

Original Poster
Rep: Reputation: 15
Yes, I used to use KDevelop too, but it consists of 95% av stuff I don't understand or need... Perhaps no big deal it sounds, but if something goes wrong it means you won't be able to fix it.

This seems to be the alternative:

http://www.gnu.org/software/make/man...ake.html#SEC51

Last edited by seriesx4; 01-05-2006 at 04:55 PM.
 
Old 01-05-2006, 05:09 PM   #4
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
You define SRCS, HDRS and CXXFLAGS but you never use them...

Add this to your Makefile, maybe it will help:
Code:
%.o: %.cpp %.h
	$(CXX) $(CXXFLAGS) $@ $<

Last edited by Hivemind; 01-05-2006 at 05:11 PM.
 
Old 01-09-2006, 09:46 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
use makedepend to write dependencies.

here's how I do it:

Code:
LIB=${PWD}.a
# ---------------------------------
# Standard makefile template
# for libraries
# shouldn't need to touch anything
#       takes the name from the directory
# ---------------------------------

AT   = @
RM   = ${AT} rm -f
ECHO = ${AT} echo
LS   = ${AT}ls
CC   = gcc
CFLAGS = ${DEBUG_FLAGS} -I ${PWD}


DEPENDFILE  = depend.mk

lib:${LIB}

-include ${DEPENDFILE}
OBJFILES = ${SRCFILES:.c=.o}

${LIB}:${LIB}(${OBJFILES}) tidy 



help:
 ${ECHO} OBJFILES = ${OBJFILES}
 ${ECHO} SRCFILES = ${SRCFILES}
 ${ECHO} LIB = ${LIB}
 nm ${LIB}
 

${DEPENDFILE}:
 ${LS} *.c|xargs echo SRCFILES = > ${DEPENDFILE}
 ${ECHO} '' >> ${DEPENDFILE} 
 makedepend -f $@ *.c
 cp $@ $@.bak

tidy:
 rm -f  ${DEPENDFILE}
 
clean: tidy
 rm -f  *.o ${LIB}  .*.swp
 
  


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
completing missing programs from K3b joel b Linux - Newbie 1 05-03-2005 11:34 PM
how to get (makefile -f makefile )output into the textview widget in Pygtk sailu_mvn Programming 3 02-28-2005 03:57 AM
generate Makefile from Makefile.in without calling ./configure ? chris78 Programming 2 05-02-2004 12:23 PM
Help completing file association bmcneely0 Linux - Newbie 0 08-21-2003 07:28 AM
Help completing URL file association bmcneely0 Linux - General 0 08-18-2003 04:33 PM

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

All times are GMT -5. The time now is 10:44 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