C / C ++头和实现文件:它们如何工作? [英] C/C++ header and implementation files: How do they work?

查看:218
本文介绍了C / C ++头和实现文件:它们如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但我已经在这里和网络上搜索了一段时间,无法找到一个明确的答案(我的尽职调查)。



所以我刚开始编程...我的问题是,main函数如何知道不同文件中的函数定义(实现)?



ex。说我有3个文件




  • main.cpp

  • myfunction.cpp

  • myfunction.hpp






  // main.cpp 

#includemyfunction.hpp
int main(){
int A = myfunction(12);
...
}

-

  // myfunction.cpp 

#includemyfunction.hpp
int myfunction(int x){
return x * x;
}

-

  // myfunction.hpp 

int myfunction(int x);

-



包括头部代码,但是头部和主函数如何知道函数定义存在,更少利用它?



如果这不清楚,我道歉

函数/类 - 例如:

告诉编译器何时编译 .cpp 文件什么函数/类可用。



.cpp 文件定义了这些函数 - 即编译器编译代码,因此产生实际的机器代码来执行在相应的 .hpp code>文件



在您的示例中, main.cpp 包括 .hpp 文件。预处理器用 .hpp 文件的内容替换 #include 。这个文件告诉编译器函数 myfunction 在其他地方定义,它需要一个参数( int )并返回 int



所以当你编译 main.cpp 到目标文件(.o扩展)中,它在该文件中记录它需要函数 myfunction 。当你将 myfunction.cpp 编译成一个目标文件时,目标文件中有一个注释,它具有 myfunction



然后,当你把两个目标文件连接成一个可执行文件时,链接器将结尾连接起来,即 main.o 使用 myfunction.o 中定义的 myfunction



我希望能帮助


This is probably a stupid question, but I've searched for quite a while now here and on the web and couldn't come up with a clear answer (did my due diligence googling).

So I'm new to programming... My question is, how does the main function know about function definitions (implementations) in a different file?

ex. Say I have 3 files

  • main.cpp
  • myfunction.cpp
  • myfunction.hpp

//main.cpp

#include "myfunction.hpp"
int main() {
  int A = myfunction( 12 );
  ...
}

-

//myfunction.cpp

#include "myfunction.hpp"
int myfunction( int x ) {
  return x * x;
}

-

//myfunction.hpp

int myfunction( int x );

-

I get how the preprocessor includes the header code, but how do the header and main function even know the function definition exists, much less utilize it?

I apologize if this isn't clear or I'm vastly mistaken about something, new here

解决方案

The header file declares functions/classes - i.e. tells the compiler when it is compiling a .cpp file what functions/classes are available.

The .cpp file defines those functions - i.e. the compiler compiles the code and therefore produces the actual machine code to perform those actions that are declared in the corresponding .hpp file.

In your example, main.cpp includes a .hpp file. The preprocessor replaces the #include with the contents of the .hpp file. This file tells the compiler that the function myfunction is defined elsewhere and it takes one parameter (an int) and returns an int.

So when you compile main.cpp into object file (.o extension) it makes a note in that file that it requires the function myfunction. When you compile myfunction.cpp into an object file, the object file has a note in it that it has the definition for myfunction.

Then when you come to linking the two object files together into an executable, the linker ties the ends up - i.e. main.o uses myfunction as defined in myfunction.o.

I hope that helps

这篇关于C / C ++头和实现文件:它们如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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