#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Makefile using a lib


    Hello, I have 3 files, sorted-list.h , sorted-list.c and main.c
    the first 2 (sorted-list.h and sorted-list.c) are going to be my library, and I know that in order to create a library out of them I first create and object file:

    Code:
    gcc -c sorted-list.c -o sorted-list.o
    Then, the archiver (ar) is invoked to produce a static library out of the object file sorted-list.o.

    Code:
    ar  rcs libsl.a  sorted-list.o
    And finally I link the library with my main ( which includes the header file sorted-list.h) like so:

    Code:
    gcc -o sl main.c libsl.a
    I'm not really familiar with creating Makefiles and i'd like to create a makefile that does exactly the same and add the option to do make debug and make clean.

    I have this so far:

    Code:
    CCFLAGS = -Wall
    
    all: main 
    
    main: libls.a
    	gcc $(CCFLAGS) -o ls main.c libls.a
    libls.a: sorted-list.o
    	ar rcs libls.a sorted-list.o
    sorted-list.o: 
    	gcc $(CCFLAGS) -c sorted-list.c -o sorted-list.o		
    clean:
    	rm -f *.o main
    However I don't really know how to add the debug and also completly clean all obj executable and libs created.

    Thanks in advanced.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,904
    Rep Power
    481

    make advice


    I once asked Stallman about make. He responded that the makefile I sent him was the strangest he'd seen. (I wanted make to rebuild if the makefile changed, and didn't really understand the proper way to do it.)

    It might be worth your time to investigate the ldconfig program.

    gnumake manual

    With following,
    $ make
    builds the library, main, and ls.

    $ make debug
    does
    make clean
    followed by
    make 'CFLAGS=-O0 -g'
    In other words, make debug starts from the beginning and recompiles all the sources using -O0 -g
    and
    In other words, if you want to change back to an optimized version without the debugging glop in it, you will need to specifically

    $ make clean
    $ make

    Now, your make file uses the target
    main
    but you specify the gcc option
    -o ls
    I inserted .PHONY targets, but I don't think you actually intended main to be phony. Not really knowing what you want, I fixed it so the all target depends on ls , and ls depends on main, and simply builds ls by copying main to ls. You could use a link, but I think you'll figure out that you actually want to skip the intermediate step and use

    all: ls
    ls: main.c etceteras...

    Of course the other thing is that make knows about libraries, (read the manual!) so I condensed the commands more than you might like and just stuck
    libls.a(sorted-list.o) in as a dependent of the target main. Remember, you can command
    $ make 'libls.a(sorted-list.o)'
    to compile sorted-list.whatever_the_source and stuff the object into libls.a .

    Confused?
    Code:
    override CFLAGS += -Wall ################# I used += and override
    
    all: ls	#################
    
    ls: main #################
    	cp main ls
    
    main: main.c libls.a(sorted-list.o) sorted-list.h ################ 
    	gcc $(CFLAGS) -o $@ $< libls.a
    
    .PHONY: clean debug
    
    clean:
    	rm -f *.o main ls
    
    debug:		      # recursive make---I'm sure there's another way.
    	make clean
    	make 'CFLAGS=-O0 -g'
    Last edited by b49P23TIvg; October 7th, 2012 at 09:36 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo