Make file trouble

This is a discussion on Make file trouble within the C Programming forums, part of the General Programming Boards category; Hi I'm trying to figure out how to make the makefile for the following. I have bookie.c and client.c which ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    101

    Question Make file trouble

    Hi

    I'm trying to figure out how to make the makefile for the following.
    I have bookie.c and client.c which should be compiled into separated executables.
    I also have one helper.h and helper.c that are needed in both booie.c and client.c

    How should I go about for the makefile?
    Is it possible to compile the whole thing in just one make file?
    How do put the linkage of the header in the makefile?

    So far my makefile is:
    Code:
    FLAGS  = -Wall -g -std=c99
    CC     = gcc
    PROG   = Client
    OBJS   = semlib.o helper.o client.o
    
    all:	${PROG}
    
    clean:
    	rm ${OBJS} ${PROG} *~
      
    ${PROG}:	${OBJS}
    	${CC} ${FLAGS} ${OBJS} -o $@
    
    .c.o:
    	${CC} ${FLAGS} $< -c
    
    ##########################
    
    semlib.o: semlib.c
    
    helper.o: helper.c
    
    client.o: client.c

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So all should just make bookie and client. (They should be separate targets in case you only change one of them.) You know which files bookie and helper should depend on. I don't know what this semlib thing is.

    Edit: to be more clear, I'm thinking something along these lines:
    Code:
    all: bookie client
    
    bookie: bookie.c helper.c helper.h
            gcc -Wall -o bookie bookie.c helper.c
    
    client: client.c helper.c helper.h
            gcc -Wall -o client client.c helper.c
    I think my syntax for all is right.
    Last edited by tabstop; 11-12-2008 at 10:42 AM.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    A very simple example for you:
    Code:
    PREFIX = $(PWD)/..
    BUILD_DIR = $(PWD)
    FIXDATE = $(BUILD_DIR)/fixbuild
    MDEPS = $(BUILD_DIR)/mkdep
    DEPSFILE = $(BUILD_DIR)/.Makefile.deps
    CPP  = $(CROSS)g++
    CC   = $(CROSS)gcc
    STRIP = $(CROSS)strip
    RES  =
    OBJ  = kybd.o lib.o hp8450.o hp8450ctrl.o $(RES)
    LIBS +=
    INCS =
    BIN  = hp8450
    CFLAGS = $(DEFS) $(INCS) -Wall -O2 $(EXTRADEFS)
    RM = rm -f
    
    .PHONY: all all-before all-after clean
    
    all: fixdate mkdeps all-before $(IOCONVERTBIN) $(TESTDISPBIN) $(BIN) all-after
    
    fixdate:
            @ ${FIXDATE} $(BUILD_DIR)
    
    ${DEPSFILE}:
    mkdeps:
            @ ${MDEPS} ${DEPSFILE}
    
    clean:
            @ ${RM} $(IOCONVERTOBJ) $(OBJ) $(BIN) build.h
    
    $(BIN): $(OBJ)
            @echo Linking $(BIN)...
            @$(CC) $(OBJ) $(CFLAGS)  -o $(BIN) $(LIBS)
    ifndef NOSTRIP
                    @ echo Stripping $(BIN)
                    @$(STRIP) $(BIN)
    endif
            @ echo Build Complete
    
    include ${DEPSFILE}
    If you want to see the scripts that are used in this one, just let me know and I'll PM them to you.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    So all should just make bookie and client. (They should be separate targets in case you only change one of them.) You know which files bookie and helper should depend on. I don't know what this semlib thing is.

    Edit: to be more clear, I'm thinking something along these lines:
    Code:
    all: bookie client
    
    bookie: bookie.c helper.c helper.h
            gcc -Wall -o bookie bookie.c helper.c
    
    client: client.c helper.c helper.h
            gcc -Wall -o client client.c helper.c
    I think my syntax for all is right.
    However, it would probably be "smarter" to do:
    Code:
    all: bookie client
    
    bookie: bookie.o helper.o
            gcc -Wall -o bookie bookie.o helper.o
    
    client: client.o helper.o
            gcc -Wall -o client client.o helper.o
    
    client.o: client.c helper.h 
             gcc -Wall -o client.o client.c
    
    helper.o: helper.c helper.h
             gcc -Wall -o helper.o helper.c
    // I think you get the idea.
    You can also simplify the "compile .c to .o" by doing this:
    Code:
    .o.c:
             gcc -Wall -o $@ $<
    
    helper.o: helper.c helper.h
    client.o: client.c helper.h
    bookie.o: bookie.c helper.h
    (Obviously, you still need the bits that tell make how to make the client and bookie executables).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by matsp View Post
    Code:
    .o.c:
             gcc -Wall -o $@ $<
    
    helper.o: helper.c helper.h
    client.o: client.c helper.h
    bookie.o: bookie.c helper.h
    (Obviously, you still need the bits that tell make how to make the client and bookie executables).

    --
    Mats
    One problem with this is that if you add any header files to the program, you will have to add that header in the Makefile. If you generate all of the dependencies on-the-fly, you don't have to worry about that portion.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Kennedy View Post
    One problem with this is that if you add any header files to the program, you will have to add that header in the Makefile. If you generate all of the dependencies on-the-fly, you don't have to worry about that portion.
    Sure - but it also makes it harder to understand what is going on, and for a small project it's probably worth the effort to add header files manually.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 04:13 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

Tags for this Thread


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