头文件结构中的链接器错误

时间:2022-02-21 15:07:50

I am having header file named data.h

我有头文件名为data.h

#ifndef data_h
#define data_h

struct Student{
    int GPA;
    int coursesCount;
    float tuitionFees;
};
struct person{
    char firstName[11];
    char familyName[21];
    char telephone[11];
    int isStudent;
    struct Student student;
};
int maxCount=20;
struct person person[20];
#endif

In student.h I did something like this :

在student.h中,我做了类似这样的事情:

#ifndef __student__
#define __student__
#include <stdio.h>
#include "data.h"
void getStudentData(struct Student);
#endif

In student.c its like this :

在student.c中它是这样的:

#include "student.h"
void getStudentData(struct Student currentStudent){
     printf("Hi");
}

I get a Linker error when I ran it through another main.c file. which include all headers.

当我通过另一个main.c文件运行它时,我收到链接器错误。其中包括所有标题。

main.c

main.c中

#include <stdio.h>
#include "student.h"
#include "data.h"
int main(){
    getStudentData(person[0].student);
}

What can be reason for this linker error? PLEASE HELP

这个链接器错误的原因是什么?请帮忙

1 个解决方案

#1


2  

Declaring variables in header files is generally a bad idea. In your case, you are declaring two variables in your header file:

在头文件中声明变量通常是个坏主意。在您的情况下,您在头文件中声明了两个变量:

int maxCount=20;
struct person person[20];

Let's fix this by declaring them in a *.c file and creating references to them in the header file.

让我们通过在* .c文件中声明它们并在头文件中创建对它们的引用来解决这个问题。

data.h

#ifndef data_h
#define data_h

struct Student{
    int GPA;
    int coursesCount;
    float tuitionFees;
};

struct person{
    char firstName[11];
    char familyName[21];
    char telephone[11];
    int isStudent;
    struct Student student;
};

extern int maxCount;
extern struct person person[20];

#endif

student.h

#ifndef student_h
#define student_h

#include <stdio.h>
#include "data.h"

void getStudentData(struct Student);

#endif

data.c

#include "data.h"
int maxcount = 20;
struct person person[20];

student.c

#include "student.h"
void getStudentData(struct Student currentStudent){
     printf("Hi");
}

main.c

#include <stdio.h>
#include "data.h"
#include "student.h"

int main(){
    getStudentData(person[0].student);
}

#1


2  

Declaring variables in header files is generally a bad idea. In your case, you are declaring two variables in your header file:

在头文件中声明变量通常是个坏主意。在您的情况下,您在头文件中声明了两个变量:

int maxCount=20;
struct person person[20];

Let's fix this by declaring them in a *.c file and creating references to them in the header file.

让我们通过在* .c文件中声明它们并在头文件中创建对它们的引用来解决这个问题。

data.h

#ifndef data_h
#define data_h

struct Student{
    int GPA;
    int coursesCount;
    float tuitionFees;
};

struct person{
    char firstName[11];
    char familyName[21];
    char telephone[11];
    int isStudent;
    struct Student student;
};

extern int maxCount;
extern struct person person[20];

#endif

student.h

#ifndef student_h
#define student_h

#include <stdio.h>
#include "data.h"

void getStudentData(struct Student);

#endif

data.c

#include "data.h"
int maxcount = 20;
struct person person[20];

student.c

#include "student.h"
void getStudentData(struct Student currentStudent){
     printf("Hi");
}

main.c

#include <stdio.h>
#include "data.h"
#include "student.h"

int main(){
    getStudentData(person[0].student);
}