Makefile with source code in 2 directory

Discussion in 'C Programming' started by Rudra Banerjee, Sep 5, 2012.

  1. Dear friends,
    my project has structure like
    $ls new
    files Makefile help.c table.c callbk_list.h cwlst.h
    where files is a directory, with hundreds of C code, which are the functions in the main code table.c

    I tried to compile with Makefile as:
    CC=gcc
    CFLAGS =-O3
    SUBDIR=files
    PCONF1=pkg-config --cflags --libs gtk+-2.0
    objects := $(wildcard $(SUBDIR)/*.o)
    ptbl: $(objects) help.c table.c callbk_list.h cwlst.h
    $(CC) `$(PCONF1)` $(objects) help.c table.c -o ptbl

    $(SUBDIR)/%.o: $(SUBDIR)/%.c
    $(CC) -c `$(PCONF1)` $<

    which is not compiling the $(SUBDIR)/%.c files at all, with make giving error:

    $ make
    gcc `pkg-config --cflags --libs gtk+-2.0` help.c table.c -o ptbl
    /tmp/ccOKcWuf.o: In function `main':
    table.c:(.text+0xa9a): undefined reference to `callback_A'
    table.c:(.text+0xb3a): undefined reference to `callback_B'
    .....
    and so on

    but if I try to compile directly,
    gcc -g table.c help.c files/prop_*.c `pkg-config --cflags --libs ^Ck+-2.0` -o ptbl
    its working fine.
    Can anybody please show me the error?
    Rudra Banerjee, Sep 5, 2012
    #1
    1. Advertising

  2. gcc -g table.c help.c files/prop_*.c `pkg-config --cflags --libs gtk+-2.0` -o ptbl
    Rudra Banerjee, Sep 5, 2012
    #2
    1. Advertising

  3. Rudra Banerjee

    Casey Carter Guest

    On 2012-09-04 23:45, Rudra Banerjee wrote:
    > objects := $(wildcard $(SUBDIR)/*.o)


    $(wildcard $(SUBDIR)/*.o) will be empty unless the object files are
    compiled already. Try:

    sources := $(wildcard $(SUBDIR)/*.c)
    objects := $(sources:.c=.o)
    Casey Carter, Sep 5, 2012
    #3

  4. >
    >
    > sources := $(wildcard $(SUBDIR)/*.c)
    >
    > objects := $(sources:.c=.o)


    Thanks,
    Its working.
    Though it was creating the .o files of SUBDIR source in maindir.
    so I did:
    exe=ptbl
    CC=gcc
    CFLAGS =-O3
    SUBDIR=files
    PCONF1=pkg-config --cflags --libs gtk+-2.0
    sources := $(wildcard $(SUBDIR)/*.c)
    objects := $(sources:.c=.o)
    $(exe): $(objects) help.c table.c callbk_list.h cwlst.h
    $(CC) `$(PCONF1)` $(objects) help.c table.c -o $(exe)

    $(SUBDIR)/%.o: $(SUBDIR)/%.c
    $(CC) -c `$(PCONF1)` $<
    mv $(*F).o $(SUBDIR)/$(*D)

    and moved the files back to SUBDIR.
    is there any way to create the .o of SUBDIR/*.c in SUBDIR only?
    Rudra Banerjee, Sep 5, 2012
    #4
  5. Rudra Banerjee

    Jorgen Grahn Guest

    On Wed, 2012-09-05, bartek szurgot wrote:
    > On 09/05/2012 07:52 AM, Rudra Banerjee wrote:
    >> $(SUBDIR)/%.o: $(SUBDIR)/%.c
    >> $(CC) -c `$(PCONF1)` $<
    >> mv $(*F).o $(SUBDIR)/$(*D)
    >>
    >> and moved the files back to SUBDIR.
    >> is there any way to create the .o of SUBDIR/*.c in SUBDIR only?

    >
    > %.o: %.c
    > $(CC) ...


    Which is the builtin rule in Gnu make, so you can omit it and not
    write anything at all! If you need something special per subdirectory
    you can e.g. do this without overriding the default rule:

    test/%.o: CPPFLAGS+=-I.

    > there is one more thing missing here - the dependencies on header files.
    >
    > you might just prefer to use ready-to-go solution. i've once wrote
    > generic makefile for compiling all files in the current directory and
    > subdirectories:
    > https://github.com/el-bart/build_process/tree/master/small_prj_build_process
    > it also tracks dependencies, allows to build automated and manual tests,
    > debug, release. all builds are done in a separate directory.


    Well, yeah, as long as he can still understand roughly what's going
    on. I've seen enough people treating the Makefiles as magic which they
    don't have to understand or care about.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Sep 5, 2012
    #5
  6. Rudra Banerjee

    Casey Carter Guest

    On 2012-09-05 00:52, Rudra Banerjee wrote:
    > Thanks,
    > Its working.
    > Though it was creating the .o files of SUBDIR source in maindir.
    > so I did:
    > exe=ptbl
    > CC=gcc
    > CFLAGS =-O3
    > SUBDIR=files
    > PCONF1=pkg-config --cflags --libs gtk+-2.0
    > sources := $(wildcard $(SUBDIR)/*.c)
    > objects := $(sources:.c=.o)
    > $(exe): $(objects) help.c table.c callbk_list.h cwlst.h
    > $(CC) `$(PCONF1)` $(objects) help.c table.c -o $(exe)
    >
    > $(SUBDIR)/%.o: $(SUBDIR)/%.c
    > $(CC) -c `$(PCONF1)` $<
    > mv $(*F).o $(SUBDIR)/$(*D)
    >
    > and moved the files back to SUBDIR.
    > is there any way to create the .o of SUBDIR/*.c in SUBDIR only?
    >

    Add your `$(PCONF1)` into CFLAGS, and get rid of the compiling rule.
    make's default rules should build everything properly:

    exe = ptbl
    PCONF1 := $(shell pkg-config --cflags --libs gtk+-2.0)
    CC = gcc
    CFLAGS = -O3 $(PCONF1)
    SUBDIR = files
    sources := $(wildcard $(SUBDIR)/*.c)
    objects := $(sources:.c=.o)

    $(exe): $(objects) help.c table.c callbk_list.h cwlst.h
    Casey Carter, Sep 5, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Liang Chen
    Replies:
    22
    Views:
    822
    Keith Thompson
    Aug 23, 2004
  2. Author Tarun Tyagi
    Replies:
    0
    Views:
    706
    Author Tarun Tyagi
    Dec 29, 2004
  3. Jens-Oliver Murer
    Replies:
    1
    Views:
    457
    Jens-Oliver Murer
    Oct 17, 2008
  4. Jenny
    Replies:
    3
    Views:
    253
    Dag Sunde
    Dec 17, 2004
  5. Nina
    Replies:
    1
    Views:
    90
    Mothra
    Oct 11, 2005
Loading...

Share This Page