使用Makefile在多个文件C ++中构建互连类

时间:2014-10-17 09:35:51

标签: c++ class build makefile

我不太熟悉从makefile构建更复杂的C ++程序(我通常使用IDE或只使用简单的C而不使用面向对象的编程)。当类在几个文件之间进行交互时,我无法弄清楚如何编写makefile。

简单示例:如何将此单个文件程序拆分为单独的文件并使用Make:

构建它
#include <math.h>

// I want to move this into Vec3d.h
class Vec3d{
    double x,y,z;
    inline void set( const double fx, const double fy, const double fz ) { x=fx; y=fy; z=fz;         }
    inline Vec3d& operator+=( const double& f ) { x+=f; y+=f; z+=f; return *this; }
    inline Vec3d  operator* ( const double& f ) const { Vec3d vo; vo.x=x*f; vo.y=y*f; vo.z=z*f; return vo; }
    // ... other operators omited for shorness
}

// I want to move this into Body.h
class Body{
    double mass;
    Vec3d pos,velocity,force; // use Vect3d
    void getForce(){};
    void move( double dt ){ // use operators defined in Vec3d
        getForce();
        v   += force * -dt;
        pos += v   *    dt;
    }
}

// I want to move this into Planet.h
class Planet : public Body{
    void getForce( ){ // overrides method from "Body"
        double r = sqrt( pos.dot(pos) ) ;
        force = pos * ( -dt/(ir*ir*ir) );
    }
}

// This should be in main.cpp
Vec3d  myVector;
Body   myBody;
Planet myPlanet;
main(){
    // ...do something with myVector, myBody and myPlanet
}

一种选择是将所有内容都包含在main.cpp 中。然而,这很糟糕,因为它会一直重新编译所有内容。

我更愿意重新编译我更改的文件,其余文件来自.o文件。但是为了独立编译Body.h,我必须将#include Vec3d.h加入其中。所以Vec3d.h 将在main.cpp中包含两次,这会产生冲突。

我知道C头文件用于此目的,但我不知道如何在C ++中使用它时我想在类体中定义方法和运算符如果可能的话(它使程序更具可读性,就像在Java中一样。)

1 个答案:

答案 0 :(得分:1)

好的,所以我对诺布问题感到遗憾。唯一的问题是,对于Vec3d,内联方法必须直接在.h文件中声明。整个解决方案是:

生成文件

all: program

program: main.o Planet.o Body.o
    g++ main.o Planet.o Body.o -o program

main: main.cpp
    g++ -c main.cpp

Body.o: Body.cpp
    g++ -c Body.cpp 

Planet.o: Planet.cpp
    g++ -c Planet.cpp 

clean:
    rm -rf *o hello

main.cpp中:

#include "Vec3d.h"
#include "Body.h"
#include "Planet.h"
Vec3d  myVector;
Body   myBody;
Planet myPlanet;
int main(){
    myVector.set(1);
    myBody.pos.set(1);
    myBody.move(0.1);
    myPlanet.getForce();
    myPlanet.move(0.1);
}

Planet.h:

class Planet : public Body{
    public:
    void getForce( );
    //void move( );
};

Planet.cpp:

#include <math.h>
#include "Vec3d.h"
#include "Body.h"
#include "Planet.h"
void Planet::getForce()      {
    double r = sqrt( pos.mag2() );
    force    = pos * ( mass/(r*r*r) );
}

Body.h:

class Body{
    public:
    // parameters
    double mass;
    // state variables
    Vec3d pos,vel;
    // temporary variables
    Vec3d force;
    // methods
    void getForce();
    void move( double dt );
};

Body.cpp:

#include "Vec3d.h"
#include "Body.h"
void Body::getForce(){ force.set(0); };
void Body::move( double dt ){
    vel  += force * dt;
    pos  += vel   * dt;
}

Vec3d.h:

// NOTE: "inline" methods must be implemented directly here
class Vec3d{
    public:
    double x,y,z;
    inline void set( const double  f         ) { x=f; y=f; z=f; };
    inline void set( const Vec3d&  v          ) { x=v.x; y=v.y; z=v.z; };
    inline void set( const double fx, const double fy, const double fz ) { x=fx; y=fy; z=fz; };

