Thread: Multiple Source Files, make files, scope, include

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    Multiple Source Files, make files, scope, include

    I'm working on a little program and I want to break it up into multiple source files and also use a make file. I can't seem to get my program to compile though and I don't know why. I type

    Code:
    mingw32-make -f makefile.mak
    and it doesn't compile. Here are some source files that illustrate my problem.

    Code:
    // main.cpp
    #include <iostream>
    using namespace std;
    
    int main () {
      CPoint A, B;
      A.set_point (3,4);
      B.set_point (5,-2);
      cout << "distance between points: " << distance(A, B) << endl;
      return 0;
    }
    Code:
    // CPoint.cpp
    #include <cmath>
    
    class CPoint {
        double x, y;
      public:
        void set_point (double,double);
        double distance(CPoint,CPoint);
    };
    
    
    void CPoint::set_point (double a, double b) {
      x = a;
      y = b;
    }
    
    double CPoint::distance (CPoint A, CPoint B){
    	double dist;
    	
    	dist = sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
    	
    	return dist;	
    }
    Code:
    #makefile.mak
    all: Mult
    
    Mult: main.o CPoint.o
    	g++ main.o CPoint.o -o Mult
    
    main.o: main.cpp
    	g++ -c main.cpp
    	
    CPoint.o: CPoint.cpp
    	g++ -c CPoint.cpp	
    			
    clean:
    	rm -rf *o Mult

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Well, distance is a member function of CPoint. So you have to call it via A.distance(...), or B.distance(...). Might be better to change the function name to distance_to( CPoint ) so you would call it like A.distance_to( B ); then get rid of all the A.'s in the implementation of the distance function.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    main() doesn't know about CPoint. The declarations and type definitions must be available. Put the definition of CPoint into a new header file CPoint.hpp and include it from all .cpp files that need it.

    Also, there's no need to be so explicit in makefiles. Given built-in pattern rules and all that, your current makefile could be written as:

    Code:
    #makefile.mak
    all: Mult
    
    Mult: main.o CPoint.o
    			
    clean:
    	rm -rf *.o Mult
    
    .PHONY: all clean
    Note also the lack of a dot in your clean rule.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by twomers View Post
    Well, distance is a member function of CPoint. So you have to call it via A.distance(...), or B.distance(...). Might be better to change the function name to distance_to( CPoint ) so you would call it like A.distance_to( B ); then get rid of all the A.'s in the implementation of the distance function.
    You also probably should remove one of the arguments of distance, so that it uses the object used to call distance, e.g.
    Code:
    a.Distance(b);
    As an alternative, you could make a distance function that takes two CPoint arguments, that is a friend function of CPoint (it needs to be a friend so that it can get to the CPoint private data).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Quote Originally Posted by CornedBee View Post
    main() doesn't know about CPoint. The declarations and type definitions must be available. Put the definition of CPoint into a new header file CPoint.hpp and include it from all .cpp files that need it.
    I presume that member functions would be placed in a third .cpp file that must be included somewhere, no?


    Code:
    #makefile.mak
    all: Mult
    
    Mult: main.o CPoint.o
    			
    clean:
    	rm -rf *.o Mult
    
    .PHONY: all clean
    Note also the lack of a dot in your clean rule.
    Is this true on *nix systems as well?

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by thetinman View Post
    I presume that member functions would be placed in a third .cpp file that must be included somewhere, no?
    No, the member functions stay in the second source file. And you never include source files.

    Is this true on *nix systems as well?
    It's a feature of GNU make and some other makes.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    I'm made some changes, but am still having problems. My files are now



    Code:
    # makefile.mak
    all: Mult
    
    Mult: main.o CPoint.o
                g++ main.o CPoint.o -o Mult
    
    main.o: main.cpp
                g++ -c main.cpp
              
    CPoint.o: CPoint.hpp
                g++ -c CPoint.hpp         
                                       
    clean:
                rm -rf *.o Mult
              
    .PHONY: all clean


    Code:
    // main.cpp
    #include <iostream>
    #include "CPoint.hpp"
    
    using namespace std;
    
    int main () {
      CPoint A, B;
      A.set_point (3,4);
      B.set_point (5,-2);
      return 0;
    }


    Code:
    // CPoint.hpp
    #include <cmath>
    
    class CPoint {
        double x, y;
      public:
        void set_point (double,double);
    };
    
    
    void CPoint::set_point (double a, double b) {
      x = a;
      y = b;
    }
    Last edited by thetinman; 09-12-2008 at 04:18 PM. Reason: formatting

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    set_point looks very much like it should be the constructor to me.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You've abandoned CPoint.cpp, which is where set_point and all other out-of-line implementations belong.

    You cannot compile a header file. You can only compile source files. Header files are included.

    In addition, header files should be listed in the dependencies. Your makefile then becomes:
    Code:
    # makefile.mak
    all: Mult
    
    Mult: main.o CPoint.o
    
    main.o: main.cpp CPoint.hpp
              
    CPoint.o: CPoint.cpp CPoint.hpp
                                       
    clean:
                rm -rf *.o Mult
              
    .PHONY: all clean
    It's possible to auto-generate the dependency information, but that's rather complicated and not worth it for a small project.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Why is a header file included if it is not getting compiled?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why is a header file included if it is not getting compiled?
    Because it becomes part of a translation unit (source file + headers and source files that it includes) that is compiled.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The contents of header files are just pasted into where '#include "x.h"' is located.
    Thus they actually end up in the actual .cpp files.
    Header files should contain declarations and class definitions and things like that.
    Like to make the compiler understand there's a CPoint class, you must give it the CPoint class definition before its usage. To avoid code duplication, you put it inside a header file and include it via #include in all source files that use the class.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    I'm just trying to compile without the makefile. My files look like this:
    Code:
    // main.cpp
    #include <iostream>
    #include "CPoint.hpp"
    
    using namespace std;
    
    int main () {
      CPoint A, B;
      A.set_point (3,4);
      B.set_point (5,-2);
      return 0;
    }
    This is the strange looking one:
    Code:
    // CPoint.hpp
    
    class CPoint {
        double x, y;
      public:
        void set_point (double,double);
    };
    
    #include "CPoint.cpp"
    Code:
    // CPoint.cpp
    
    void CPoint::set_point (double a, double b) {
      x = a;
      y = b;
    }
    In the strange one, I have to put the statement
    Code:
    #include "CPoint.cpp"
    after the primary section of code to prevent compilation errors. This placement looks strange to me, and I suspect that it is bad form - Is it bad form?

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Don't include source code.

    But then again, you've never actually posted any error messages either. Only the useless refrain "it doesn't work".

    CPoint.cpp includes CPoint.hpp (not as you have it).

    Then from the command line, it's
    g++ CPoint.cpp main.cpp

    Or the makefile in your original post.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex across multiple source files
    By Quasar in forum Linux Programming
    Replies: 7
    Last Post: 12-04-2007, 08:25 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Mutiple source files for one program
    By earth_angel in forum C Programming
    Replies: 7
    Last Post: 06-08-2005, 09:47 AM
  5. Multiple source files for one program
    By gflores in forum C++ Programming
    Replies: 3
    Last Post: 08-15-2004, 02:32 AM