如何用Dev C++编译器将多个文件编译?

在Dev C++中,如何将以下三个文件
theClass.h,
theClass.cpp,
useTheClass.cpp
编译成可执行代码?
其中,theClass.h为声明文件包含类的定义等,theClass.cpp为类的函数定义及其它函数实现,useTheClass.cpp为具体应用的程序文件(包含main函数)。

一开始,我直接用include的方法,发现不能顺利通过编译,会出现链接错误,后来通过:
新建工程(建立一个空白的工程即可),然后在工程选项单中添加进这三个文件即可。

另外,如果熟悉makefile,可以自己编写这个makefile文件。

例子:
C++ Primer Plus 第五版 第九章 程序清单9.10,9.11,9.12

 

//  namesp.h

//  create the pers and debts namespaces

namespace  pers
{
    
const int LEN = 40;
    
struct Person
    
{
        
char fname[LEN];
        
char lname[LEN];
    }
;
    
void getPerson(Person &);
    
void showPerson(const Person &);
}


namespace  debts
{
    
using namespace pers;
    
struct Debt
    
{
        Person name;
        
double amount;
    }
;

    
void getDebt(Debt &);
    
void showDebt(const Debt &);
    
double sumDebts(const Debt ar[], int n);
}


//  namesp.cpp -- namespaces

#include 
< iostream >
#include 
" namesp.h "

namespace  pers
{
    
using std::cout;
    
using std::cin;
    
void getPerson(Person & rp)
    
{
        cout 
<< "Enter first name: ";
        cin 
>> rp.fname;
        cout 
<< "Enter last name: ";
        cin 
>> rp.lname;
    }

    
    
void showPerson(const Person & rp)
    
{
        std::cout 
<< rp.lname << "" << rp.fname;
    }

}


namespace  debts
{
    
using namespace pers;
    
void getDebt(Debt & rd)
    
{
        getPerson(rd.name);
        std::cout 
<< "Enter debt: ";
        std::cin 
>> rd.amount;
    }

    
    
void showDebt(const Debt & rd)
    
{
        showPerson(rd.name);
        std::cout 
<<": $" << rd.amount << std::endl;
    }

    
    
double sumDebts(const Debt ar[], int n)
    
{
        
double total = 0;
        
for (int i = 0; i < n; i++)
            total 
+= ar[i].amount;
        
return total;
    }

}
//  namessp.cpp
#include  < iostream >
#include 
" namesp.h "
    
void  other( void );
void  another( void );
  
int  main( void )
{
    
using debts::Debt;
    
using debts::showDebt; 
    Debt golf 
= {"Benny""Goatsniff"}120.0};
    showDebt(golf);
    other();
    another();
    system(
"pause");
    
    
return 0;
}
 
    
void  other( void

    
using std::cout;
    
using std::endl;
    
using namespace debts;
    Person dg 
= {"Doodles""Glister"};
    showPerson(dg); 
    cout 
<< endl; 
    Debt zippy[
3];
      
    
int i; 
    
for(i=0;i<3;i++
        getDebt(zippy[i]); 
    
for(i=0;i<3;i++
        showDebt(zippy[i]); 
    cout
<<"Total debt: $"<<sumDebts(zippy,3)<<endl; 
    
    
return
}
 
    
void  another( void

    
using pers::Person; 
    Person collector
={"Milo","Rightshift"}
    pers::showPerson(collector); 
    std::cout 
<< std::endl; 
}


另外,VC中的多个文件编译于上述不同,它没必要建立工程。可以直接在Project-》Add to Project里添加文件即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值