在一个项目中创建许多cpp文件 [英] create many cpp files in one project

查看:201
本文介绍了在一个项目中创建许多cpp文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以给我一些例子,说明如何在一个主要的cpp中,在c ++中传递不同的cpp文件。

解决方案

在大于一个简单的Hello World的项目中,更多的.cpp(技术上称为翻译单元)被编写,以逻辑上分离应用程序的各部分,并减少编译次数。



请注意,不同的cpp文件不会合并在一个主要的.cpp(因为我认为你认为从你的问题),但每个.cpp是自己编译;编译器会为每个.cpp生成一个对象模块(通常为.o或.obj),然后调用链接器将这些模块链接在一起以生成最终的可执行文件。



如何使这项工作?在手动调用编译器的小项目中,你可以调用指定命令行上的所有.cpps的编译器,让它重新编译所有内容并自己调用链接器。



当程序越来越大时,通常委托任务调用编译器只对修改的文件,然后运行链接器到某种实用程序:许多人使用Makefiles,许多人只使用一个IDE管理所有的文件一个项目,并自动调用编译器和链接器只需按一个按钮。实际上,在不同的步骤中手动调用编译器和链接器是很不常见的。



为了使各种模块之间的通信成为可能,C ++允许只是 / em>函数/类/变量与prototypes / extern声明(通常在头文件中完成),而不需要在当前翻译单元中实际定义它们。这允许编译器检查语法并发出用于过程调用的代码,并指示链接器在其他对象模块中查找这些缺失的部分。



什么通常做的是关联到每个.cpp一个.hpp(或.h如果你是老式:))头包含所有的原型和声明相对于它的.cpp,应该可以从其他模块访问。这样,如果A.cpp需要调用在B.cpp中定义的函数,它可以简单地包括Bh



一个快速示例可以是:



A.hpp



  #ifndef A_CPP_INCLUDED 
#define A_CPP_INCLUDED
// ^^^这些是头部守卫,用于避免同一个头部的多个包含

//声明

//注意,这并不定义一个全局变量,它只是对编译器说在某些模块中有一个全局命名以这种方式,让我访问它
extern int AUselessGlobalVar;
int AddFive(int In);

#endif



A.cpp



  #includeA.hpp//每个.cpp都包含自己的.hpp文件是有用的,所以它有所有的原型

//实际上定义var
int AUselessGlobalVar;

//实际上定义函数
int AddFive(int In)
{
return In + 5;
}



Main.cpp



pre> #include< iostream>

#includeAh//现在可以访问在Ah中声明的所有函数/ var

int main()
{
std: :cin>> AUselessGlobalVar;
std :: cout<<< AddFive(AUselessGlobalVar);
return 0;
}

顺便说一下,在这个博客条目中有一个不错的描述链接的古典模型,这是与我们正在谈论的很相关。


can you give me a few examples of how different cpp files are communicated in one main cpp, in c++?

解决方案

In projects bigger than a simple Hello World more .cpp (technically called "translation units") are written, to separate logically the parts of the application and to decrease compilation times.

Notice that the different cpp files are not merged in a single main .cpp (as I suppose you think from your question), but each .cpp is compiled on its own; the compiler generates an object module (usually .o or .obj) for each .cpp, and then it's the linker is called to link together such modules to produce the final executable.

How to make this work? In little projects where you call the compiler by hand, you may just call the compiler specifying all the .cpps on the command line and let it recompile everything and call the linker by itself.

When the program grows bigger, you usually delegate the task of calling the compiler only on the modified files and then run the linker to some kind of utility: many people use Makefiles, many just use an IDE that manages all the files in a project and automagically invokes the compiler and the linker just pressing a button. Actually it's quite uncommon to manually invoke the compiler and the linker in separate steps.

To make the communication between the various modules possible, C++ allows to just declare functions/classes/variables with prototypes/extern declarations (which usually is done in header files) without actually defining them in the current translation unit. This lets the compiler check the syntax and emit the code for the procedure calls, and instructs the linker to look for these "missing pieces" in the other object modules.

What is usually done is to associate to each .cpp a .hpp (or .h if you are old-fashioned :) ) header which contains all the prototypes and declarations relative to its .cpp that should be accessible from the other modules. In this way, if A.cpp needs to call a function defined in B.cpp, it can simply include B.h.

A quick example may be:

A.hpp

#ifndef A_CPP_INCLUDED
#define A_CPP_INCLUDED
// ^^^ these are header guards, used to avoid multiple inclusions of the same header    

// Declarations

// Notice that this does not define a global variable, it just says to the compiler "in some module there's a global named in this way, let me access it"
extern int AUselessGlobalVar;
int AddFive(int In);

#endif

A.cpp

#include "A.hpp" //it's useful that each .cpp includes its own .hpp, so it has all the prototypes already in place

// Actually define the var
int AUselessGlobalVar;

// Actually define the function
int AddFive(int In)
{
    return In + 5;
}

Main.cpp

#include <iostream>

#include "A.h" // now it can access all the functions/vars declared in A.h

int main()
{
    std::cin>>AUselessGlobalVar;
    std::cout<<AddFive(AUselessGlobalVar);
    return 0;
}

By the way, in this blog entry there's a nice description of the classical model for linking, which is quite related to what we're talking about.

这篇关于在一个项目中创建许多cpp文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