[英]linking files in c( multiple definition of…)
Im trying to link a few files in c and im getting this erorr: "multiple definition of createStudentList" 我正在尝试在c中链接一些文件,并得到以下错误提示:“ createStudentList的多个定义”
my main.c: 我的main.c:
#include "students.h"
int main(void)
{
return 0;
}
students.h: students.h:
#ifndef _students_h_
#define _students_h_
#include "students.c"
bool createStudentList();
#endif
students.c: students.c:
#include <stdbool.h>
typedef struct Students
{
int id;
double average;
} Student;
bool createStudentList()
{
return true;
}
Due to the includes, you have the function createStudentList()
defined in both main.o and student.o , which leads to the linker error you observe. 由于包含在内,您在main.o和student.o中都定义了函数createStudentList()
,这会导致您观察到链接器错误。
I would suggest to do the following. 我建议执行以下操作。 The structure (type) definition and function prototype should go into the header file: 结构(类型)定义和函数原型应放入头文件中:
#ifndef _students_h_
#define _students_h_
#include <stdbool.h>
typedef struct Students
{
int id;
double average;
} Student;
bool createStudentList(void);
#endif
and the actual code in the sourcefile, which includes the headerfile 以及源文件中的实际代码,其中包括头文件
#include "students.h"
bool createStudentList(void)
{
return true;
}
Now you can use both the type and the function createStudentList
in other source files by including students.h
. 现在,通过包含students.h
您可以在其他源文件中使用类型和函数createStudentList
。
Remove #include "students.c"
from students.h. 从students.h中删除#include "students.c"
。 Because of this the definition is occuring twice - one from students.h and another from students.c - hence the clash. 因此,定义发生了两次-一个来自students.h,另一个来自students.c-因此发生了冲突。
Just remove the above mentioned line and also add #include <stdbool.h>
in your students.h. 只需删除上面提到的行,然后在students.h中添加#include <stdbool.h>
。 Do these modifications and your code will compile and link fine. 进行这些修改,您的代码将可以编译和链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.