Calling a CPP function from C with no header file

huangapple go评论71阅读模式
英文:

Calling a CPP function from C with no header file

问题

我不明白,如果没有.h文件,那么如何进行'connection'。这是代码:

文件 main.c:

void Run( void ) ;
int main(void)
{
    Run() ;
}

文件:run.cpp:

static MyClass gMyClassInstance;
extern "C" {
    void Run( void )
    {
       gMyClassInstance->Run();
    }

}

在整个项目中搜索了Run,再也没有出现,也没有在任何.h文件中。它是怎么做到的?

英文:

I don't get it, how is the 'connection' made if there is no .h file.

Here is the code:

file main.c:

void Run( void ) ;
int main(void)
{
    Run() ;
}

file: run.cpp:

static MyClass gMyClassInstance;
extern "C" {
    void Run( void )
    {
       gMyClassInstance->Run();
    }

}

Searched for Run in the entire project, never appears again and not in any .h file. How does it do it?

答案1

得分: 6

.h文件用于避免在.c/.cpp文件之间重复声明(当函数在本地未被定义时,编译器需要这些声明)。

在你的例子中,只有一个声明 void Run( void );,直接在main.c中,而不是在一个单独的.h文件中,这对于编译器来说已经足够了。与run.cpp中定义的Run函数的连接将由链接器完成(作为名为_Run的入口点)。

在C/C++编程中,头文件绝非强制要求。

英文:

.h files are used to avoid duplicating the declarations from .c/.cpp file to .c/.cpp file (declarations are required by the compiler when the functions are not defined locally).

In your example, there is a single declaration void Run( void );, directly in main.c rather than in a separate .h, and this is enough for the compiler. The connection with the Run function defined in run.cpp will be made by the linker (as an entry point named _Run).

Header files are by no means mandated in C/C++ programming.

答案2

得分: 3

在C和C++中,每个.cpp或.c文件都被编译为独立的翻译单元。这个过程包括三个阶段:预处理、编译和链接。

在预处理阶段,#include命令被解释为在该点复制和粘贴头文件的内容。每个预处理文件都是文本文件,仍然是有效的C或C++代码。如果你想了解更多关于这个过程的信息,你的编译器可能允许你对文件进行预处理并观察结果文件。

然后,预处理文件被分别编译。从编译器的角度来看,函数声明最初在哪里并不重要,因为它处理的是预处理文件。你可以将预处理文件添加到项目中作为.cpp文件,它仍然可以编译。

因为文件被分别编译,所以通常会调用在不同翻译单元中定义的函数。函数声明只是告诉编译器该函数在程序的某个地方可以找到。

在每个翻译单元编译完成后,链接器会处理它们以生成最终的程序。这包括查找每个函数定义,并建立与每个函数调用的连接。如果找不到定义,就会出现错误。

英文:

In C and C++, each .cpp or .c file is compiled as an individual translation unit. There are three phases in the process that are relevant here: Preprocessing, compilation, and linking.

During preprocessing, #include command is interpreted to copy and paste the contents of the header file at that point. Each preprocessed file is a text file, and is still valid C or C++. Your compiler probably allows you to preprocess a file and observe the result file, if you want to find out more about the process.

Preprocessed files are then compiled separately. From the viewpoint of the compiler, it doesn't matter where the function declaration was initially, because it works on the preprocessed files. You could take the preprocessed file, add it to your project as .cpp file, and it would still compile.

Because the files are compiled separately, it is common to call functions that are defined in a different translation unit. A function declaration simply tells the compiler that the function will be found somewhere in the program.

After every translation unit has been compiled, the linker processes them to make a final program. This includes finding each function definition, and making the connection with each function call. If the definition is not found, there will be an error.

huangapple
  • 本文由 发表于 2023年5月10日 17:52:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定