As programs get larger,

本文介绍如何在IDE中创建并编译多文件项目,使用前向声明解决跨文件函数调用问题,并探讨了大型项目中管理多个源文件的重要性。

As programs get larger, it is not uncommon to split them into multiple files for organizational purposes. One advantage of working with an IDE is they make working with multiple files much easier. You already know how to create and compile single-file projects. Adding new files to existing projects is very easy.

In Visual Studio 2005 Express, right click on “Source Files” in the Solution Explorer window on the left, and choose Add -> New Item. Give the new file a name, and it will be added to your project.

In Code::Blocks, go to the file menu and choose “new file”. Give the new file a name, and Code::Blocks will ask you if you want to add it to the active project. Click “Yes”. Note that you will also have to click the “Release” and “Debug” checkboxes, to make sure it gets added to both versions.

Compile your project just the same as before. Couldn’t be much easier!

Now, consider the following multiple-file program:

add.cpp:

1
2
3
4
int add( int x, int y)
{
     return x + y;
}

main.cpp:

1
2
3
4
5
6
7
8
#include <iostream>
 
int main()
{
     using namespace std;
     cout << "The sum of 3 and 4 is: " << add(3, 4) << endl;
     return 0;
}

Try compiling this program for yourself. You will note that it doesn’t compile, and it gives the same compiler error as the program in the previous lesson where the functions were declared in the wrong order:

add.cpp(10) : error C3861: 'add': identifier not found
add.cpp(15) : error C2365: 'add' : redefinition; previous definition was 'formerly unknown identifier'

When the compiler is compiling a code file, it does not know about the existence of functions that live in any other files. This is done so that files may have functions or variables that have the same names as those in other files without causing a conflict.

However, in this case, we want main.cpp to know about (and use) the add() function that lives in add.cpp. To give main.cpp access to the add function, we can use a forward declaration:

main.cpp with forward declaration:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
 
int add( int x, int y); // forward declaration using function prototype
 
int main()
{
     using namespace std;
     cout << "The sum of 3 and 4 is: " << add(3, 4) << endl;
     return 0;
}

Now, when the compiler is compiling main.cpp, it will know what add is. Using this method, we can give files access to functions that live in another file. However, as programs grow larger and larger, it becomes tedious to have to forward declare every function you use that lives in a different file. To solve that problem, the concept of header files was introduced. We discuss header files in the lesson onheader files.

Try compiling add.cpp and the main.cpp with the forward declaration for yourself. We will begin working with multiple files a lot once we get into object-oriented programming, so now’s as good a time as any to make sure you understand how to add and compile multiple file projects.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值