PDA

View Full Version : Xcode #include strangeness


WhatMeWorry
2005.06.30, 09:44 PM
I've got one file where I've put all my user defined types
into. It is named TypeDefs.h

typedef int myType; // one of many user types.

----------------------------------
Source File: MySource.cpp

#include "TypeDefs.h"

myType heyThere; //compiler error: myType does not name a type.

----------------------------------

It's so simple, that I think something very subtle is going on.

I've got many other source files which #include the TypeDefs.h file
with no problems whatsoever. I even just copied and pasted the
typedef directly into the Source file and then the compile works fine.

I'm thinking that Xcode for some reason is simpling not #including the
header file TypeDefs.h. Maybe I've turned off including without
realizing it? Is there a way within Xcode to verify that TypeDefs.h is
being included?

Fenris
2005.06.30, 10:10 PM
Try putting #pragma once at the top of your header. It might work. ;)

WhatMeWorry
2005.07.01, 01:57 AM
Should typedefs go into .h header files or .cpp files? Looking at the typedef statement, lets
take a real simple example:

typedef int Integer;

There is no memory allocated at this time, so that makes it a declaration; Therefore
it should go in the .h file. Right?

OneSadCookie
2005.07.01, 03:22 AM
Questions like that show a fundamental misunderstanding of how "headers" work.

Use the "Preprocess" command in Xcode (or the -E flag to gcc) to see the source that's making it to the compiler. Hopefully that should clear up your understanding.

DoG
2005.07.01, 03:31 AM
Besides that being a pretty pointless declaration, the rule is that public declarations (the typedefs and prototypes you want to use in other modules) go into the header, all the private stuff and definitions go into the implementation file.

Also, note that #pragma once is sort of deprecated (i use it, though), but you can also use
#ifndef __MYFILE_H__
#define __MYFILE_H__
/*code goes here*/
#endif
as a "header guard".