About Store Forum Documentation Contact



Post Reply 
Including header problem - VS 2012
Author Message
Kiekos Offline
Member

Post: #1
Including header problem - VS 2012
Hey there! This time it's a problem with coding and I'd like to pitch this thread at those who use VS 2012 or anyone who happens to know the solution.

I've created myself a header file with useful functions but I need them to be accessible in all the source files in the project. So there's that Main.h header file that is included in every source file and I thought: "hey, I'm just gonna include my header in the main header and done!" but it turns out it's not that simple...

The compiler returns a number of errors that the function is being redefined as it already exists in Game.obj (or sth like that) although it shouldn't (I mean - including the same header in every source file does not redefine a function). The solution I came up with was to change functions from void to inline void (and so on) and the code works.

But I don't want to keep it that way and simply have my functions (which are in the Functions.h header file) accessible in every source file.

Any ideas?

Thanks,
Kiekos
11-20-2013 03:23 PM
Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #2
RE: Including header problem - VS 2012
What tools are you using: 1.0 or 2.0; code editor or VS only?
11-20-2013 05:13 PM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #3
RE: Including header problem - VS 2012
Only VS 2012
11-20-2013 05:26 PM
Find all posts by this user Quote this message in a reply
para Offline
Member

Post: #4
RE: Including header problem - VS 2012
If I'm understanding you correctly, you could probably use something like:

Code:
#ifndef MY_HEADER
#define MY_HEADER

// header content

#endif
11-20-2013 06:19 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Pixel Perfect Offline
Member

Post: #5
RE: Including header problem - VS 2012
You can use the non standard:

pragma once

too, but personally I prefer the define mechanism outlined by para above
11-20-2013 06:26 PM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #6
RE: Including header problem - VS 2012
@para, I've already tried it! It still didn't work though... I tend to use that in most if not all of my headers.
11-20-2013 07:32 PM
Find all posts by this user Quote this message in a reply
psyco001 Offline
Member

Post: #7
RE: Including header problem - VS 2012
can it be that you declared your functions in that header instead of only defining them?
11-20-2013 09:47 PM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #8
RE: Including header problem - VS 2012
@psyco001, I don't think so... Everything seems to be allright!

Okay guys, here's how you can recreate the problem:

1) Open the VS solution for Tutorials provided with the engine.
2) Open this tutorial: Tutorials\Source\Advanced\5 - Projects\02 - Inventory.
3) Create a header file called "Functions.h" (for example) and define some function there:
Code:
void function(bool &variable)
{
    variable = true;
}
4) Add the function's header file to the project.
5) Put #include "Functions.h" in "Main.h" header file so that the function's header is included in all *.cpp files.
6) Try to compile...

It seems to return an errors of this sort:
Code:
1>Inventory.obj : error LNK2005: "void __cdecl function(bool)" (?function@@YAX_N@Z) already defined in Inventory Gui.obj
1>Item.obj : error LNK2005: "void __cdecl function(bool)" (?function@@YAX_N@Z) already defined in Inventory Gui.obj
1>Main.obj : error LNK2005: "void __cdecl function(bool)" (?function@@YAX_N@Z) already defined in Inventory Gui.obj
1>Player.obj : error LNK2005: "void __cdecl function(bool)" (?function@@YAX_N@Z) already defined in Inventory Gui.obj
1>Tutorials.exe : fatal error LNK1169: one or more multiply defined symbols found

and "(...) defined in Inventory Gui.obj" part is the name of the first *.cpp file in the project - this time it's "Inventory Gui.cpp" while in my project it's "Game.cpp".
(This post was last modified: 11-20-2013 10:18 PM by Kiekos.)
11-20-2013 10:16 PM
Find all posts by this user Quote this message in a reply
psyco001 Offline
Member

Post: #9
RE: Including header problem - VS 2012
yep, you declared it, but you could or should not do that.

you should use a header guard with only the definition in the header and the declaration in a c/cpp file and if you want you can define your function as extern:

Code:
//functions.h
#ifndef FUNCTIONS_H__
#define FUNCTIONS_H__
void function(bool &variable);// or extern void function(...
#endif

Code:
//functions.cpp

#include "functions.h"
void function(bool &variable)
{
    variable = true;
}
(This post was last modified: 11-20-2013 10:29 PM by psyco001.)
11-20-2013 10:28 PM
Find all posts by this user Quote this message in a reply
Pixel Perfect Offline
Member

Post: #10
RE: Including header problem - VS 2012
The code examples correct but the following statement has the descriptions the wrong way round:

(11-20-2013 10:28 PM)psyco001 Wrote:  you should use a header guard with only the definition in the header and the declaration in a c/cpp file and if you want you can define your function as extern:
The header file generally contains the declarations (with possible exceptions such as inline definitions) with the c/cpp file containing the definitions.
11-20-2013 10:58 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #11
RE: Including header problem - VS 2012
If you want to have the definition of the function in the header file,
then you can do this by adding "inline" keyword:

inline void func()
{
}

but I know a better solution:
use EE 2.0 Code Editor grin
11-20-2013 10:59 PM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #12
RE: Including header problem - VS 2012
Shame on me... I've actually tried that but I forgot to #include "stdafx.h" at the beginning of *.cpp and it didn't work lol.

So having only declaration in Functions.h and definition in Functions.cpp does the trick!
Thanks psyco smile

EDIT: @Esenthel, thanks but I know about the "inline" method grin I figured it out but I'm afraid it works only for short functions as the compiler may skip the long ones (not inline them) and they won't work (learnt it hard way lol).

As far as the EE 2.0 Code Editor is concerned... I'm planning to buy the license in a bit and switch to that, so don't worry smile
(This post was last modified: 11-20-2013 11:06 PM by Kiekos.)
11-20-2013 10:59 PM
Find all posts by this user Quote this message in a reply
psyco001 Offline
Member

Post: #13
RE: Including header problem - VS 2012
yeah, you are right i've forgotten the inline because i've never used it.
for me the code is easier to understand and to find when it is seperated
(This post was last modified: 11-20-2013 11:21 PM by psyco001.)
11-20-2013 11:21 PM
Find all posts by this user Quote this message in a reply
Pixel Perfect Offline
Member

Post: #14
RE: Including header problem - VS 2012
(11-20-2013 10:59 PM)Kiekos Wrote:  EDIT: @Esenthel, thanks but I know about the "inline" method grin I figured it out but I'm afraid it works only for short functions as the compiler may skip the long ones (not inline them) and they won't work (learnt it hard way lol).
Whilst a complier is not forced to inline any function marked as such all functions will be implemented even if they are not 'inlined'. Not sure what you mean by 'won't work'?
11-20-2013 11:34 PM
Find all posts by this user Quote this message in a reply
Post Reply