简体   繁体   English

C包含错误多定义错误

[英]C include error multiple definition error

I'm encountering a classic error but still don't get why it occurs... 我遇到一个经典错误,但仍然不知道为什么会发生...

Below is the simplified explanation Apparently I have two C files main.c and support.c 下面是简化的说明显然我有两个C文件main.c和support.c

in support.ci have one function void bla(int input); 在support.ci中有一个函数void bla(int input);

in main.ci have several functions using bla from support.c, and i included 在main.ci中,使用了来自support.c的bla的几个功能,并且包括

#include<support.c> 

at the top of main.c 在main.c的顶部

However I cannot compile the project because of the error multiple definition of bla, first defined here (eclipse points to the definition of bla in support.c) 但是由于错误bla的多重定义(这里首先定义),我无法编译项目(eclipse指向support.c中的bla的定义)

I know that optimally I would have to create header file support.h and gives prototype extern void bla(int input) there, but for this I have to include the .c file. 我知道最佳情况下,我将必须创建头文件support.h并在其中提供原型extern void bla(int输入),但是为此,我必须包括.c文件。

Thanks in advance. 提前致谢。

The preprocessor will copy the contents of support.c , and paste it to main.c to replace the line #include<support.c> . 预处理器将复制support.c的内容,并将其粘贴到main.c以替换行#include<support.c> So there are two definition of the function bla , one in support.c , the other in main.c . 因此,函数bla有两个定义,一个在support.c ,另一个在main.c

The solution is, don't include an source file. 解决的办法是,不包括源文件。 Put the declarations of functions that you want to export in a header file support.h , and include the header file in main.c : 将要导出的函数的声明放在头文件support.h ,并将头文件包括在main.c

#include "support.h"

You don't include source files into other source files. 您不要将源文件包括在其他源文件中。 Instead you make a header file (with the extension .h ) that contains declarations of functions, ie function prototypes. 而是创建一个头文件(扩展名为.h ),其中包含函数的声明 ,即函数原型。 Then you build both source files separately, and link them together to form the final executable. 然后,您分别构建两个源文件,并将它们链接在一起以形成最终的可执行文件。

So a header file support.h like 所以头文件support.h喜欢

#ifndef SUPPORT_H
#define SUPPORT_H

void blah(void);

#endif

(The preprocessor #ifdef / #define / #endif things are for include guards , to protect from multiple inclusion in the same source file.) (预处理程序#ifdef / #define / #endif适用于include guards ,以防止在同一源文件中多次包含。)

Then the support.c source file 然后是support.c源文件

#include "support.h"

void blah(void)
{
    /* Some code here... */
}

And finally the main.c source file 最后是main.c源文件

#include "support.h"

int main(void)
{
    blah();
    return 0;
}

If you have an IDE (like Visual Studio) if you add these files to your project the IDE will make sure everything is built and linked properly. 如果您具有IDE(如Visual Studio),并且将这些文件添加到项目中,则IDE将确保所有内容均已正确构建和链接。 If you're compiling on the command line, compile each source file into an object file (usually using an option like -c (used for GCC and clang)) and then link the two object files together to create the executable. 如果在命令行上进行编译,请将每个源文件编译成一个目标文件(通常使用-c类的选项(用于GCC和clang)),然后将两个目标文件链接在一起以创建可执行文件。


Command line example with GCC: GCC的命令行示例:

$ gcc -Wall -c main.c -o main.o
$ gcc -Wall -c support.c -o support.o
$ gcc main.o support.o -o my_program

The above three commands will first compile the source files into object files, and then link them together. 上面的三个命令将首先将源文件编译为目标文件,然后将它们链接在一起。

What compiler are you using? 您正在使用什么编译器?

When compiling, make sure you do this: 编译时,请确保执行以下操作:

gcc main.c support.c -o yourProgram

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

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