linking files in c( multiple definition of…)

后端 未结 2 1531
醉话见心
醉话见心 2021-01-28 16:33

Im trying to link a few files in c and im getting this erorr: \"multiple definition of createStudentList\"

my main.c:

#include \"students.h\" 

int mai         


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-01-28 17:32

    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.

    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 
    
    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.

提交回复
热议问题