Cannot include GLM in .mm files

Firstly, a big thanks for this great library that Groovounet has written.

Im trying to write a simple 3d engine for the iPhone and would like to make use of glm internally. I am using C++ but still need to use Cocoa Touch for the initialisation code which is contained in .mm files. I am not using glm from Objective C code.

The problem is that these mm files include certain C++ classes header files which include glm. glm has certain defines which are reserved keywords in the Objective C language, and since therefore cannot compile.

Example

GLView.mm

#include “Renderer.h” //This is a C++ file

Renderer.h

#include <glm/glm.hpp>

GLView.mm won’t compile because it will indirectly include glm and the compiler will give errors on encountering certain Objective C keywords in the glm header.

Is there a way to notify the XCode compiler that a particular include in a .mm file is to be treated as C++ code? Something like

#define COMPILE_AS_CPLUS
#include <glm/glm.hpp>
#undef COMPILE_AS_CPLUS

Hi GamerSg,

Thanks! :slight_smile:

Unfortunately, Objective C++ isn’t supported yet by GLM. However, I am really interested to add this support. Could you send me a build log or even a XCode project so that I can start working on it? I can then provide GLM snapshots to start working with GLM.

Hi Groovounet,

The errors are triggered with an extremely simple scenario. Create an empty xcode project file. Add a .mm (Objective C++) file to your project with a minimal main function and include glm.

From what ive deduced so far, all errors appear to be with enums being named YES and NO. I believe they are reserved keywords in Objective C and are synonymous with true and false in C++.

Here is a log

…/external/include/glm/./core/_detail.hpp:179: error: expected identifier before ‘(’ token
…/external/include/glm/./core/_detail.hpp:179: error: expected }' before '(' token ../external/include/glm/./core/_detail.hpp:179: error: expected ';' before numeric constant ../external/include/glm/./core/_detail.hpp:181: error: expected;’ before ‘}’ token
…/external/include/glm/./core/_detail.hpp:205: error: expected identifier before ‘(’ token
…/external/include/glm/./core/_detail.hpp:205: error: expected }' before '(' token ../external/include/glm/./core/_detail.hpp:205: error: expected ';' before numeric constant ../external/include/glm/./core/_detail.hpp:207: error: expected;’ before ‘}’ token
…/external/include/glm/./core/_detail.hpp:229: error: expected identifier before ‘(’ token
…/external/include/glm/./core/_detail.hpp:229: error: expected }' before '(' token ../external/include/glm/./core/_detail.hpp:229: error: expected ';' before numeric constant ../external/include/glm/./core/_detail.hpp:231: error: expected;’ before ‘}’ token
…/external/include/glm/./core/_detail.hpp:232: error: expected declaration before ‘}’ token

I encountered this often in my own multiplatform projects.
Usually it is because you named a variable or an argument to be “id”. The problem is “id” is a type in objective-C…

see “section 4. Object datatype (id)” of http://www.faqs.org/faqs/computer-lang/Objective-C/faq/

No, the problem is different.

Personally, I managed to use GLM with Objective-C++.
The only thing you have to change is those enums which are at that particular position. I don’t remember exactly, but some renaming solves the problem. If you are interested, I can check that out for you.

Anyway, now I have my running application powered by GLM.

Hi aqnuep, if you could point out the changes or better still provide us with the changed files, it would be very helpful. Im sure groove could make use of your changes for the next version of GLM.

@GamerSg: Here is a snapshot of the “Objective-C support” branch:

GLM with Objective C support snapshot

You can follow the progress and download further updates on this topic by following this link:
http://ogl-math.git.sourceforge.net/git/…ads/objective-c

Feel free to report further issues here.
If the changes required remain that simple, I will apply these changes for the final GLM 0.9.0 release.

Enjoy!

@aqnuep:

That would be great!

I forgot to say that there is a Trac Ticket for this feature if you want:
https://sourceforge.net/apps/trac/ogl-math/ticket/1

Ive downloaded the snapshot and given it a try and it appears to be working now, although im only using a simple skeleton code in my file and only declared a single vec4 in my code. But atleast it compiles fine, for now atleast.

Ill keep you updated if i encounter any issues as i make proper use of the library.

Once again, thanks for the quick fix.

Great, thanks!