    inline Vec3d& operator =( const double f ) { x=f; y=f; z=f; return *this; };
    inline Vec3d& operator+=( const double f ) { x+=f; y+=f; z+=f; return *this; };
    inline Vec3d& operator*=( const double f ) { x*=f; y*=f; z*=f; return *this; };
    inline Vec3d& operator+=( const Vec3d&  v ) { x+=v.x; y+=v.y; z+=v.z; return *this; };
    inline Vec3d& operator-=( const Vec3d&  v ) { x-=v.x; y-=v.y; z-=v.z; return *this; };
    inline Vec3d& operator*=( const Vec3d&  v ) { x*=v.x; y*=v.y; z*=v.z; return *this; };
    inline Vec3d& operator/=( const Vec3d&  v ) { x/=v.x; y/=v.y; z/=v.z; return *this; };
    inline Vec3d operator+ ( const double f ) const { Vec3d vo; vo.x=x+f; vo.y=y+f; vo.z=z+f; return vo; };
    inline Vec3d operator* ( double f ) const { Vec3d vo; vo.x=x*f; vo.y=y*f; vo.z=z*f; return vo; };

    inline Vec3d operator+ ( const Vec3d& vi ) const { Vec3d vo; vo.x=x+vi.x; vo.y=y+vi.y; vo.z=z+vi.z; return vo; };
    inline Vec3d operator- ( const Vec3d& vi ) const { Vec3d vo; vo.x=x-vi.x; vo.y=y-vi.y; vo.z=z-vi.z; return vo; };
    inline Vec3d operator* ( const Vec3d& vi ) const { Vec3d vo; vo.x=x*vi.x; vo.y=y*vi.y; vo.z=z*vi.z; return vo; };
    inline Vec3d operator/ ( const Vec3d& vi ) const { Vec3d vo; vo.x=x/vi.x; vo.y=y/vi.y; vo.z=z/vi.z; return vo; };

    inline void fma( const double f, const Vec3d& b ){ x+=f*b.x; y+=f*b.y; z+=f*b.z;       };

    inline void fma( const Vec3d& a, const Vec3d& b ){ x+=a.x*b.x; y+=a.y*b.y; z+=a.z*b.z; };
    inline void fms( const Vec3d& a, const Vec3d& b ){ x-=a.x*b.x; y-=a.y*b.y; z-=a.z*b.z; };
    inline void fda( const Vec3d& a, const Vec3d& b ){ x+=a.x/b.x; y+=a.y/b.y; z+=a.z/b.z; };
    inline void fds( const Vec3d& a, const Vec3d& b ){ x-=a.x/b.x; y-=a.y/b.y; z-=a.z/b.z; };

    inline Vec3d fma_func( const double f, const Vec3d& b ){ Vec3d vo; vo.x=x+f*b.x;   vo.y=y+f*b.y;   vo.x=z+f*b.z;   return vo; };
    inline Vec3d fma_func( const Vec3d& a, const Vec3d& b ){ Vec3d vo; vo.x=x+a.x*b.x; vo.y=y+a.y*b.y; vo.z=z+a.z*b.z; return vo; };
    inline Vec3d fms_func( const Vec3d& a, const Vec3d& b ){ Vec3d vo; vo.x=x-a.x*b.x; vo.y=y-a.y*b.y; vo.z=z-a.z*b.z; return vo; };
    inline Vec3d fda_func( const Vec3d& a, const Vec3d& b ){ Vec3d vo; vo.x=x+a.x/b.x; vo.y=y+a.y/b.y; vo.z=z+a.z/b.z; return vo; };
    inline Vec3d fds_func( const Vec3d& a, const Vec3d& b ){ Vec3d vo; vo.x=x-a.x/b.x; vo.y=y-a.y/b.y; vo.z=z-a.z/b.z; return vo; };

    inline double dot ( const Vec3d& a ){ return x*a.x + y*a.y + z*a.z;  };
    inline double mag2(                ){ return x*x+y*y+z*z;            };

    inline void  cross     ( const Vec3d& a ){ double _x=y*a.z-z*a.y;   double _y=z*a.x-x*a.z; z=x*a.y-y*a.x;     x=_x; y=_y;  };
    inline Vec3d cross_func( const Vec3d& a ){ Vec3d vo; vo.x=y*a.z-z*a.y; vo.y=z*a.x-x*a.z; vo.z=x*a.y-y*a.x;   return vo;   };

};