Results 1 to 9 of 9

Thread: Help-Makefile issue

  1. #1
    Join Date
    Feb 2012
    Beans
    93

    Help-Makefile issue

    I would like learn how to write Makefile and some issues:

    Code:
       objects = foo.o bar.o
    
        all: $(objects) 
    
        $(objects): %.o: %.c
                $(CC) -c $(CFLAGS) $< -o $@
    My questions is:

    1)what does "all: %(objects)" mean? without this line, does it still work?
    2)%.o equal to "foo.o bar.o", % equal to "foo bar" or *? In others words, % equal to all files? %.c equal to "foo.c bar.c" or "*.c"?

    My material about Makefile is very limited, mainly for gcc and g++. Would you please tell me some material which clarifes Makefile in detail?

    Thanks!
    Last edited by pellyhawk; July 24th, 2012 at 07:53 AM.

  2. #2
    Join Date
    May 2007
    Beans
    251

    Re: Help-Makefile issue

    Quote Originally Posted by pellyhawk View Post
    I would like learn how to write Makefile and some issues:

    Code:
       objects = foo.o bar.o
    
        all: $(objects) 
    
        $(objects): %.o: %.c
                $(CC) -c $(CFLAGS) $< -o $@
    My questions is:

    1)what does "all: %(objects)" mean? without this line, does it still work?
    It tells make to create a target "all" that depends on "$(objects)" - i.e. the value of the variable objects that you have defined above. Note, it's a $ and not %.

    So, in other words, it's equivalent to
    Code:
    all: foo.o bar.o
    The reason is to simply allow running "make all" and be done with it.

    Also, if there is no .DEFAULT target defined, the first target defined will be treated as the default target. So, if you just do "make", it's equivalent of saying "make all".

    Quote Originally Posted by pellyhawk View Post
    2)%.o equal to "foo.o bar.o", % equal to "foo bar" or *? In others words, % equal to all files? % equal to "foo.c bar.c" or "*.c"?
    No, '%' is not like a '*' wildcard. The rule
    Code:
    %.o : %.c
    is like a template to expand into something like
    Code:
    foo.o : foo.c
          .......
    
    bar.o : bar.c
          ........
    That is, for every .o file, create a rule with matching .c file as a dependency. Rather, for every .c file found, create a rule like above.

    HTH.
    The Unforgiven

  3. #3
    Join Date
    Feb 2012
    Beans
    93

    Re: Help-Makefile issue

    Thanks a lot. I have understood what you told me about "%.c". but I am still confused by "%".

    If only "%" is used, what dose it mean? Especially
    Code:
        vpath %.c foo
        vpath %   blish
        vpath %.c bar
    why "%" is used in line 2 while "%.c" are used in line 1 and 3. I guess these 3 lines mean that all .c files are searched in directories foo, blish and bar. why not use
    Code:
        vpath %.c foo
        vpath %.c blish
        vpath %.c bar

  4. #4
    Join Date
    May 2007
    Beans
    251

    Re: Help-Makefile issue

    Quote Originally Posted by pellyhawk View Post
    Thanks a lot. I have understood what you told me about "%.c". but I am still confused by "%".

    If only "%" is used, what dose it mean? Especially
    Code:
        vpath %.c foo
        vpath %   blish
        vpath %.c bar
    why "%" is used in line 2 while "%.c" are used in line 1 and 3. I guess these 3 lines mean that all .c files are searched in directories foo, blish and bar. why not use
    Code:
        vpath %.c foo
        vpath %.c blish
        vpath %.c bar
    Turns out that I was wrong about '%' - it is indeed a wildcard.
    However, found this piece to be quite helpful about vpath understanding:
    http://www.cmcrossroads.com/ask-mr-m...path-and-vpath

    See if it helps you.
    The Unforgiven

  5. #5
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,566
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Help-Makefile issue

    Quote Originally Posted by pellyhawk View Post
    Thanks a lot. I have understood what you told me about "%.c". but I am still confused by "%".

    If only "%" is used, what dose it mean?
    You seem to have taken your example from the GNU Make Manual. So '%' means "any sequence of zero or more characters".
    Especially
    Code:
        vpath %.c foo
        vpath %   blish
        vpath %.c bar
    why "%" is used in line 2 while "%.c" are used in line 1 and 3. I guess these 3 lines mean that all .c files are searched in directories foo, blish and bar.
    The second directive will match any file that is being searched for. So if a.s is needed for example, then it will look in the blish directory.
    why not use
    Code:
        vpath %.c foo
        vpath %.c blish
        vpath %.c bar
    In this case, if a.s is needed for example, then it will not look in the blish directory. It will only look there for files whose name ends in .c

  6. #6
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Help-Makefile issue

    Code:
    $(objects): %.o: %.c
        $(CC) -c $(CFLAGS) $< -o $@
    this is unnecessary (at least in GNU Make), there is an implicit rule for that

    Code:
    firas@aoba test % ls
    bar.c  foo.c  Makefile
    firas@aoba test % cat Makefile 
    CC=gcc
    CFLAGS=-std=c99 -pedantic -Wall -Wextra -g
    OBJECTS=foo.o \
    	bar.o
    
    all: $(OBJECTS)
    firas@aoba test % make
    gcc -std=c99 -pedantic -Wall -Wextra -g   -c -o foo.o foo.c
    gcc -std=c99 -pedantic -Wall -Wextra -g   -c -o bar.o bar.c
    firas@aoba test % ls
    bar.c  bar.o  foo.c  foo.o  Makefile
    「明後日の夕方には帰ってるからね。」


  7. #7
    Join Date
    Feb 2012
    Beans
    93

    Re: Help-Makefile issue

    the_unforgiven and spjackson,

    Thanks a lot.

    Bachstelze,

    I am totally confused. In your Makefile, nothing about gcc is called, but why the following commands worked
    Code:
    gcc -std=c99 -pedantic -Wall -Wextra -g   -c -o foo.o foo.c
    gcc -std=c99 -pedantic -Wall -Wextra -g   -c -o bar.o bar.c
    It is very weird! Would you please clarify it for me? I am new to gcc and Makefile and reading. Many thanks.

  8. #8
    Join Date
    May 2007
    Beans
    251

    Re: Help-Makefile issue

    Quote Originally Posted by pellyhawk View Post
    Bachstelze,

    I am totally confused. In your Makefile, nothing about gcc is called, but why the following commands worked
    Code:
    gcc -std=c99 -pedantic -Wall -Wextra -g   -c -o foo.o foo.c
    gcc -std=c99 -pedantic -Wall -Wextra -g   -c -o bar.o bar.c
    It is very weird! Would you please clarify it for me? I am new to gcc and Makefile and reading. Many thanks.
    All versions of make generally have built-in rules (called implicit rules) for most commonly used compilation targets - for various languages that are popular at the time.

    Check this page from the make manual:
    http://www.gnu.org/software/make/man...cit-Rules.html

    You can print the internal rules database of make using the command:
    Code:
    make -p
    The output is quite large, so you may want to pipe it through less/more or dump it into a file for reading later.

    HTH
    The Unforgiven

  9. #9
    Join Date
    Feb 2012
    Beans
    93

    Re: Help-Makefile issue

    Quote Originally Posted by the_unforgiven View Post
    All versions of make generally have built-in rules (called implicit rules) for most commonly used compilation targets - for various languages that are popular at the time.

    Check this page from the make manual:
    http://www.gnu.org/software/make/man...cit-Rules.html

    You can print the internal rules database of make using the command:
    Code:
    make -p
    The output is quite large, so you may want to pipe it through less/more or dump it into a file for reading later.

    HTH
    Thanks for your help and I shall look through it carefully.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •