Like Tree4Likes
  • 1 Post By dmh2000
  • 1 Post By rcgldr
  • 2 Post By KCfromNC

Make utility and Header Files.

This is a discussion on Make utility and Header Files. within the C Programming forums, part of the General Programming Boards category; Hello to all. We suppose that a program consists of three source files main.c f1.c and f2.c plus two header ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    299

    Make utility and Header Files.

    Hello to all.

    We suppose that a program consists of three source files main.c f1.c and f2.c plus two header files, f1.h and f2.h. All three source files include f1.h, but only f1.c and f2.c include f2.h.

    I wrote a makefile for this program and I want your opinion if is it correct :

    Code:
    demo : demo.o f1.o f2.o 
               gcc -o demo demo.o f1.o f2.o    
    
    demo.o : main.c f1.h
                gcc -c main.c
    
    f1.o:  f1.c  f1.h f2.h
            gcc -c f1.c
    f2.o:  f2.c f1.h f2.h
             gcc -c f2.c
    I read that the order of rules is arbitrary ... so we can start with demo.o as a first rule? of f1.o ?

    The file is to be named demo of course and I know about the tab space before the command of each rule.

    Another issue that I need some opinion is the following :

    If a change affects a header file we will recompile all files that include this header file since they could potentially be affected by the change. (Some of them might not be , but it pays to be conservative)

    How can "some of them might not be?" Do you have an example?

    Thank you in advance.
    Last edited by Mr.Lnx; 04-15-2013 at 07:33 AM.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    If a change affects a header file we will recompile all files that include this header file since they could potentially be affected by the change. (Some of them might not be , but it pays to be conservative)

    How can "some of them might not be?" Do you have an example?
    an example of a change that technically would not need a recompile would be if you added some comments to the include file without changing anything else.

    your makefile looks right to me.
    Mr.Lnx likes this.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > How can "some of them might not be?" Do you have an example?
    Consider using something like
    $ gcc -MM foo.c
    foo.o: foo.c foo.h
    to provide you with accurate dependency rules.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    299
    Quote Originally Posted by Salem View Post
    > How can "some of them might not be?" Do you have an example?
    Consider using something like
    $ gcc -MM foo.c
    foo.o: foo.c foo.h
    to provide you with accurate dependency rules.
    Hmmm yes we take the dependencies with this option -MM it is the first time that I see this option though.

    another question .... what does it mean with that

    If the linker can't find the functions that are in file foo.c it may not know about the file

    It means that the file in which the functions are defined ? For example if we call from foo.c two functions that are defined in another file for example foo2.c ... linker does not know the file foo2.c if can't find the functions?

    and from this source -> UNIX Make

    on the paragraph Simple Example (In the beginning) the description of how make will work is the same with C? (Because It says about C++ file1.cc for example not file1.c)

    thank you in advance

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,333
    Quote Originally Posted by Mr.Lnx View Post
    Code:
    demo.o : main.c f1.h
                gcc -c main.c
    How does gcc -c main.c producde demo.o?

    Quote Originally Posted by Mr.Lnx View Post
    I read that the order of rules is arbitrary
    For some make utilities, the first target in the make file is the default. Otherwise you'd have to specify which target you want built.

    Quote Originally Posted by Mr.Lnx View Post
    If a change affects a header file we will recompile all files that include this header file since they could potentially be affected by the change. (Some of them might not be , but it pays to be conservative). How can "some of them might not be?" Do you have an example?
    If a change was made to a header file, such as a change to a structure that only one of the c programs uses, then the remaining c programs would not be affected.
    AndiPersti likes this.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    299
    Quote Originally Posted by rcgldr View Post
    How does gcc -c main.c producde demo.o?
    Code:
    demo : main.o f1.o f2.o
               gcc -o demo main.o f1.o f2.o   
     
    main.o : main.c f1.h
                gcc -c main.c
     
    f1.o:  f1.c  f1.h f2.h
            gcc -c f1.c
    f2.o:  f2.c f1.h f2.h
             gcc -c f2.c
    I think now it's ok

    Quote Originally Posted by rcgldr View Post
    If a change was made to a header file, such as a change to a structure that only one of the c programs uses, then the remaining c programs would not be affected.
    Aha a wider perception because you are talking about of entire programs not only one program which is made of many files... this has to do with portability(using the same header to other programs). That is what you want to say?

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by Mr.Lnx View Post
    Code:
    demo : main.o f1.o f2.o
               gcc -o demo main.o f1.o f2.o   
     
    main.o : main.c f1.h
                gcc -c main.c
     
    f1.o:  f1.c  f1.h f2.h
            gcc -c f1.c
    f2.o:  f2.c f1.h f2.h
             gcc -c f2.c
    Another way to do this :

    Code:
    # Use variables for the target ($@ = demo here) and the dependencies ($^ = main.o f1.o f2.o)
    demo : main.o f1.o f2.o
               gcc -o $@ $^
    
    # Define a generic rule for getting *.o from *.c.  $< is the .c file in this case
    %.o : %.c 
            gcc -c $<
    
    # Add additional dependencies for header files. They are on top of the  ".o needs a .c file" 
    # rule above, so no need to include the .c files in this list as well
    main.o : f1.h
    f1.o:  f1.h f2.h
    f2.o:  f1.h f2.h
    The benefit is that you're not repeating things more than you need to - instead of repeating lists of files or actions several times, they're just in one place. That way when you add and remove files you only have to change them in one place rather than in several. Or if you modify the options you pass to gcc, you just have to do it in one place instead of for each .o:.c rule.
    stahta01 and Mr.Lnx like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to make the best use of header files
    By alanb in forum C++ Programming
    Replies: 4
    Last Post: 08-31-2009, 05:45 PM
  2. the proper way to make .h/header files
    By revelation437 in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2004, 02:22 AM
  3. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  4. Replies: 2
    Last Post: 08-08-2003, 05:36 PM
  5. Help with Header/Make files
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 04-10-2002, 11:57 PM

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