首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

每个cpp资料都会被编译吗

2013-03-14 
每个cpp文件都会被编译吗?先上代码:test1.cpp:#include iostreamusing namespace stdint MAX_VALUE 2

每个cpp文件都会被编译吗?
先上代码:
test1.cpp:

#include <iostream>
using namespace std;

int MAX_VALUE = 20;
namespace{
void g_fun(){
cout << "test1.g_fun(): " << MAX_VALUE << endl;
int tmp;
cin >> tmp;
}
}


Main.cpp:
#include <iostream>
#include "test1.h"
using namespace std;

int main(){
cout << "Main.MAX_VALUE: " << MAX_VALUE << endl;
g_fun();
}
void g_fun(){
cout << "Main.g_fun(): " << MAX_VALUE << endl;
}


头文件test1.h:
#ifndef _TEST1_H_
#define _TEST1_H_
const int MAX_VALUE = 20;
void g_fun();
#endif


各位看官,这里的test1.cpp和Main.cpp应该是一点关系都没有了吧,只不是都在一个项目里而已。在编译Main.cpp的过程中,test1.cpp会被编译吗?
编译应该是只编译入口文件以及引用的文件吧,跟入口无关的文件都编译的话,性能不是会下降吗? 编译 C++ 关联
[解决办法]
都是单个文件独立编译的,最后由链接器链接成可执行文件。
[解决办法]
引用:
引用:都是单个文件独立编译的,最后由链接器链接成可执行文件。根本就没有被引用过的cpp文件也会被编译吗?那是不是有点无用功呢?


你加到工程里面的话,就会被编译,编译时可不知道这个文件没有用到。

热点排行