I have a util.h containing a function which will be used in a.h and 'b.h', and further more, a.h and b.h will include each other in order to access some classes defined in each other.
//util.h
#ifndef _UTIL_H_
#define _UTIL_H_
#include <iostream>
void foo()
{
std::cout << "foo\n";
}
#endif
//a.h, it has a a.cpp
#ifndef _A_H_
#define _A_H_
#include "util.h"
#include "b.h"
//some classes' definition
#endif
//b.h, it has a b.cpp
#ifndef _B_H_
#define _B_H_
#include "util.h"
#include "a.h"
//some classes' definition
#endif
My problem is that, I got multiple definition error for foo. How?
I thought the problem might be that, a.h includes util.h and b.h, and b.h includes util.h once again, so I got multiple def error. But it can't seem to make sense, because in util.h I wrote #ifndef/#define guards.
Anyone can give me a help, thanks.
g++ main.cpp a.cpp b.cpp.static void foo() { /* body */ }, if you want to put the function body in the header.UTIL_H_for the header guards.