I'm a C newbie and I was just trying to write a console application with Code::Blocks. Here's the (simplified) code: main.c:
我是一个C新手,我正在尝试用代码::Blocks编写一个控制台应用程序。这里是(简化的)代码:main.c:
#include <stdio.h>
#include <stdlib.h>
#include "test.c" // include not necessary for error in Code::Blocks
int main()
{
//t = test(); // calling of method also not necessary
return 0;
}
test.c:
test.c:
void test() {}
When I try to build this program, it gives the following errors:
当我尝试构建这个程序时,它会出现以下错误:
*path*\test.c|1|multiple definition of `_ test'| obj\Debug\main.o:*path*\test.c|1|first defined here|
There is no way that I'm multiply defining test (although I don't know where the underscore is coming from) and it seems highly unlikely that the definition is somehow included twice. This is all the code there is.
我不可能将定义测试相乘(尽管我不知道下划线来自哪里),而且似乎不太可能以某种方式将定义包含两次。这就是所有的代码。
I've ruled out that this error is due to some naming conflict with other functions or files being called test or test.c. Note that the multiple and the first definition are on the same line in the same file.
我已经排除了这个错误是由于一些命名冲突与其他函数或文件被称为测试或测试。注意,多个定义和第一个定义在同一个文件中的同一行上。
Does anyone know what is causing this and what I can do about it? Thanks!
有人知道是什么导致了这种情况吗?我能做些什么?谢谢!
7 个解决方案
#1
70
You actually compile the source code of test.c
twice:
您实际上编译了测试的源代码。c两次:
- The first time when compiling
test.c
itself, - 第一次编译测试。c本身,
- The second time when compiling
main.c
which includes all thetest.c
source. - 第二次编译时主。包括所有的测试。c源代码。
What you need in your main.c
in order to use the test()
function is a simple declaration, not its definition. This is achieved by including a test.h
header file which contains something like:
你主要需要什么。为了使用test()函数,c是一个简单的声明,而不是它的定义。这是通过包含一个测试来实现的。h头文件包含如下内容:
void test(void);
This informs the compiler that such a function with input parameters and return type exists. What this function does ( everything inside {
and }
) is left in your test.c
file.
这通知编译器这样一个具有输入参数和返回类型的函数存在。这个函数的功能({和}中的所有内容)都留在测试中。c文件。
In main.c, replace #include "test.c"
by #include "test.h"
.
在主。c,取代# include”测试。# c”包括“test.h”。
A last point: with your programs being more complex, you will be faced to situations when header files may be included several times. To prevent this, header sources are sometimes enclosed by specific macro definitions, like:
最后一点:由于您的程序更复杂,您将面临一些情况,当头文件可能被包括几次。为了防止这种情况发生,头消息源有时被特定的宏定义包围,比如:
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
void test(void);
#endif
#2
22
The underscore is put there by the compiler and used by the linker. The basic path is:
下划线由编译器放在那里,由链接器使用。的基本路径是:
main.c
test.h ---> [compiler] ---> main.o --+
|
test.c ---> [compiler] ---> test.o --+--> [linker] ---> main.exe
So, your main program should include the header file for the test module which should consist only of declarations, such as the function prototype:
因此,您的主程序应该包含测试模块的头文件,该头文件应该只包含声明,如函数原型:
void test(void);
This lets the compiler know that it exists when main.c is being compiled but the actual code is in test.c, then test.o.
这让编译器知道它在main时存在。c正在编译中,但是实际的代码正在测试中。c,然后test.o。
It's the linking phase that joins together the two modules.
它是连接两个模块的连接阶段。
By including test.c into main.c, you're defining the test() function in main.o. Presumably, you're then linking main.o and test.o, both of which contain the function test().
通过测试。c为主要。c,您正在main.o中定义test()函数。假设你是连接main。o和测试。o,它们都包含函数test()。
#3
9
You shouldn't include other source files (*.c) in .c files. I think you want to have a header (.h) file with the DECLARATION of test function, and have it's DEFINITION in a separate .c file.
您不应该在.c文件中包含其他源文件(*.c)。我认为您希望有一个标头(.h)文件和测试函数声明,并将其定义在一个单独的.c文件中。
The error is caused by multiple definitions of the test function (one in test.c and other in main.c)
这个错误是由测试函数的多个定义引起的(一个在测试中)。c和其他的
#4
5
I had similar problem and i solved it following way.
我也遇到过类似的问题,我用同样的方法解决了它。
Solve as follows:
解决如下:
Function prototype declarations and global variable should be in test.h file and you can not initialize global variable in header file.
函数原型声明和全局变量应该在测试中。h文件,不能初始化头文件中的全局变量。
Function definition and use of global variable in test.c file
测试中全局变量的函数定义和使用。c文件
if you initialize global variables in header it will have following error
如果在header中初始化全局变量,则会出现以下错误
multiple definition of `_ test'| obj\Debug\main.o:path\test.c|1|first defined here|
' _ test'| obj\Debug\main.o:path\test的多重定义。c | 1 | |首先定义
Just declarations of global variables in Header file no initialization should work.
在头文件中声明全局变量没有初始化应该起作用。
Hope it helps
希望它能帮助
Cheers
干杯
#5
4
Including the implementation file (test.c
) causes it to be prepended to your main.c and complied there and then again separately. So, the function test
has two definitions -- one in the object code of main.c
and once in that of test.c
, which gives you a ODR violation. You need to create a header file containing the declaration of test
and include it in main.c
:
包含实现文件(test.c)将使其预写到主文件中。c,然后分别遵守。因此,函数测试有两个定义——main的对象代码中的一个。c和一次测试。c,这就违反了ODR。您需要创建一个包含测试声明的头文件,并将其包含在main.c:
/* test.h */
#ifndef TEST_H
#define TEST_H
void test(); /* declaration */
#endif /* TEST_H */
#6
4
If you have added test.c to your Code::Blocks project, the definition will be seen twice - once via the #include and once by the linker. You need to:
如果您添加了测试。c到您的代码:::Blocks项目,定义将被看到两次——一次通过#include,一次通过链接器。你需要:
- remove the #include "test.c"
- 删除# include“test.c”
- create a file test.h which contains the declaration: void test();
- 创建一个文件测试。h,包含声明:void test();
include the file test.h in main.c
- 包括文件测试。h c
#7
4
If you're using Visual Studio you could also do "#pragma once" at the top of the headerfile to achieve the same thing as the "#ifndef ..."-wrapping. Some other compilers probably support it as well .. .. However, don't do this :D Stick with the #ifndef-wrapping to achieve cross-compiler compatibility. I just wanted to let you know that you could also do #pragma once, since you'll probably meet this statement quite a bit when reading other peoples code.
如果您正在使用Visual Studio,您还可以在headerfile顶部执行“#pragma once”,以实现与“#ifndef…”—包装相同的功能。其他一些编译器可能也支持它。但是,不要这样做:D坚持使用#ifndef包装来实现跨编译兼容性。我只是想让您知道您也可以做#pragma一次,因为您可能在阅读别人的代码时很可能会遇到这个语句。
Good luck with it
祝你好运,
#1
70
You actually compile the source code of test.c
twice:
您实际上编译了测试的源代码。c两次:
- The first time when compiling
test.c
itself, - 第一次编译测试。c本身,
- The second time when compiling
main.c
which includes all thetest.c
source. - 第二次编译时主。包括所有的测试。c源代码。
What you need in your main.c
in order to use the test()
function is a simple declaration, not its definition. This is achieved by including a test.h
header file which contains something like:
你主要需要什么。为了使用test()函数,c是一个简单的声明,而不是它的定义。这是通过包含一个测试来实现的。h头文件包含如下内容:
void test(void);
This informs the compiler that such a function with input parameters and return type exists. What this function does ( everything inside {
and }
) is left in your test.c
file.
这通知编译器这样一个具有输入参数和返回类型的函数存在。这个函数的功能({和}中的所有内容)都留在测试中。c文件。
In main.c, replace #include "test.c"
by #include "test.h"
.
在主。c,取代# include”测试。# c”包括“test.h”。
A last point: with your programs being more complex, you will be faced to situations when header files may be included several times. To prevent this, header sources are sometimes enclosed by specific macro definitions, like:
最后一点:由于您的程序更复杂,您将面临一些情况,当头文件可能被包括几次。为了防止这种情况发生,头消息源有时被特定的宏定义包围,比如:
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
void test(void);
#endif
#2
22
The underscore is put there by the compiler and used by the linker. The basic path is:
下划线由编译器放在那里,由链接器使用。的基本路径是:
main.c
test.h ---> [compiler] ---> main.o --+
|
test.c ---> [compiler] ---> test.o --+--> [linker] ---> main.exe
So, your main program should include the header file for the test module which should consist only of declarations, such as the function prototype:
因此,您的主程序应该包含测试模块的头文件,该头文件应该只包含声明,如函数原型:
void test(void);
This lets the compiler know that it exists when main.c is being compiled but the actual code is in test.c, then test.o.
这让编译器知道它在main时存在。c正在编译中,但是实际的代码正在测试中。c,然后test.o。
It's the linking phase that joins together the two modules.
它是连接两个模块的连接阶段。
By including test.c into main.c, you're defining the test() function in main.o. Presumably, you're then linking main.o and test.o, both of which contain the function test().
通过测试。c为主要。c,您正在main.o中定义test()函数。假设你是连接main。o和测试。o,它们都包含函数test()。
#3
9
You shouldn't include other source files (*.c) in .c files. I think you want to have a header (.h) file with the DECLARATION of test function, and have it's DEFINITION in a separate .c file.
您不应该在.c文件中包含其他源文件(*.c)。我认为您希望有一个标头(.h)文件和测试函数声明,并将其定义在一个单独的.c文件中。
The error is caused by multiple definitions of the test function (one in test.c and other in main.c)
这个错误是由测试函数的多个定义引起的(一个在测试中)。c和其他的
#4
5
I had similar problem and i solved it following way.
我也遇到过类似的问题,我用同样的方法解决了它。
Solve as follows:
解决如下:
Function prototype declarations and global variable should be in test.h file and you can not initialize global variable in header file.
函数原型声明和全局变量应该在测试中。h文件,不能初始化头文件中的全局变量。
Function definition and use of global variable in test.c file
测试中全局变量的函数定义和使用。c文件
if you initialize global variables in header it will have following error
如果在header中初始化全局变量,则会出现以下错误
multiple definition of `_ test'| obj\Debug\main.o:path\test.c|1|first defined here|
' _ test'| obj\Debug\main.o:path\test的多重定义。c | 1 | |首先定义
Just declarations of global variables in Header file no initialization should work.
在头文件中声明全局变量没有初始化应该起作用。
Hope it helps
希望它能帮助
Cheers
干杯
#5
4
Including the implementation file (test.c
) causes it to be prepended to your main.c and complied there and then again separately. So, the function test
has two definitions -- one in the object code of main.c
and once in that of test.c
, which gives you a ODR violation. You need to create a header file containing the declaration of test
and include it in main.c
:
包含实现文件(test.c)将使其预写到主文件中。c,然后分别遵守。因此,函数测试有两个定义——main的对象代码中的一个。c和一次测试。c,这就违反了ODR。您需要创建一个包含测试声明的头文件,并将其包含在main.c:
/* test.h */
#ifndef TEST_H
#define TEST_H
void test(); /* declaration */
#endif /* TEST_H */
#6
4
If you have added test.c to your Code::Blocks project, the definition will be seen twice - once via the #include and once by the linker. You need to:
如果您添加了测试。c到您的代码:::Blocks项目,定义将被看到两次——一次通过#include,一次通过链接器。你需要:
- remove the #include "test.c"
- 删除# include“test.c”
- create a file test.h which contains the declaration: void test();
- 创建一个文件测试。h,包含声明:void test();
include the file test.h in main.c
- 包括文件测试。h c
#7
4
If you're using Visual Studio you could also do "#pragma once" at the top of the headerfile to achieve the same thing as the "#ifndef ..."-wrapping. Some other compilers probably support it as well .. .. However, don't do this :D Stick with the #ifndef-wrapping to achieve cross-compiler compatibility. I just wanted to let you know that you could also do #pragma once, since you'll probably meet this statement quite a bit when reading other peoples code.
如果您正在使用Visual Studio,您还可以在headerfile顶部执行“#pragma once”,以实现与“#ifndef…”—包装相同的功能。其他一些编译器可能也支持它。但是,不要这样做:D坚持使用#ifndef包装来实现跨编译兼容性。我只是想让您知道您也可以做#pragma一次,因为您可能在阅读别人的代码时很可能会遇到这个语句。
Good luck with it
祝你好运,