简体   繁体   English

如何从另一个文件中的函数调用main的同一文件中的函数

[英]How to call a function in the same file of main from a function in another file

I have a program with multiple headers and cpp files, I want to call a function defined in the same file main is defined (from a function defined in another file). 我有一个包含多个头文件和cpp文件的程序,我想调用在同一文件中定义的函数,主要是从另一个文件中定义的函数定义的。 If I'm correct, the file with the main function never has a corresponding header, right? 如果我是对的,则具有main函数的文件将永远没有相应的标头,对吗? How would I do it? 我该怎么办?

Why wouldn't you be allowed to have a header for functions inside main.cpp ? 为什么不允许您在main.cpp使用函数的标头? Assuming main.cpp defines main and foo , nothing prevents you from having a header containing the declaration of foo . 假设main.cpp定义了mainfoo ,那么没有什么可以阻止您使用包含foo声明的标头。

In fact, some libraries and frameworks make their own main . 实际上,某些库和框架是自己的main In cases like these, you just link against the library that contains the main . 在这种情况下,您只需链接到包含main的库即可。

For example, if you look at the last example code of this page of Boost's Unit Test Framework, you will see there is no main . 例如,如果您查看Boost的单元测试框架页面的最后一个示例代码,您将看到没有main The main is already written for you inside the Boost Unit Test Framework library. 已经在Boost Unit Test Framework库中为您编写了main

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM