[英]linking files in c( multiple definition of…)
我正在尝试在c中链接一些文件,并得到以下错误提示:“ createStudentList的多个定义”
我的main.c:
#include "students.h"
int main(void)
{
return 0;
}
students.h:
#ifndef _students_h_
#define _students_h_
#include "students.c"
bool createStudentList();
#endif
students.c:
#include <stdbool.h>
typedef struct Students
{
int id;
double average;
} Student;
bool createStudentList()
{
return true;
}
由于包含在内,您在main.o和student.o中都定义了函数createStudentList()
,这会导致您观察到链接器错误。
我建议执行以下操作。 结构(类型)定义和函数原型应放入头文件中:
#ifndef _students_h_
#define _students_h_
#include <stdbool.h>
typedef struct Students
{
int id;
double average;
} Student;
bool createStudentList(void);
#endif
以及源文件中的实际代码,其中包括头文件
#include "students.h"
bool createStudentList(void)
{
return true;
}
现在,通过包含students.h
您可以在其他源文件中使用类型和函数createStudentList
。
从students.h中删除#include "students.c"
。 因此,定义发生了两次-一个来自students.h,另一个来自students.c-因此发生了冲突。
只需删除上面提到的行,然后在students.h中添加#include <stdbool.h>
。 进行这些修改,您的代码将可以编译和链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.