0

I have a header file which begins with

#if !defined(__GLOBAL_H)
#define  __GLOBAL_H

then some code followed by

#endif

The code contains only function declarations, some include of other header files and the a few template functions. However, the problem occurs when I add one single line of code. I get linker error that the function I added has already been defined in an object file. I'm using Visual Studio 2012 Premium as compiler. I have tried to remove any existing function from the header file, and that also goes through the compiler. On the other hand, if I add any new line that may be new to the compiler, it refuses to compile saying it has already been defined. Does anyone have any clue what might be wrong or can I have stumbled upon an error inside the compiler itself? (which I highly doubt) Edit: The solution was to declare the function the the header file, but to define it in the CPP file. But the real issue was that when I include a header file for returning an object of the type declared in the header file, it does not compile. It many of the errors "Missing ; in front of *" which was types declared in other header files.

Martin
  • 2,606
  • 4
  • 24
  • 38
  • 1
    Don't put function definitions in the header then. – Kerrek SB Apr 20 '13 at 23:26
  • Thanks, it worked :) However, I got another problem as well. When I include one specific header file, I get many "missing ; before *" when I am returning pointers in functions. – Martin Apr 20 '13 at 23:28
  • Your code has undefined behaviour because of `__GLOBAL_H`, which is [illegal](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – JBentley Apr 20 '13 at 23:53
  • That post says it is legal, besides it should not really matter as we have been instructed to write this as __(NAME OF THE HEADER FILE)_H. I don't think that writing __GLOBAL_H is illegal. – Martin Apr 21 '13 at 00:00
  • @Martin You're wrong, see Mike's answer. From the question I linked to: *Reserved in any scope, including for use as implementation macros: [...] identifiers containing adjacent underscores (or "double underscore")* – JBentley Apr 21 '13 at 00:01
  • @Martin Or, if you prefer the quote from the C++03 standard (also in that link): *Each name that contains a double underscore (_ _) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any use.* – JBentley Apr 21 '13 at 00:03
  • I have removed the double underscores from all my header files in the project and it still does not compile. (Now GLOBAL_H) What do you mean with implementation? Like how the std library is written? – Martin Apr 21 '13 at 00:10
  • @Martin Oh, I wasn't trying to imply this was your problem, in this case. You'll quite often find in C++ that you can do something illegal without anything going wrong, but it's still undefined behaviour and it should be avoided. Yes, the standard library is one example - library headers are free to use reserved words, and don't have to guarantee correct behaviour if you use them too. – JBentley Apr 21 '13 at 00:19
  • Ok, so __(something) is reserved by the implementation. But it still does not compile :( – Martin Apr 21 '13 at 00:39

2 Answers2

2

Although you haven't shown use any code or error messages, I'm guessing that there are function definitions (including the code for the function body), not just declarations, in the header.

These must either be declared inline, which allows them to be defined in more than one translation unit; or moved into a single source file, leaving just the declaration in the header, so they are only defined on one translation unit.

The "One Definition Rule" says that (unless they are inline) functions may only have one definition in the program.

Also, your include guard shouldn't begin with an underscore, nor contain a double underscore; names like that are reserved.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

Adding non-inlined function definitions to header files is generally bad. The compiler will generate code for the function in every file it is included in resulting in the redefintion error you are encountering. Instead you should declare the function in the header and place the definition in a source file (.cpp).

Global.h

#if !defined(__GLOBAL_H)
#define  __GLOBAL_H

void somefunction();  // <-- declaring the function.

#endif

SomeSource.cpp

#include "Global.h"

// Here is where define the function
void somefunction()
{
}
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
  • Adding the code on the .cpp file instead worked. However, when I include another header file in my header file, it protests saying "missing ; before *", which is after each time I return a pointer to something. – Martin Apr 20 '13 at 23:30
  • You may be declaring the function incorrectly. Make sure there's a semi-colon at the end. – Captain Obvlious Apr 20 '13 at 23:31
  • 1
    @Martin: That sounds like it's returning a type that hasn't been declared yet. Things usually need to be declared before they are used. Make sure you're including all the headers you need. – Mike Seymour Apr 20 '13 at 23:31
  • They are. It compiles fine without including the header. But when I include it, it doesn't compile (giving the posts I previously posted). – Martin Apr 20 '13 at 23:35
  • @Martin You should update your question to include the error message you get and function declaration you are adding to the header file. Otherwise we're flying blind. – Captain Obvlious Apr 20 '13 at 23:38