multiple decleration error when including header file instead of cpp

Hi i am trying to code an ncurses snake game on linux.
I have these following headers and .cpp files with inclusion guard
point.h -> to define a point struct with x,y
draw.h -> to define some methods to use with ncurses(which includes ncurses)
snake.h -> snake object which includes ("point.h"),(draw.h"),(<unistd>),(<time>)
snake.cpp -> includes ("snake.h")

and finally main.cpp
however if i include snake.h in main, i get multiple definitions of function error
so i tried including snake.cpp and yet it works. Why is that? How can i fix that.
And i use g++ with -std=c++11 -lncurses flags. It really annoyed me after checking all those header inclusion guards etc.

Here are the files in case you would like to check

http://pastie.org/private/pd1516ya4ypf1pccpbdj1a draw.h
http://pastie.org/private/hcvld1rrzryaty14q4cnq point.h
http://pastie.org/private/woxdfvj9ijw03utt3hgjdg snake.h
http://pastie.org/private/pjo7ypioaj1qnpyovfctq snake.cpp
http://pastie.org/private/5ffatsax8b88thlbdgsnea main.cpp


Oh and i tried multiple ifndef formats like "__" at the end or no underscore at all result is the same i dont think compiler considers underscores during that


Thank you
Last edited on
Thank you for your help!

I have read that yesterday when i faced that issue. I guess i am missing something. Could you elaborate what you meant by point.h a little more with a simple example. I really really would appreciate it.
I'd recommend reading this article by Disch.

http://www.cplusplus.com/forum/articles/10627/

Anyways, you are including point.h and draw.h inside of snake.h then including snake.h in main.cpp. That works perfectly fine - if you use the header guards properly. :P You're guarding AFTER you include files whereas you should be guarding BEFORE that or else the compiler goes like this: "Hmm ok include point.h, OH WAIT HEADER GUARDS NOW GOTTA STOP!!!" But it's too late. The other headers already got included again.

Correct way:
1
2
3
4
5
6
7
8
#pragma once
//OR
#ifndef _A_H
#define _A_H

#include "b.h"

#endif 


Wrong way:
1
2
3
4
5
6
7
8
#include "b.h"

#pragma once
//OR
#ifndef _A_H
#define _A_H

#endif 


Hope that I helped! =D
It helped, umm but funny thing is i also tried that before and i tried again it still gives me the same error which is
g++ -std=c++11 main.cpp snake.cpp -lncurses
/tmp/ccS2IJeY.o: In function `point::point()':
snake.cpp:(.text+0x0): multiple definition of `point::point()'
/tmp/ccuisl1n.o:main.cpp:(.text+0x0): first defined here
/tmp/ccS2IJeY.o: In function `point::point()':
snake.cpp:(.text+0x0): multiple definition of `point::point()'
/tmp/ccuisl1n.o:main.cpp:(.text+0x0): first defined here
/tmp/ccS2IJeY.o: In function `point::point(int, int)':
snake.cpp:(.text+0x20): multiple definition of `point::point(int, int)'
/tmp/ccuisl1n.o:main.cpp:(.text+0x20): first defined here
/tmp/ccS2IJeY.o: In function `point::point(int, int)':
snake.cpp:(.text+0x20): multiple definition of `point::point(int, int)'
/tmp/ccuisl1n.o:main.cpp:(.text+0x20): first defined here
/tmp/ccS2IJeY.o: In function `init()':
snake.cpp:(.text+0x43): multiple definition of `init()'
/tmp/ccuisl1n.o:main.cpp:(.text+0x43): first defined here
/tmp/ccS2IJeY.o: In function `getMax(bool)':
snake.cpp:(.text+0x85): multiple definition of `getMax(bool)'
/tmp/ccuisl1n.o:main.cpp:(.text+0x85): first defined here
/tmp/ccS2IJeY.o: In function `drawColumn(int, char)':
snake.cpp:(.text+0xe8): multiple definition of `drawColumn(int, char)'
/tmp/ccuisl1n.o:main.cpp:(.text+0xe8): first defined here
/tmp/ccS2IJeY.o: In function `drawLine(int, char)':
snake.cpp:(.text+0x140): multiple definition of `drawLine(int, char)'
/tmp/ccuisl1n.o:main.cpp:(.text+0x140): first defined here
collect2: error: ld returned 1 exit status


only including snake.cpp file in main solves it why is that? :(
Can you post the updated code so I can see if you're doing everything correctly?
http://pastie.org/private/1wqycpu0dgn3geg5yuaf1g draw.h
http://pastie.org/private/2vqbpnj1m0mvgdxskrqmw point.h
http://pastie.org/private/lfirfyf82vcjmigvugqdcq snake.h
http://pastie.org/private/g3xshcx7uwsvtfezzgf6g snake.cpp
http://pastie.org/private/vamcgomxihtqd2kpilubg main.cpp

Really annoying issue. Is it in the code or compiling stage error?
How should i use g++ in this project? I tried using these;
g++ -std=c++11 main.cpp snake.cpp point.h draw.h -lncurses(this one gives multiple definition errors)

g++ -std=c++11 main.cpp snake.h point.h draw.h -lncurses(this one says snake class and methods are missing)
The problem is you're including function implementations in your header files.

In draw.h, you have implementations for
1
2
3
4
void init()
int getMax(bool axis)
void drawColumn(int pos,char krk)
void drawLine(int pos,char krk)

header files are for declarations, not implementations.
As soon as you include draw.h in more than one .cpp module, those functions get compiled multiple times, which is what the linker is trying to tell you.

This has nothing to do with include guards. When snake,cpp is compiled (which includes draw.h), those functions get compiled. Now when main.cpp gets compiled, those function get compiled again. main.cpp does not know those function definitions have been included elsewhere.

Move those function implementations to a .cpp file. They do not belong in a .h file.

Similar problems exist in point.h. snake.h is okay as it does not include any function implementations.
Last edited on
Topic archived. No new replies allowed.