将定义的函数的函数原型放在不同的源(而不是头)文件中合法/好吗

Is it legal / good to put function prototype of a function defined in a different source (not header) file?

本文关键字:函数 文件 好吗 原型 定义      更新时间:2023-10-16

我不确定我的描述是否恰当地描述了这个问题。当我试图理解外部联系和内部联系时,我发现了这一点。假设我有一个包含2个文件的项目:

//A.cpp
#include <iostream>
void doSomething();
int main()
{
    doSomething();
    return 0;
}
//B.cpp
#include <iostream>
void doSomething()
{
    std::cout << "Doing it" << std::endl;
    std::cin.get();
}

请注意,这两个文件都不是标头。他们只提供2个翻译单元。

我惊讶地发现它可以正确编译和工作。当我在不同的文件中使用相同的实用函数(如线性插值)时,我习惯于编写这样的代码来避免多定义错误:

//A.cpp
#include <iostream>
static void doSomething()
{
    std::cout << "Doing it" << std::endl;
    std::cin.get();
}
int main()
{
    doSomething();
    return 0;
}

//B.cpp
#include <iostream>
static void doSomething()
{
    std::cout << "Doing it" << std::endl;
    std::cin.get();
}
/* some other functions that call doSomething() */

这显然是多余的,上面的方法似乎解决了这个问题。但我想知道这真的是一种被接受的风格吗?如果没有IDE的帮助,甚至无法找到函数的定义。

第一块代码是合法的,但这不是一个好的做法。最好创建一个.h文件,在其中放置函数原型,并在所有使用该函数的.cc文件中创建#include .h文件。

//B.h
#ifndef B_H
#define B_H
void doSomething();
#endif
//A.cpp
#include <iostream>
#include "B.h"
int main()
{
    doSomething();
    return 0;
}
//B.cpp
#include <iostream>
#include "B.h"
void doSomething()
{
    std::cout << "Doing it" << std::endl;
    std::cin.get();
}

static关键字表示此函数、对象或变量仅在该转换单元中可用,该转换单元通常是一个cpp文件。您可以在多个cpp文件中拥有多个static doSomething函数。

关于链接。为了使用功能,提供原型就足够了。这通常在h文件中完成,但您也可以手动提供非静态函数的函数原型。h文件只是用来定义函数库的原型,以便c文件使用它们。正如我所说,你也可以通过其他方式提供原型。由链接器将这些函数链接在一起。