help: makefile: directory rules

This is a discussion on help: makefile: directory rules within the C Programming forums, part of the General Programming Boards category; There are two directories /src1 and /src2 where the soruce codes are, and another directory /headers for head files. How ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    23

    help: makefile: directory rules

    There are two directories /src1 and /src2 where the soruce codes are, and another directory /headers for head files.

    How should the makefile set variables ( not to hard code all the directories) so that the complier can find these files?


    Many thanks!

    Paul
    Last edited by paulur; 03-16-2006 at 06:00 PM.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    that depends upon your compiler

    BIN = gcc

    BASE = ./
    SRC1 = $(BASE)/src1/
    SRC2 = $(BASE)/src2/

    to include headers use -I like :

    $(BIN) -I "./headers/"

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    hi dude543,

    Thank you so much for your help!

    Another question is that how to put the compiled file in a predifined directory. For example, there is a directory ./output/, and I want all the compiled .o file be stored in ./output. and also, like the .exe file in another directory like ./?

    Thanks again.

    Regards,
    Paul

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    something like that

    Code:
    odir=./obj
    exedir=./exe
    target=myprog.exe
    
    $(exedir)/$(target): $(odir)/mod1.o $(odir)/mod1.o
    	gcc $(odir)/mod1.o $(odir)/mod1.o -o $(exedir)/$(target)
    
    $(odir)/mod1.o: mod1.c
    	gcc -c mod1.c -o $(odir)/mod1.o
    
    $(odir)/mod2.o: mod2.c
    	gcc -c mod2.c -o $(odir)/mod2.o
    Kurt

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Thanks for the message.

    Now i'm using :
    .c.o:
    $(CC) -c $(CFLAGS) $<

    rather than individaul lines to declare complie. How should I add the directory information in this command?

    Thanks again!

    Paul

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    I've just figured out by myself as the following

    hope it can help anyone in future. And thanks for the helps from these nice guys.

    #===================START========================= ===
    CC = gcc

    BASE = ./
    HEAD_DIR = $(BASE)headers/
    SRC_DIR = $(BASE)src/ $(BASE)src/file_process/
    OBJ_DIR = $(BASE)obj/

    SRC = main.c real_lines.c parse_line.c record.c
    OBJ = main.o read_lines.o parse_line.o record.o
    HEADER = file_handler.h
    PROG = handler

    CFLAGS = -ggdb -Wall -I$(HEAD_DIR)

    vpath %.c $(SRC_DIR)
    vpath %.o $(OBJ_DIR)

    $(OBJ_DIR)%.o : %.c
    $(COMPILE.c) $< -o $@

    $(PROG) : $(addprefix $(OBJ_DIR), $(OBJ))
    $(LINK.o) $^ -o $@

    all : $(PROG)

    clean :
    -rm handler $(addprefix $(OBJ_DIR), $(OBJ))
    #================END======================

    Basically, i've got a ./src where all my source codes are, a ./headers where all .head files, and ./obj where i'd like all complied files to be.

    -I$(HEAD_DIR) : links head files included in source file.

    vpath %.c $(SRC_DIR) and vpath %.o $(OBJ_DIR) : tells the compiler the directories and source codes and head files

    (COMPILE.c) <==> $(CC) -c $(CFLAGS)

    $(addprefix $(OBJ_DIR), $(OBJ)) : that's the thing making evey generated object to a predefined directory.

    $(LINK.o) $^ -o $@ : I'm still a bit confused it, but just know it's a implicit rule like (COMPILE.c) .
    Last edited by paulur; 03-19-2006 at 10:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 01:54 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 6
    Last Post: 07-30-2003, 03:08 AM
  5. Directory reading trouble
    By samGwilliam in forum Linux Programming
    Replies: 0
    Last Post: 03-10-2002, 08:43 AM

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