Xcode: Wrong file extension can cause STL errors

Xcode relies on the file extension to figure out if a module should be interpreted as C, C++, Objective-C or Objective-C++. These extensions are, respectively: .c, .cpp, .m, .mm. Xcode also looks at a per-file project setting, which can be accessed from the IDE by selecting the file and pressing Command-I (Get Info). The setting is under General/File Type. The project setting overrides the type that is derived from the file name.

A problem arises when using precompiled headers. Since in Xcode precompiled headers are compiled directly from the header file, rather than a source module that includes the header, it can get confused as to which language needs to be used.

I have a project that was compiling fine when it wasn't using precompiled headers. This project has a common header file (common.h) which includes several STL classes: <vector>, <list>, <algorithm>, etc. The files that use this header (including some Objective-C++ files) were all set up correctly: they were either .cpp or .mm. When I set it up to use precompiled headers, I got the following errors:


error: vector: No such file or directory
error: list: No such file or directory
error: algorithm: No such file or directory
error: map: No such file or directory
error: string: No such file or directory
error: parse error before 'namespace'
warning: data definition has no type or storage class


Why would the error happen only when I turn on precompiled headers?

The problem turned out to be the main project file (main.m). It looks like Xcode looks at your main entry module (main.m or main.mm) in order to find out which language to use to compile your precompiled header.

Renaming main.m to main.mm fixed it.

1 comment:

Anonymous said...

Thanks, I was having an issue including 'std::map' and it was because I indirectly included it from a .m file. You just saved me a headache, cheers.