I have the following problem if I include a header file to my program. ( To decrease the length of post I deleted the implementation of functions )
My files look like:
// The main file: cturing.cpp
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include "rule.h"
#include "rekurzio.h"
int main(){
}
/////////////////////////////
// rule.h
#ifndef RULE_H
#define RULE_H
struct rule{
char kallapot,kjel;
char vallapot,vjel,velmozdulas;
};
rule getRuleFromString(std::string line){
}
bool addRule(rule szabaly, rule *szabalyok, int &ruleNo){
}
#endif
////////////////////////////
// rekurzio.h
#ifndef REKURZIO_H
#define REKURZIO_H
#include "rule.h"
void startRekurzio(rule*);
#endif
////////////////////////////
// rekurzio.cpp
#include <string>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include "rekurzio.h"
void getOptions(rule *options,rule *szabalyok, char allapot, char betu, int &j){
}
int getHely(int curr, char direction, std::string &comb){
}
bool realRekurzio(char &allapot, int hely, std::string combination, rule *szabalyok){
}
void nextCombination(std::string combination){
}
void startRekurzio(rule *szabalyok){
}
If I try to compile the program, I receive this error:
$ g++ cturing.cpp rekurzio.cpp
/tmp/cchzIu1i.o: In function `getRuleFromString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
rekurzio.cpp:(.text+0x0): multiple definition of `getRuleFromString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccHXKygG.o:cturing.cpp:(.text+0x0): first defined here
/tmp/cchzIu1i.o: In function `addRule(rule, rule*, int&)':
rekurzio.cpp:(.text+0x118): multiple definition of `addRule(rule, rule*, int&)'
/tmp/ccHXKygG.o:cturing.cpp:(.text+0x118): first defined here
collect2: error: ld returned 1 exit status
What could cause that multiple definition error, when everything defined only once?
I have more files and if I include them.. Uhm I've got more of this error message.
On the other hand if I do not include the rekruzio.h file ( and remove the function calls which is from this file ), but still add the rekurzio.cpp to the compiler it still have this problem. Every header file has the #ifndef/#define/#endif preprocessor directives as you can see.
After compiling with removed rekurzio.h import and only the main file ( cturing.cpp ) everything is OK.