I’m using this header file to read textfiles ( I use it to load shader files) and I use it in two different classes.
I’m getting the error Multiple Definition of textFileRead(char*).
Here is the header file:
#ifndef READFILE_H
#define READFILE_H
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "string"
#include "fstream"
char *textFileRead(char *fn) {
FILE *fp;
char *content = NULL;
int count=0;
if (fn != NULL) {
fp = fopen(fn,"rt");
if (fp != NULL) {
fseek(fp, 0, SEEK_END);
count = ftell(fp);
rewind(fp);
if (count > 0) {
content = (char *)malloc(sizeof(char) * (count+1));
count = fread(content,sizeof(char),count,fp);
content[count] = '\0';
}
fclose(fp);
}
}
return content;
}
#endif READFILE_H
what am I doing wrong?
Functions defined in the header need to be marked as
inlineto prevent multiple definition.Either this, or separate the implementation to an implementation file.
Include guards guard against multiple definition in the same translation unit, this is a linker problem. The symbol is defined multiple times across translation units.