Thread: Help with making a Makefile

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    42

    Help with making a Makefile

    I would lik to create a program in C/C++ running in Linux with multiple files, with 1 main file calling all my subroutines. There will also be a header file containing all my global variables that will be shared to used in each of the individual subroutines(So that i can access the same data anywhere).
    Each of the subroutines will be in contained in an individual file.

    eg. main.cpp, funct1.cpp, funct2.cpp, calculate.cpp, variables.h

    main.cpp will call funct1.cpp & funct2.cpp. In funct1.cpp & funct2.cpp, both will call on funct3.cpp.

    Prob:
    funct3 contains multiple small functions within. In which file should i declare these sub functions? should this funct3 be saved as a .cpp or a .h?

    Since all the cpp files require e shared use of the same variables, do i have to #include "variables.h" in every of the files? If not, in which file should i having this statement? I am facing the problem of redeclaration of variables when linking the files together with my makefile. However, if i did not include this in each of the files, i am unable to create my object files.

    I am unable to link the files together. How should i go about creating the makefile as well as rest of the files?

    My makefile goes like this:

    Code:
    test: main.o funct1.o funct2.o funct3.o variables.h
      cc -o test main.o funct1.o funct2.o funct3.o variables.h
    main.o: main.cpp variables.h
      cc -o main.cpp
    funct1.o: funct1.cpp funct3.cpp variables.h
      cc -o funct1.cpp
    funct2.o: funct2.cpp funct3.cpp variables.h
      cc -o funct1.cpp
    funct3.o: funct3.cpp variables.h
      cc -o funct1.cpp

  2. #2
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    Quote Originally Posted by Eavan Hyde
    I would lik to create a program in C/C++ running in Linux with multiple files, with 1 main file calling all my subroutines. There will also be a header file containing all my global variables that will be shared to used in each of the individual subroutines(So that i can access the same data anywhere).
    Each of the subroutines will be in contained in an individual file.

    eg. main.cpp, funct1.cpp, funct2.cpp, calculate.cpp, variables.h

    main.cpp will call funct1.cpp & funct2.cpp. In funct1.cpp & funct2.cpp, both will call on funct3.cpp.

    Prob:
    funct3 contains multiple small functions within. In which file should i declare these sub functions? should this funct3 be saved as a .cpp or a .h?

    Since all the cpp files require e shared use of the same variables, do i have to #include "variables.h" in every of the files? If not, in which file should i having this statement? I am facing the problem of redeclaration of variables when linking the files together with my makefile. However, if i did not include this in each of the files, i am unable to create my object files.

    I am unable to link the files together. How should i go about creating the makefile as well as rest of the files?

    My makefile goes like this:

    Code:
    test: main.o funct1.o funct2.o funct3.o variables.h
      cc -o test main.o funct1.o funct2.o funct3.o variables.h
    main.o: main.cpp variables.h
      cc -o main.cpp
    funct1.o: funct1.cpp funct3.cpp variables.h
      cc -o funct1.cpp
    funct2.o: funct2.cpp funct3.cpp variables.h
      cc -o funct1.cpp
    funct3.o: funct3.cpp variables.h
      cc -o funct1.cpp

    It should be like this:
    Code:
    main.o: main.cpp variables.h
      cc -c main.cpp
    ......................
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A makefile should contain your compilation dependencies, not your functional dependencies.

    So
    funct1.o: funct1.cpp funct3.cpp variables.h
    You don't need to depend on funct3.cpp in order to compile func1.cpp. The point where funct.o depends on funct3.o is when you link all the files together.

    All you need is the name of the source file, and the header file(s) which it includes.

    For the sake of brevity, standard includes like iostream are not included in dependencies, since they can be regarded as never changing.

    Also, your compiler command lines need to produce the target (funct1.o) from the dependencies

    Code:
    test: main.o funct1.o funct2.o funct3.o
      cc -o test main.o funct1.o funct2.o funct3.o
    main.o: main.cpp variables.h
      cc -c main.cpp
    funct1.o: funct1.cpp variables.h
      cc -c funct1.cpp
    funct2.o: funct2.cpp variables.h
      cc -c funct2.cpp
    funct3.o: funct3.cpp variables.h
      cc -c funct3.cpp
    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.

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    42
    Thks for pointing out the mistake.

    I tried it however my program still does not work. I am getting problems getting my object file. Is it because of the way i do my #include in my cpp files.

    I am getting the error of not finding the variables and when i include "variables.h" in all my cpp files, i get the error of redeclaration of variables. Why is this so?

    basically my program requires a funct1.cpp to read in input values to be stored in the global variables in <variables.h> after which the rest of my functions will manipulate these values.

    can anyone help?

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    9
    You should not declare variables in you header file.
    If you need global variables (better try to avoid them if possible) then you need to state in each C-file in which you want to use them that they are extern.
    Like this:
    Code:
        extern int global_number;
    However you want to chose one file in which you leave the extern. In that file the memory for that variable will be allocated.

    However i think you should read a little bit more basics documentation. They explain all this much better than i can do here

    Chris

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    42
    I was considering declaring variables in my header files because i have many variables in this program because it is a very large program consisting of many files(of which most of the files are functions).

    It is hard for me to pass so many parameters into the functions and since i require the same variables and data, i was thinking of allocatting memory location so that i can access the data in each of the variables in the header files, thus the use of global variables.

    Does it mean that i have to redeclare all e global variables in each of the files that requires them? If not how do i go about sharing the data n accessing them?

    My data type varies and some of the functions call other functions which require the same data.

    any suggesstions?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefile Problem: None rule to make target
    By chris24300 in forum Linux Programming
    Replies: 25
    Last Post: 06-17-2009, 09:45 AM
  2. Need help with makefile
    By New_Programmer in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2009, 04:55 PM
  3. Building a project using a Makefile
    By starcatcher in forum Windows Programming
    Replies: 2
    Last Post: 11-23-2008, 11:50 PM
  4. makefile blues....
    By WaterNut in forum C Programming
    Replies: 6
    Last Post: 05-30-2005, 08:22 PM
  5. Need help with Makefile
    By xshapirox in forum C++ Programming
    Replies: 14
    Last Post: 09-28-2004, 03:32 PM