C++多文件编译问题

本文主要介绍了C++在多文件编译时遇到的预编译头问题,包括VS2008的C4627警告的解决方法,如取消预编译头的使用或修改预编译头文件。同时,讨论了#include指令的双引号和尖括号的区别,并给出了多文件结构的组织方式,强调了在每个cpp文件中包含预编译头文件的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:

在查找预编译头使用时跳过解决

环境:VS2008

微软官方解释:
Visual C++Concepts: Building a C/C++ ProgramCompiler Warning (level 1) C4627Error Message
”: skipped when looking for precompiled header use
While searching for the location where a precompiled header is used, thecompiler encountered an #include directive for the include file. The compilerignores the #include directive, but issues warning C4627 if the precompiledheader does not already contain the include file.

解决方案:

1、去掉预编译头
项目->属性->配置属性->c/c++->预编译头->创建使用预编译头->不使用预编译头

2、将包含文件加到预编译头stdafx.h文件中

3、在每个.cpp中包含预编译头stdafx.h文件

 

2: 

#include  “包含文件名”  或 #include  <包含文件名>

两种格式的区别仅在于:

①使用双引号:系统首先到当前目录下查找被包含文件,如果没找到,再到系统指定的“包含文件目录”(由用户在配置环境时设置)去查找。

②使用尖括号:直接到系统指定的“包含文件目录”去查找。一般地说,使用双引号比较保险。

 多文件结构

大程序倾向于分成多个源文件,其理由为:

(1)避免重复编译函数。

(2)使程序更加容量管理。

(3)把相关函数放到一特定源文件中。

 

 3:具体多文件架构

1,  在每个.cpp文件中都要加入#include "stdafx.h" 才不会出现“在查找预编译头使用时跳过”的错误。或者直接去掉预编译头:项目->属性->配置属性->c/c++->预编译头->创建使用预编译头->不使用预编译头

2,  A.cpp文件中要#include “A.h”

在A.cpp文件中如果出现了cout不识别,请加入语句:

#include <iostream>

using namespace std;

这些都是在哪编译错误了,需要用的时候再加。

3,  举例:

node.h:

#defineMAX_LEN 10
#include <string>
using namespace std;
#ifndefNODE
#defineNODE
classnode
{
     public:
         node();
         node(stringname);
         stringgetName() const;
         void setName(string name);
 
     private:
         stringname;
         node*ports[MAX_LEN];
};
#endif

node.cpp:

#include "stdafx.h"
#include "node.h"
#include <iostream>
using namespace std;
 
//构造函数
node::node()
{
     this->name="abcd";
     for(inti=0;i<MAX_LEN;i++)
     {
         ports[i]=NULL;
     }
}
//构造函数
node::node(string name)
{
     this->name=name;
     for(inti=0;i<MAX_LEN;i++)
     {
         ports[i]=NULL;
     }
}
 
//成员函数
string node::getName()const
{
     cout<<name.c_str()<<endl;
     return name;
}
voidnode::setName(string name)
{
     this->name=name;
}

更一般的:

A.h:

/*用ifndef是为了避免其他文件重复#include“A.h”从而是的A.h中变量等被重复声明了。*/
#ifndefA_H
#defineA_H
classA
{
     int a;
public:
     A();
     A(int a);
     void printA();
};
#endif

 

       A.cpp

/*
在每个.cpp文件中都要加入#include "stdafx.h" 才不会出现“在查找预编译头使用时跳过”的错误。或者直接去掉预编译头:项目->属性->配置属性->c/c++->预编译头->创建使用预编译头->不使用预编译头
*/
#include "stdafx.h"
/*A.cpp文件中要#include “A.h”*/
#include "A.h"  
A::A()
{
     a=0;
}
A::A(inta)
{
     this->a=a;
}
voidA::printA()
{
     //....cout...
}


关于继承:

node.h

#defineMAX_LEN 10
#include <string>
using namespace std;
#ifndefNODE_H
#defineNODE_H
classnode
{
     public:
         node();
         node(stringname);
         stringgetName() const;
         void setName(string name);
 
     private:
         stringname;
         node*ports[MAX_LEN];
};
#endif
 

node.cpp

#include "stdafx.h"
#include "node.h"
#include <iostream>
using namespace std;
 
//构造函数
node::node()
{
     this->name="abcd";
     for(inti=0;i<MAX_LEN;i++)
     {
         ports[i]=NULL;
     }
}
//构造函数
node::node(string name)
{
     this->name=name;
     for(inti=0;i<MAX_LEN;i++)
     {
         ports[i]=NULL;
     }
}
 
//成员函数
string node::getName()const
{
     cout<<name.c_str()<<endl;
     return name;
}
voidnode::setName(string name)
{
     this->name=name;
}
 

host继承node

host.h

#include "node.h"
#ifndefHOST_H
#defineHOST_H
classhost:public node
{
 
};
#endif

host.cpp

#include "stdafx.h"
#include "host.h"
 


 

#include "stdafx.h"这个文件主要的作用是用来把公共的头文件诸如#include <iostream>以及其他自定义头文件诸如#include“node.h”包含在其中。这样在每个cpp文件都只需要#include "stdafx.h"就可以了,而无需每个.cpp或.h文件在用到的时候都要包含这些东西了。

    
//*******************************  
//*        头文件:StdAfx.h        *  
//*        描  述:包含所有头文件        *  
//*******************************  
   
// 条件编译  
#ifndef STDAFX_H  
#define STDAFX_H  
  
// 包含工程所需所有头文件  
#include <iostream>  
#include <stdlib.h>  
using namespace std;  
#include "CPoint.h"  
#include "CLine.h"  
#include “test.h”  

#endif STDAFX_H  
 


在我的代码中:

// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
 
#pragma once
 
#include "targetver.h"
 
#include <stdio.h>
#include <tchar.h>
//以下是自己增加的头文件
//1,系统头文件
#include <iostream>
#include <string>
using namespace std;
//2,自定义头文件
#include "A.h"
#include "host.h"
#include "level1Switch.h"
#include "level2Switch.h"
#include "level3Switch.h"
#include "node.h"
#include "Switch.h"
 
 
// TODO: 在此处引用程序需要的其他头文件
 


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值