41

I'm using AddThis to add sharing options in my iOS app.

I have imported the classes and added the -fno-objc-arc flag to all the imported classes since they don't use ARC.

However, when I try to run the app I get a slew of Parse Issues such as:

Expected identifier or '('
Unknown type name 'NSString'
Unknown type name 'Protocol'
...

These errors occur in NSObjCRuntime, NSZone, and NSObject. I have the requisite frameworks included as well. Any ideas?

Including this image if it helps: image

soleil
  • 12,133
  • 33
  • 112
  • 183
  • 1
    Are you trying to do this in a c file? – CodaFi Aug 08 '12 at 05:09
  • Not sure what you mean, but there is one .c file in the library, called Base64Transcoder.c – soleil Aug 08 '12 at 05:15
  • Looks like the same issue here: http://stackoverflow.com/questions/7654752/dropbox-sdk-breaks-project-wont-compile-ipad-application, but no solution. Assuming I have the one .c file, how do I work around that? – soleil Aug 08 '12 at 05:19
  • 2
    The problems only arise when you try to link ObjC frameworks to C files (in my experience at least). Does Base64.c import any frameworks? – CodaFi Aug 08 '12 at 05:21

6 Answers6

131

I had the same issue on my project when I was trying to mix C code (.h and .c) with Objective-C code. Found the reason of the issue:

Check your .pch file to make sure every Objective-C framework #import (such as #import <UIKit/UIKit.h>) is enclosed in:

#ifdef __OBJC__

#endif

If they're outside of this conditional scope, the compiler will try to import Objective-C frameworks into C source code.

starball
  • 20,030
  • 7
  • 43
  • 238
horacex
  • 2,101
  • 3
  • 18
  • 17
  • 2
    Thank you so much, just saved me an hours long headache right here! You've got to wonder if the issue is that you're trying to import Objective-C frameworks into c code why don't they say so? Why does XCode generate cryptic error messages that are next to meaningless? Anyway, thanks!!! And yeah, I put all my imports in the .pch file inside the ifdef and it all worked. – n13 May 10 '13 at 05:33
  • you are welcome. that's exactly the value of this community. I got much more help from it :) – horacex Jun 13 '13 at 14:27
  • 4
    THANK YOU! Followers, also make sure to not #define NSStrings or const. NSNumbers outside of the __OBJC__ block! – Raffael Dec 28 '13 at 19:51
  • 1
    This was *exactly* the cause of my problem. Thanks – Tim Kane Jul 27 '14 at 15:21
  • 1
    I don't have a PCH, so what should I do? –  Mar 27 '16 at 05:58
  • Thanks lot @horacex – Jeba Moses May 26 '17 at 04:43
  • Solved my Problem Thx – Hassy May 29 '17 at 07:59
21

I just changed the filename of Base64Transcoder.c to Base64Transcoder.m, and now the project compiles. I have no idea why this fixes the problem, but it works.

soleil
  • 12,133
  • 33
  • 112
  • 183
  • 1
    I am guessing it was. You made your c file into an objective-c file so after that importing objective-c frameworks worked. The fun part is that the .pch file is imported into all c files as well as obj-c files so if there are unprotected obj-c imports in the pch file you end up with this idiotic error message. – n13 May 10 '13 at 05:35
  • Unbelievable. Thanks so much. – PKCLsoft Mar 20 '17 at 13:24
19

I had the same issue, using C and C++ code with objective C, and i doesnt have a .pch The easiest solution was to go into your build settings -> Custom Compiler Flags and set the "Other C Flags" to "-x objective-c" and set the "Other C++ Flags" to "-x objective-c++"

this will do the trick with xCode 7.2

KIDdAe
  • 2,714
  • 2
  • 22
  • 29
Bastien
  • 517
  • 5
  • 9
17

I have had the same problem when my project contained .cpp files.

If .cpp file doesn't contain ObjectiveC frameworks(e.g. ) it has to 'Default-C++ Source' type

enter image description here,

but if .cpp file has ObjectiveC frameworks - it must be as 'Objective-C++ Source'

enter image description here

gaRik
  • 607
  • 6
  • 10
3

TLDR: if your PCH file is OK, look through your CPP file headers to see if you've accidentally included any headers for Objective C objects.

The Details: I got this because I had accidentally included an Objective-C class header in a C++ class header, indirectly. The structure was this:

Compass.h defined a pure Objective C class.

ActionTracker.h defined a C++ class that understood Objective C constructs (via ActionTracker.mm).

HelloWorld.h defined a purely C++ class.

In my original setup, HelloWorld.h included ActionTracker.h, but this was OK as ActionTracker.h didn't yet contain Compass.h. Later, I changed my code and included Compass.h in ActionTracker.h, which then pulled it into HelloWorld.h and I got these errors.

0

I had this same problem when I tried to move the info.plist file from one directory to another. This somehow triggered XCode to edit the build phases for that target and significantly increased the amount of "Compile Sources" and "Copy Bundle Resources".

Luckily my project has multiple targets that I use for testing, (i.e. App Demo, App Dev, App Local, App 1.1, App 1.2 etc)

So I just duplicated one of the unaffected targets and renamed it (also renamed the bundle identifier and the build scheme) and this obviously fixed the problem for me since it's not the whole project that was affected but only that specific target.

If you want to try my solution, try to create a new target from scratch, or duplicate and rename any of your un-affected targets.

Matthys Du Toit
  • 2,168
  • 3
  • 21
  • 34