<legend id='q2kTu'><style id='q2kTu'><dir id='q2kTu'><q id='q2kTu'></q></dir></style></legend>
    <bdo id='q2kTu'></bdo><ul id='q2kTu'></ul>
<i id='q2kTu'><tr id='q2kTu'><dt id='q2kTu'><q id='q2kTu'><span id='q2kTu'><b id='q2kTu'><form id='q2kTu'><ins id='q2kTu'></ins><ul id='q2kTu'></ul><sub id='q2kTu'></sub></form><legend id='q2kTu'></legend><bdo id='q2kTu'><pre id='q2kTu'><center id='q2kTu'></center></pre></bdo></b><th id='q2kTu'></th></span></q></dt></tr></i><div id='q2kTu'><tfoot id='q2kTu'></tfoot><dl id='q2kTu'><fieldset id='q2kTu'></fieldset></dl></div>

  • <tfoot id='q2kTu'></tfoot>
  • <small id='q2kTu'></small><noframes id='q2kTu'>

        C/C++ 头文件和实现文件:它们是如何工作的?

        C/C++ header and implementation files: How do they work?(C/C++ 头文件和实现文件:它们是如何工作的?)

        <small id='ykuDA'></small><noframes id='ykuDA'>

        • <i id='ykuDA'><tr id='ykuDA'><dt id='ykuDA'><q id='ykuDA'><span id='ykuDA'><b id='ykuDA'><form id='ykuDA'><ins id='ykuDA'></ins><ul id='ykuDA'></ul><sub id='ykuDA'></sub></form><legend id='ykuDA'></legend><bdo id='ykuDA'><pre id='ykuDA'><center id='ykuDA'></center></pre></bdo></b><th id='ykuDA'></th></span></q></dt></tr></i><div id='ykuDA'><tfoot id='ykuDA'></tfoot><dl id='ykuDA'><fieldset id='ykuDA'></fieldset></dl></div>

          1. <legend id='ykuDA'><style id='ykuDA'><dir id='ykuDA'><q id='ykuDA'></q></dir></style></legend>

                <tbody id='ykuDA'></tbody>
              <tfoot id='ykuDA'></tfoot>
                • <bdo id='ykuDA'></bdo><ul id='ykuDA'></ul>

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

                  问题描述

                  可能是一个愚蠢的问题,但我已经在这里和网上搜索了很长时间,但找不到明确的答案(我的尽职调查谷歌搜索).

                  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).

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

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

                  例如.假设我有 3 个文件

                  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

                  推荐答案

                  头文件 declares 函数/类 - 即在编译 .cpp 时告诉编译器记录哪些函数/类可用.

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

                  .cpp 文件定义了这些函数 - 即编译器编译代码并因此生成实际的机器代码来执行在相应的 .hpp 中声明的那些操作文件.

                  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.

                  在您的示例中,main.cpp 包含一个 .hpp 文件.预处理器将 #include 替换为 .hpp 文件的内容.这个文件告诉编译器函数 myfunction 是在别处定义的,它接受一个参数(一个 int)并返回一个 int.

                  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.

                  因此,当您将 main.cpp 编译为目标文件(.o 扩展名)时,它会在该文件中注明它需要函数 myfunction.当您将 myfunction.cpp 编译成目标文件时,目标文件中有一个注释,说明它具有 myfunction 的定义.

                  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.

                  然后,当您将两个目标文件链接到一个可执行文件中时,链接器将最终联系起来 - 即 main.o 使用 myfunctionmyfunction.o.

                  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.

                  希望能帮到你

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                  <legend id='DkYsu'><style id='DkYsu'><dir id='DkYsu'><q id='DkYsu'></q></dir></style></legend>

                  <small id='DkYsu'></small><noframes id='DkYsu'>

                      • <i id='DkYsu'><tr id='DkYsu'><dt id='DkYsu'><q id='DkYsu'><span id='DkYsu'><b id='DkYsu'><form id='DkYsu'><ins id='DkYsu'></ins><ul id='DkYsu'></ul><sub id='DkYsu'></sub></form><legend id='DkYsu'></legend><bdo id='DkYsu'><pre id='DkYsu'><center id='DkYsu'></center></pre></bdo></b><th id='DkYsu'></th></span></q></dt></tr></i><div id='DkYsu'><tfoot id='DkYsu'></tfoot><dl id='DkYsu'><fieldset id='DkYsu'></fieldset></dl></div>
                          <tbody id='DkYsu'></tbody>

                        <tfoot id='DkYsu'></tfoot>
                            <bdo id='DkYsu'></bdo><ul id='DkYsu'></ul>