在 C++ 程序中使用多个 .cpp 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6995572/
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 21:06:18  来源:igfitidea点击:

Using multiple .cpp files in c++ program?

c++function

提问by Rakso

I recently moved from Java for C++ but now when I am writing my application I'm not interested in writing everything of the code in the main function I want in main function to call another function but this other function is in another .cpp file.

我最近从 Java 转移到 C++,但是现在当我编写我的应用程序时,我对在 main 函数中编写所有代码不感兴趣,我希望在 main 函数中调用另一个函数,但另一个函数在另一个 .cpp 文件中。

Let me explain better if you wouldn't understand:
I have one file: main.cppinside it I have main function.

如果您不明白,让我更好地解释一下:
我有一个文件:main.cpp在它里面我有 main 函数。

I have the second file: second.cppinside I have a function called second()I want to call this function called second()from my main function..

我有第二个文件:second.cpp里面我有一个函数,second()我想调用second()从我的主函数调用的这个函数..

Any help?

有什么帮助吗?

回答by Puppy

You must use a tool called a "header". In a header you declare the function that you want to use. Then you include it in both files. A header is a separate file included using the #includedirective. Then you may call the other function.

您必须使用称为“标题”的工具。在标头中声明要使用的函数。然后将其包含在两个文件中。标头是使用#include指令包含的单独文件。然后你可以调用另一个函数。

other.h

其他.h

void MyFunc();

main.cpp

主程序

#include "other.h"
int main() {
    MyFunc();
}

other.cpp

其他.cpp

#include "other.h"
#include <iostream>
void MyFunc() {
    std::cout << "Ohai from another .cpp file!";
    std::cin.get();
}

回答by Creat

You should have header files (.h) that contain the function's declaration, then a corresponding .cpp file that contains the definition. You then include the header file everywhere you need it. Note that the .cpp file that contains the definitions also needs to include (it's corresponding) header file.

您应该有包含函数声明的头文件 (.h),然后是包含定义的相应 .cpp 文件。然后,您可以在任何需要的地方包含头文件。请注意,包含定义的 .cpp 文件还需要包含(对应的)头文件。

// main.cpp
#include "second.h"
int main () {
    secondFunction();
}

// second.h
void secondFunction();

// second.cpp
#include "second.h"
void secondFunction() {
   // do stuff
}

回答by m_pGladiator

In C/C++ you have header files (*.H). There you declare your functions/classes. So for example you will have to #include "second.h"to your main.cppfile.

在 C/C++ 中,您有头文件 (*.H)。在那里你声明你的函数/类。因此,例如,您将不得不#include "second.h"对您的main.cpp文件。

In second.hyou just declare like this void yourFunction();In second.cppyou implement it like

second.h你只是像这样声明void yourFunction();second.cpp你实现它像

void yourFunction() { 
   doSomethng(); 
}

Don't forget to #include "second.h"also in the beginning of second.cpp

不要忘记#include "second.h"在开始时second.cpp

Hope this helps:)

希望这可以帮助:)

回答by Michael D. Blake

You can simply place a forward declaration of your second()function in your main.cppabove main(). If your second.cpphas more than one function and you want all of it in main(), put all the forward declarations of your functions in second.cppinto a header file and #includeit in main.cpp.

您可以简单地将您的second()函数的前向声明放在main.cpp上面的main(). 如果你second.cpp有一个以上的功能,你希望所有的它main(),把你的函数的所有前置声明中second.cpp为头文件和#includemain.cpp

Like this-

像这样-

Second.h:

第二.h:

void second();
int third();
double fourth();

main.cpp:

主.cpp:

#include <iostream>
#include "second.h"
int main()
{
    //.....
    return 0;
}

second.cpp:

第二个.cpp:

void second()
{
    //...
}

int third()
{ 
    //...
    return foo;
}

double fourth()
{ 
    //...
    return f;
}

Note that: it is not necessary to #include "second.h"in second.cpp. All your compiler need is forward declarations and your linker will do the job of searching the definitions of those declarations in the other files.

请注意:没有必要#include "second.h"second.cpp. 您的编译器所需要的只是前向声明,而您的链接器将完成在其他文件中搜索这些声明的定义的工作。