I am not sure what is the best practice for including multiple c files. I was given two header files for an assignment, and am now required to create the corresponding c files.
Right now my issue is that I need to use functions defined in course.c in student.c, however, I am running into a linker issue that I am not sure how to resolve. The way I am currently including files is by creating multiple function definitions!
student.h
#include <stdbool.h>
#include <stdint.h>
struct course;
struct student;
struct student_id {
uint16_t sid_year;
uint32_t sid_serial;
};
struct student* student_create(struct student_id, bool grad_student);
void student_free(struct student*);
void student_take(struct student *s, struct course*, uint8_t grade);
int student_grade(struct student*, struct course*);
double student_passed_average(const struct student*);
bool student_promotable(const struct student*);
student.c
#include "student.h"
#include "course.c"
struct student
{
<Fields>
};
struct student* student_create(struct student_id id, bool grad_student)
{
<definition>
}
void student_free(struct student* s)
{
<definition>
}
void student_take(struct student *s, struct course* c, uint8_t grade)
{
<definition>
}
int student_grade(struct student* s, struct course* c)
{
<definition>
}
double student_passed_average(const struct student* s)
{
<definition>
}
bool student_promotable(const struct student* s)
{
<definition>
}
course.h
#include <stdint.h>
/** Course subjects. */
enum subject {
SUBJ_ENGI,
SUBJ_CIV,
SUBJ_ECE,
SUBJ_MECH,
SUBJ_ONAE,
SUBJ_PROC,
SUBJ_CHEM,
SUBJ_ENGL,
SUBJ_MATH,
SUBJ_PHYS,
};
struct course;
struct course* course_create(enum subject, uint16_t code);
enum subject course_subject(const struct course*);
uint16_t course_code(const struct course*);
void course_hold(struct course*);
void course_release(struct course*);
int course_refcount(const struct course*);
course.c
#include "course.h"
#include <stdio.h>
#include <stdlib.h>
struct course {
<fields>
};
struct course* course_create(enum subject sub, uint16_t code)
{
<definition>
}
enum subject course_subject(const struct course* c)
{
<definition>
}
uint16_t course_code(const struct course* c)
{
<definition>
}
void course_hold(struct course* c)
{
<definition>
}
void course_release(struct course* c)
{
<definition>
}
int course_refcount(const struct course* c)
{
<definition>
}
Which gives the following errors:
gcc *
multiple definition of `course_create'; ... first defined here ...
multiple definition of `course_subject'; ... first defined here ...
multiple definition of `course_code'; ... first defined here ...
multiple definition of `course_hold'; ... first defined here ...
multiple definition of `course_release'; ... first defined here ...
multiple definition of `ref_count'; ... first defined here ...
I am assuming this is an issue with me including course.c
in student.c
, but I need to include course.c to get the course structure definition. So I am wondering how I can go about retrieving the definition of struct course
without using the line #include course.c