niels
niels

Reputation: 185

xcode gives syntax error on cpp code

I am trying to reuse Apple's Speak Here sample code in my own iPhone app. I downloaded and compiled the project with no problems, so I then added some of the files to my own application. When I try to compile my own application, the compiler gives me

MeterTable.h:54: error: syntax error before 'MeterTable'

The relevant code is:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

class MeterTable  // line 54
{
public:

It looks kind of like xcode isn't recognizing MeterTable.h and MeterTable.mm as C++ files. I've checked File>>GetInfo for MeterTable.mm, though, and it lists filetype as sourcecode.cpp.cpp.

Why won't this compile?

Upvotes: 0

Views: 2850

Answers (3)

Harsh Mehrotra
Harsh Mehrotra

Reputation: 65

DO the following things and then try:

  1. Go to the Build Setting and search "Compile Sources As"
  2. Select "According to file type"
  3. If you are including CPP file in any of your Objective C class then its implementation class should be .mm, just change its extension to .mm from.m.
  4. Remember, even if you are using CPP code hierarchically, you need to change the extension to .mm.

Upvotes: 3

mfazekas
mfazekas

Reputation: 5709

  1. You're including "MeterTable.h" in a non C++ file other than MasterTable.mm.
  2. The error is not in 'MeterTable.h' but in the header included before it. Note that <stdlib.h>... can be a noop if they are included before.

If you want to make sure your file is compiled with C++, you can add this code to the begining of MasterTable.mm:

#ifdef __cplusplus
#error "compiled as C++"
#else
#error "compiled as C"
#endif 

Upvotes: 1

mouviciel
mouviciel

Reputation: 67919

Try to enclose #include directives with extern "C" { }

Upvotes: 0

Related Questions