在头文件中定义 C++ 函数是一个好习惯吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25274312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:17:36  来源:igfitidea点击:

Is it a good practice to define C++ functions inside header files?

c++functionheader-files

提问by Nobody

I'm wondering if it's a good practice to store C++ regular functions, not methods(the ones in classes) inside header files.

我想知道将 C++ 常规函数而不是方法(类中的方法)存储在头文件中是否是一个好习惯。

Example:

例子:

#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED

int add(int a, int b)
{
   return a + b;
}

#endif

And Use it like this:

并像这样使用它:

#include <iostream>
#include "Functions.h"

int main(int argc, char* args[])
{
    std::cout << add(5, 8) << std::endl;
    return 1;
}

Is this a good a good practice? Thanks in advance!

这是一个好习惯吗?提前致谢!

回答by Some programmer dude

If you want to use a function in multiple source files (or rather, translation units), then you place a function declaration(i.e. a function prototype) in the header file, and the definitionin one source file.

如果你想在多个源文件(或者更确切地说,翻译单元)中使用一个函数,那么你将一个函数声明(即一个函数原型)放在头文件中,并将定义放在一个源文件中。

The when you build, you first compile the source files to object files, and then you link the object files into the final executable.

构建时,首先将源文件编译为目标文件,然后将目标文件链接到最终的可执行文件中。



Example code:

示例代码:

  • Header file

    #ifndef FUNCTIONS_H_INCLUDED
    #define FUNCTIONS_H_INCLUDED
    
    int add(int a, int b);  // Function prototype, its declaration
    
    #endif
    
  • First source file

    #include "functions.h"
    
    // Function definition
    int add(int a, int b)
    {
        return a + b;
    }
    
  • Second source file

    #include <iostream>
    #include "functions.h"
    
    int main()
    {
        std::cout << "add(1, 2) = " << add(1, 2) << '\n';
    }
    
  • 头文件

    #ifndef FUNCTIONS_H_INCLUDED
    #define FUNCTIONS_H_INCLUDED
    
    int add(int a, int b);  // Function prototype, its declaration
    
    #endif
    
  • 第一个源文件

    #include "functions.h"
    
    // Function definition
    int add(int a, int b)
    {
        return a + b;
    }
    
  • 第二个源文件

    #include <iostream>
    #include "functions.h"
    
    int main()
    {
        std::cout << "add(1, 2) = " << add(1, 2) << '\n';
    }
    

How you build it depends very much on your environment. If you are using an IDE (like Visual Studio, Eclipse, Xcode etc.) then you put all files into the project in the correct places.

您如何构建它在很大程度上取决于您的环境。如果您使用的是 IDE(如 Visual Studio、Eclipse、Xcode 等),那么您将所有文件放入项目中的正确位置。

If you are building from the command line in e.g. Linux or OSX, then you do like

如果您在 Linux 或 OSX 等命令行中构建,那么您确实喜欢

$ g++ -c file1.cpp
$ g++ -c file2.cpp
$ g++ file1.o file2.o -o my_program

The flag -ctells the compiler to generate an object file, and name it the same as the source file but with a .osuffix. The last command links the two object files together to form the final executable, and names it my_program(that's what the -ooption does, tells the name of the output file).

该标志-c告诉编译器生成一个目标文件,并将其命名为与源文件相同但带有.o后缀。最后一个命令将两个目标文件链接在一起形成最终的可执行文件,并为其命名my_program(这就是-o选项所做的,告诉输出文件的名称)。

回答by Alexey Shmalko

No. If you import the same header from two files, you get redefinition of function.

不会。如果从两个文件中导入相同的头文件,则会重新定义函数。

However, it's usual if the function is inline. Every file needs it's definition to generate code, so people usually put the definition in header.

但是,如果函数是内联的,这很正常。每个文件都需要它的定义来生成代码,因此人们通常将定义放在标题中。

Using staticalso works because of fact that static functions are not exported from object file and in this way can't interfere with other functions with the same name during linkage.

使用static也有效,因为静态函数不是从目标文件中导出的,这样在链接过程中不会干扰其他同名函数。

It's also OK to define member functions inside the classin header as C++ standard considers them as inline.

也可以在classin 头文件中定义成员函数,因为 C++ 标准将它们视为inline.

回答by Nikopol

No. After preprocessing, each source file will contain the header file. Then, at the linking stage you will end up with a multiple definition error because you will have multiple definitions of the same function.

否。预处理后,每个源文件都将包含头文件。然后,在链接阶段,您最终会遇到多个定义错误,因为您将拥有同一函数的多个定义。

Using inlineor staticwill get rid of the linking error. Unless you want the function to be inline, it is best to declarethe function in the header and defineit in a single source file and link it.

使用inlineorstatic将摆脱链接错误。除非您希望函数为inline,否则最好在头文件中声明函数并在单个源文件中定义它并链接它。

If you declare the function as inline, then each of its function call in the source file will be replaced with the code inside the inlined function. So, there's no extra symbol defined.

如果将函数声明为inline,则源文件中的每个函数调用都将替换为inlined 函数中的代码。因此,没有定义额外的符号。

If you declare the function as static, then the function symbol will not be exported from the translation unit. Therefore, no duplicate symbols.

如果将函数声明为static,则不会从翻译单元导出函数符号。因此,没有重复的符号。