A simple question about writing a Makefile

This is a discussion on A simple question about writing a Makefile within the C++ Programming forums, part of the General Programming Boards category; I have main.cc and test.h files, main.cc includes test.h Here is the Makefile, but it doesn't work. any clue? Code: ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    A simple question about writing a Makefile

    I have main.cc and test.h files,
    main.cc includes test.h

    Here is the Makefile, but it doesn't work. any clue?


    Code:
    main : main.o                                                                                                                                                                
            g++ -g main.o -o main                                                                                                                                                
    main.o : main.cc test.h                                                                                                                                                      
            g++ -o main.o -c -g test.h main.cc
    Last edited by meili100; 04-29-2008 at 03:19 PM.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Quote Originally Posted by meili100 View Post
    I have main.cc and test.h files,
    main.cc includes test.h

    Here is the Makefile, but it doesn't work. any clue?


    Code:
                                                                                                                                                  
            g++ -o main.o -c -g test.h main.cc
    Don't list "test.h" on the compile line.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thanks. It works.
    Is that to say, as long as a head file is #included in a correct .cc file, the Makefile never needs to contain the head file? But why I often see a Makefile contains a head file?

  4. #4
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Quote Originally Posted by meili100
    Is that to say, as long as a head file is #included in a correct .cc file, the Makefile never needs to contain the head file?
    No. As long as the header file is #included correctly, the compiler doesn't need to be supplied with it on the command line; it will access it when it compiles the appropriate .cc file.
    Make will want to know about it however as a dependency so it can detect when to recompile certain parts.

    Code:
    main.o : main.cc test.h
    This tells make that main.o depends on main.cc and test.h, and you want it to know this. If make detects that main.cc or test.h have been updated, then it will execute the command(s) you supply in order to create main.o, in this case the next line:

    Code:
    g++ -o main.o -c -g test.h main.cc
    This calls the compiler and gives it test.h and main.cc as the source files, just as if you had typed it yourself on the command line. However, test.h is not a source file, it is a header, so you don't want to include it as one of the arguments to g++.

    Notice that the first line is a "dependancy" while the second line is a "command". Dependancies are what tells make what depends on what. "main.o depends on main.cc and test.h, so if any of these are updated then execute the following command to re-build main.o"

    Then:
    Code:
    main : main.o
    tells make that "main depends on main.o, so if main.o has been updated, execute the following command(s) to rebuild main".

    Code:
    g++ -g main.o -o main
    Is the command make uses (the same as you would type yourself on the command line) to build the file main.

    So if you modify test.h, then the next time you make, it will see that main.o depends on test.h which was updated so it will execute the command to build main.o again, and then it will see that main depends on main.o which was updated, and so it will execute the command to build main.

    Make can get really tricky, and I am still figuring it out myself, but it is a great tool. Google around for makefile tutorials.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Question!!
    By gameuser in forum C++ Programming
    Replies: 2
    Last Post: 06-06-2009, 05:42 PM
  2. Simple question about the C programming tutorial #1
    By elsheepo in forum C Programming
    Replies: 13
    Last Post: 01-19-2009, 07:59 PM
  3. Simple OS writing to screen help
    By HLA91 in forum C Programming
    Replies: 1
    Last Post: 02-24-2008, 08:07 PM
  4. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

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