现在的位置: 首页 > 综合 > 正文

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

2013年10月08日 ⁄ 综合 ⁄ 共 2541字 ⁄ 字号 评论关闭

在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里添加文件即可。

抱歉!评论已关闭.