C++ 链接两个 .cpp 和一个 .h 文件

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

Linking two .cpp and a .h files

c++linker

提问by Overflowh

I'm doing an exercise (from the third chapter of Thinking in C++) but I have a problem linking two .cpp files.

This is the exercise:

我正在做一个练习(来自用 C++ 思考的第三章),但是我在链接两个 .cpp 文件时遇到了问题。

这是练习:

Create a header file (with an extension of ‘.h'). In this file, declare a group of functions by varying the argument lists and return values from among the following: void, char, int, and float. Now create a .cpp file that includes your header file and creates definitions for all of these functions. Each definition should simply print out the function name, argument list, and return type so you know it's been called. Create a second .cpp file that includes your header file and defines int main( ), containing calls to all of your functions. Compile and run your program.

创建一个头文件(扩展名为“.h”)。在此文件中,通过改变参数列表并返回以下值来声明一组函数:void、char、int 和 float。现在创建一个 .cpp 文件,其中包含您的头文件并为所有这些函数创建定义。每个定义都应该简单地打印出函数名称、参数列表和返回类型,以便您知道它已被调用。创建第二个 .cpp 文件,其中包含头文件并定义 int main( ),其中包含对所有函数的调用。编译并运行你的程序。

I've created the three files, but when I try to compile the compiler give me this error:

我已经创建了三个文件,但是当我尝试编译编译器时给我这个错误:

undefined reference to `func1()'

undefined reference to `func2(int)'|

undefined reference to `func3(char, int, double)'|

undefined reference to `func4()'|

||=== Build finished: 4 errors, 0 warnings ===|

对 `func1()' 的未定义引用

对 `func2(int)' 的未定义引用|

对 `func3(char, int, double)' 的未定义引用|

对 `func4()' 的未定义引用|

||=== 构建完成:4 个错误,0 个警告 ===|

Why it cannot found the function declaration?

为什么找不到函数声明?

##EDIT
These are my three files:

##EDIT
这是我的三个文件:

func_ex.h

func_ex.h

void func1(void);
int func2(int i);
char func3(char c, int i, double d);
float func4(void);

func_ex.cpp

func_ex.cpp

#include "func_ex.h"
using namespace std;

void func1(void) {
    cout << "void func1(void)" << endl;
}

int func2(int i) {
    cout << "int func2(int i)" << endl;
}

char func3(char c, int i, double d) {
    cout << "func3(char c, int i, double d)" << endl;
}

float func4(void) {
    cout << "func4(void)" << endl;
}

func_ex_main.cpp

func_ex_main.cpp

#include "func_ex.h"
#include <iostream>
using namespace std;

int main(int argc, char* argv[]) {
    func1();
    int i;
    func2(i);
    char c; double d;
    func3(c, i, d);
    func4();
}

I'm usig GCC as compiler (and Code::Blocks as IDE, but I think that's not important).

我使用 GCC 作为编译器(使用 Code::Blocks 作为 IDE,但我认为这并不重要)。

回答by samoz

It sounds like the file is not finding the functions appropriately. Is the header file included in both files? You can include it like:

听起来该文件没有适当地找到功能。头文件是否包含在两个文件中?你可以像这样包含它:

#include "myheader.h"

Did you make sure to compile both files together? Such as:

您是否确保将两个文件一起编译?如:

gcc -o myprogram file1.cpp file2.cpp

gcc -o myprogram file1.cpp file2.cpp

回答by xda1001

gcc -c func_ex.cpp -o func_ex.o
gcc func_ex_main.cpp func_ex.o -o func_ex_main

回答by Bisher Dokkmak

It looks every beginner is going through this (including myself), in the main.cpp you include the .cpp file and not the .h,

看起来每个初学者都在经历这个(包括我自己), 在 main.cpp 中包含 .cpp 文件而不是 .h

//func_ex_main.cpp
#include "func_ex.cpp" // instead of .h
#include <iostream>
using namespace std;
//etc...

because the .cpp is linked to the header through the include in that .cpp file, and each function definition is linked to the deceleration through the :: notation before the definition.

因为 .cpp 通过该 .cpp 文件中的包含链接到标题,并且每个函数定义通过定义之前的 :: 符号链接到减速。

Remember that the default constructor and destructor in the .h file is declared and defined automatically(if you didn't override or overload it), which will result in a compile error if you just re-defined it in the .cpp file, so if you didn't want to override it just don't mention the constructor neither the destructor in neither the header nor source files.

请记住,.h 文件中的默认构造函数和析构函数是自动声明和定义的(如果你没有覆盖或重载它),如果你只是在 .cpp 文件中重新定义它会导致编译错误, 所以如果您不想覆盖它,请不要在头文件和源文件中提及构造函数和析构函数

回答by Sunny Chan

For those like me who may be using the IDE BloodShed Dev C++.

对于像我这样可能正在使用 IDE BloodShed Dev C++ 的人。

Create a project folder then proceed to place your H files and your CPP files inside your project folder.

创建一个项目文件夹,然后继续将您的 H 文件和 CPP 文件放在您的项目文件夹中。

I had the same problem as mentioned above, the conclusion I came upon was that my compiler was only compiling the H files and wouldn't even read the CPP files linked to it. When I put them in a project folder my compiler would compile all files together hence allowing my code to run.

我遇到了与上面提到的相同的问题,我得出的结论是我的编译器只编译了 H 文件,甚至不会读取链接到它的 CPP 文件。当我将它们放在项目文件夹中时,我的编译器会将所有文件编译在一起,从而允许我的代码运行。

Here is a link of creating a project folder for those using Bloodshed.

这是为使用 Bloodshed 的人创建项目文件夹的链接。

http://www.horstmann.com/bigcpp/help/bloodshed/index.html

http://www.horstmann.com/bigcpp/help/bloodshed/index.html

As for other IDE users, I believe the problem to be similar. IF anyone needs further elaboration just let me know.

至于其他IDE用户,我相信问题是相似的。如果有人需要进一步详细说明,请告诉我。

回答by newbie

I use code::blocks too, and I had the same problem. The default setting in this IDE is that only one file is built. Here's what I did: project -> properties -> build targets -> Select the files related in the bottom right section. Hope this helps!

我也使用 code::blocks,我遇到了同样的问题。此 IDE 中的默认设置是仅构建一个文件。这是我所做的:项目 -> 属性 -> 构建目标 -> 选择右下部分中的相关文件。希望这可以帮助!

回答by Catalin Barakat

First of all, how can you have func1(), func2(), func3() without having a return statement. Try to put your header body inside these (#include guards) :

首先,你怎么能有 func1(), func2(), func3() 没有 return 语句。尝试将您的标题正文放在这些(#include 守卫)中:

#ifndef func_ex.h
#define func_ex.h
/* your code */

#endif 

回答by Nebiat

I think you should have #include"func_x.cpp"in your func_main.cpp

我认为你应该#include"func_x.cpp"在你的func_main.cpp

回答by Andrew Wiens

You need to put an include like this:

你需要像这样放置一个包含:

#include "headerfilename.h"

at the very top of each .cpp document.

在每个 .cpp 文档的最顶部。