Added openfient, switch to c++ duplicate symbol

Member
Posts: 306
Joined: 2009.03
Post: #1
So I wanted to add open fient which requires c++. I changed my delegate to .mm and i added a compiler setting or 2 that they required.

Now I get duplicate symbol messages on some of my global variables. I moved them both to a globals.h and wrapped their decleration as such:
Code:
#ifndef GLOBALS
#define GLOBALS
GGLOptions _GGLOptions;
bool _isTouch;

#endif

This fixed the GGL one but the isTouch one still complains. Clearly adding their code change my include order or build rules somehow but I don't see why it could be a duplicate symbol if its guarded like that. Any thoughts?
Quote this message in a reply
Member
Posts: 306
Joined: 2009.03
Post: #2
I even changed it so globals.h is only included in my pch and never in any file and its still giving duplicate warning. I don't understand how that's possible.
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #3
Don't declare variables in a header - put them in a .c or .m file. In the header just put extern references to them.
Code:
//globals.c
#define GLOBALS_C
GGLOptions _GGLOptions;
bool _isTouch;

Code:
//globals.h
#ifndef GLOBALS_C
#ifndef GLOBALS_H
#define GLOBALS_H
extern GGLOptions _GGLOptions;
extern bool _isTouch;

#endif
#endif
Quote this message in a reply
Member
Posts: 306
Joined: 2009.03
Post: #4
Thanks a lot.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #5
backslash Wrote:Don't declare variables in a header - put them in a .c or .m file. In the header just put extern references to them.

To expand on this, the reason you have to do it this way is because globally scoped variables add a symbol to the object file produced by each compilation module that includes them, and when more than one of these object files are passed to the linker, symbol collisions occur. For example:
  • header.h contains "int foo;"
  • filea.c includes header.h
  • fileb.c includes header.h
  • filea.o contains a symbol named "foo"
  • fileb.o contains a symbol named "foo"
  • When ld tries to stitch filea.o and fileb.o together, it ends up with two symbols named "foo" and can't continue.
Quote this message in a reply
Member
Posts: 306
Joined: 2009.03
Post: #6
Its still not clear though why the #ifndef didn't stop it, but regardless using that solution i was able to fix it.
Quote this message in a reply
Apprentice
Posts: 13
Joined: 2006.10
Post: #7
#ifndef stops only when you include same header twice (or more) in same file.
If filea.c included header.h and some other_header.h which also includes h header.h - only then preprocessor would strip out variable declarations on second time.
Preprocessor processes each c/cpp file individually - it doesn't keep defined defines globally. If it would, then you would not be able to include any header in more than one file.
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #8
When you include a header file, that is logically equivalent to copy and pasting the header into your C file. That means that when you include the header file in two C files you are actually declaring your global variable twice (even though you only actually wrote a single declaration).
By moving the declaration to a C file you are ensuring that it only gets compiled once.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Symbol(s) not found using oalTouch example code monteboyd 2 3,473 Sep 26, 2010 10:49 PM
Last Post: monteboyd
  How do I get iPod Player to respect silent switch? CVogelbusch 0 1,780 Nov 25, 2009 05:34 AM
Last Post: CVogelbusch
  Reading the Silent Switch? Bachus 5 3,787 Nov 6, 2008 09:06 PM
Last Post: Bachus