iOS - expected '=', ',', ';', 'asm' or '__attribute__' before typedef [duplicate] - iphone

I'm trying to port the speakhere example into another app and I'm having issues. I copied all the files, and all the frameworks, but for some reason I get a bunch of compile errors that I've never seen before and thus don't know what to do. The only difference is that i'm not suing IB and so i had to change it slightly.
What does error: expected '=', ',', ';', 'asm' or '__attribute__' before 'foo' mean?... I get this error multiple times for different files
In my situation the first error is pointing at 'MeterTable'.. a class that includes <stdlib.h>,<stdio.h> and <math.h>. But those files seem to be importing fine (if i remove them i get more errors)
Any suggestions on how to debug this?
TIA!
EDIT:
I still can't seem to figure it out. I'm literally just copying files from the example into another project. Can someone check it out please ? SpeakHerePort.zip and the original is here SpeakHere.zip

Your problem is that you are compiling SpeakHerePortAppDelegate.m, which is an Objective C file, but it is indirectly including MeterTable.h which is a C++ header file.
Rename it to SpeakHerePortAppDelegate.mm (double m) so that it is compiled as Objective C++ and your problem is resolved.
Name all your files .mm and then all your code will be compiled as Objective C++

In my case, the .h and .m in question are built fine with regular target, and the App can run as well.
However after the subset of the files are moved under a static library target, it gets this compile error when the static library is built.
Was stuck for a while & tried the above mentioned techniques, unfortunately they didn't help in my case.
Noted that this error happened only for the NSString*, for e.g.,
extern double const kTimeout; // fine
extern NSString* const kImageType; // compile error
After the above analysis & little break, eventually the problem is resolved by adding the the following import to the .h - "Foundation/Foundation.h"

It sounds like an unfinished declaration, probably in a header file. Search for 'foo' (or whatever the symbol actually is) across all project files, using ⇧⌘F (Edit ▸ Find ▸ Find In Project...) in Xcode, and/or examine the headers you're including where MeterTable is declared. Sometimes the compiler gets confused about the actual location of the error, since header files are frequently #imported into other files, so the problem can be manifest in multiple locations.

This might not have applied to this exact situation, but I had this exact error too, which was caused by a bad forward declaration. In Objective-C, make sure your forward-declares begin with the # sign - e.g.
#class MyClass;
Those of us still on autopilot from C++ will forget the #, see that XCode has highlighted class as a reserved keyword, and think all is well with the world. It is not.

It means that you have a syntax error. If you paste the code in question, it's easier to debug.

I had a similar scenario to some of the posts above. I'd written a C++ class based off of the examples in the Audio Queue Services documentation, and had this compilation issue in a test project. This post helped a tremendous amount.
Today, I'm incorporating the C++ class in my project, and got the build error again. In my scenario, I had to also set the type (using the "Get Info" window) to sourcecode.cpp.objcpp for the objective-c class that was calling my C++ class.

Related

C++ forward reference in IOS Project (forward references to 'enum' type)

I'm using a SDK in one of my projects and when adding in the libraries etc, I get an error from this line of code, which is from inside a .h file. I cannot change the .m since its not available to me. Its inside .a (lib)
-(enum scanDeviceID)ID;
And the error message is:
Semantic Issue
ISO C++ forbids forward references to 'enum' types
Are there any compilier/build setting that I can modify to make this work?
Failing that, I know very little C++, is there something on the line of code I can change to make it work?
The line of code says that this method returns an element that's named in the scanDeviceID enum. The problem is that the compiler doesn't see a declaration of that enum. Somewhere in a .h file you should have that declaration. Importing the file should fix it.

Need clarification on what's going on when linking libraries in iOS

This is probably a totally noob question but I have missing links in my mind when thinking about linking libraries in iOS. I usually just add a new library that's been cross compiled and set the build and linker paths without really know what I'm doing. I'm hoping someone can help me fill in some gaps.
Let's take the OpenCV library for instance. I have this totally working btw because of a really well written tutorial( http://niw.at/articles/2009/03/14/using-opencv-on-iphone/en ), but I'm just wanting to know what is exactly going on.
What I'm thinking is happening is that when I build OpenCV for iOS is that your creating object code that gets placed in the .a files. This object code is just the implementation files( .m ) compiled. One reason you would want to do this is to make it hard to see the source code and so that you don't have to compile that source code every time.
The .h files won't be put in the library ( .a ). You include the .h in your source files and these header files communicate with the object code library ( .a ) in some way.
You also have to include the header files for your library in the Build Path and the Library itself in the Linker Path.
So, is the way I view linking libraries correct? If , not can someone correct me on this ?
Basically, you are correct.
Compiling the source code of a library produces one object file for each of the source files (in more than one, if compiled multiply times against different architectures). Then all the object files are archived (or packaged) into one .a file (or .lib on Windows). The code is not yet linked at this stage.
The .h files provide an interface for the functionality exposed by the library. They contain constants, function prototypes, possibly global declarations (e.g. extern int bad_global;), etc. -- basically, everything that is required to compile the code which is using the library.
.h files do not 'communicate' with object code in any way. They simply provide clues for the compiler. Consider this header file:
// library.h
extern int bad_global;
int public_func(int, const void*);
By including this file in your own code, you're simply telling the compiler to copy and paste these declarations into your source file. You could have written declarations for OpenCV library and not use the headers provided with it. In other words, you're asking the compiler to not issue errors about undefined symbols, saying "I have those symbols elsewhere, ok? Here are their declarations, now leave me alone!".
The header files need to be included in the search path in order for compiler to find them. You could simply include them via the full path, e.g. #include "path/to/file.h", or supply an -I option for your compiler, telling him where to look for additional headers, and use #include <file.h> instead.
When your code is compiled, the declarations in header files serve as an indication that symbols your code is using are defined somewhere. Note the difference between the words declaration and definition. Header files contain only declarations most of the time.
Now, when your code is compiled, it must be linked in order to produce the final executable. This is where the actual object code stored in the library comes into play. The linker will look at each symbol, function call, etc. in your object code and then try to find the corresponding definition for each such symbol. If it doesn't find one in the object code of your program, it will look the standard library and any other library you've provided it with.
Thus, it is important to understand that compilation and linkage are two separate stages. You could write any function prototypes at all and use them in your code, it will compile cleanly. However, when it comes to the linking stage, you have to provide implementation for symbols used in your code, or you won't get your executable.
Hope that makes sense!
The .a is the compiled version of the code.
The header files provided with a library are its public interface. They show what classes, methods, properties are available. They do not "communicate" with the binary code.
The compiler needs the headers to know that a symbol (a method name for example) is defined somewhere else. They are associated with the right "piece of code" in the library binary later during the "link" step.

iphone error when unit testing: Expected '=', ',', ';', 'asm' or '__attribute__' before '*' token

I get the following error when I try to build (Build->build) a unit test. My unit test is a logic test (it does not run on the device).
Things that are probably related to my problem are:
I am using libxml2 and a wrapper around it (which I found at cocoawithlove).
This wrapper has some C functions defined (it is not an Objective C class with #interface and #implementation).
The function I am using (and on which the error is occuring) is
NSArray *PerformHTMLXPathQuery(NSData *document, NSString *query);
The strange thing is that all works perfectly well, when I build my application and run it in the simulator. However when I try to run my unit test, I get this error:
/Users/me/XCodeWorkspace/MyProject/XPathQuery.h:15:0 Expected '=', ',', ';', 'asm' or '__attribute__' before '*' token in /Users/me/XCodeWorkspace/MyProject/XPathQuery.h
There are some related questions here, but none of them seems to solve my problem.
In particular:
iphone error: expected '=', ',', ';', 'asm' or '__attribute__' before ' 'foo'
But here some c++ code should be included.
Thank you for help.
In my case, the .h and .m in question are built fine with regular target, and the App can run as well.
However after the subset of the files are moved under a static library target, it gets this compile error when the static library is built.
Was stuck for a while & tried the techniques from another post here, unfortunately they didn't help in my case.
Noted that this error happened only for the NSString*, for e.g.,
extern double const kTimeout; // fine
extern NSString* const kImageType; // compile error
After the above analysis & little break, eventually the problem is resolved by adding the the following import to the .h - "Foundation/Foundation.h"
Hope it helps.
Most likely the compiler is trying to compile your file as C code and not Objective-C code. It's seeing something strange like NSString, which isn't part of the C language, so it's barfing at you.
If you change the file to Objective-C or Objective-C++, it should work. (You can do this be getting info on the file and changing the source type)
After trying to change the filetype of my files (I tried several different things and none of them helped) I decided to rewrite these files as Objective-C files with methods instead of functions.
This solved my problem.

Objective-C error: expected '=', ',', ';', 'asm' or '__attribute__' before 'class'

I'm getting this from an iPhone app I'm working on. Not sure how to interpret the error... It's thrown at a few place in my code. I can't see any pattern of occurrence.
Is this a generic error? What's the meaning of it?
The error you posted indicates that you have a syntax error around your use of class. Manually inspect the first location the error is reported, and you might notice the cause.
To help you debug further, please include the surrounding code so we can better help you.
Most common causes:
Missed # in #class for forward class declaration in headers
Missed ; after the declaration of an enum, a structure, or a typedef
Copied C++ code, where class is used to declare a structure, but code is invalid in Objective-C
I've just solved this exact same problem and I have been tearing my hair out over it.
GCC wasn't highlighting the problem in the header file where the error actually occurred - I had a stray 'B' character at the bottom of a header file (from running command-B to compile). The error was then being thrown in the .m file and other .h files which included the problematic one, often at the #class statement.
If it's throwing an issue with your #class statement, the problem is almost definitely in one of the preceding header files, as you include them directly beforehand - try commenting these out one-by-one and recompiling to find out which. Once you find the culprit file, finding the actual error will be much easier.
Same as Ronan except this time I had a stray character in a source file just before I started importing headers (similarly it was an 's' for Command-S). This particularly threw me since the error message was associated with a library header file that I had never touched and the app was working fine for weeks before that. So if you are getting these errors associated with header files that appear to be fine and that you haven't touched look around for something like this.

iphone error: expected '=', ',', ';', 'asm' or '__attribute__' before ' 'foo'

I'm trying to port the speakhere example into another app and I'm having issues. I copied all the files, and all the frameworks, but for some reason I get a bunch of compile errors that I've never seen before and thus don't know what to do. The only difference is that i'm not suing IB and so i had to change it slightly.
What does error: expected '=', ',', ';', 'asm' or '__attribute__' before 'foo' mean?... I get this error multiple times for different files
In my situation the first error is pointing at 'MeterTable'.. a class that includes <stdlib.h>,<stdio.h> and <math.h>. But those files seem to be importing fine (if i remove them i get more errors)
Any suggestions on how to debug this?
TIA!
EDIT:
I still can't seem to figure it out. I'm literally just copying files from the example into another project. Can someone check it out please ? SpeakHerePort.zip and the original is here SpeakHere.zip
Your problem is that you are compiling SpeakHerePortAppDelegate.m, which is an Objective C file, but it is indirectly including MeterTable.h which is a C++ header file.
Rename it to SpeakHerePortAppDelegate.mm (double m) so that it is compiled as Objective C++ and your problem is resolved.
Name all your files .mm and then all your code will be compiled as Objective C++
In my case, the .h and .m in question are built fine with regular target, and the App can run as well.
However after the subset of the files are moved under a static library target, it gets this compile error when the static library is built.
Was stuck for a while & tried the above mentioned techniques, unfortunately they didn't help in my case.
Noted that this error happened only for the NSString*, for e.g.,
extern double const kTimeout; // fine
extern NSString* const kImageType; // compile error
After the above analysis & little break, eventually the problem is resolved by adding the the following import to the .h - "Foundation/Foundation.h"
It sounds like an unfinished declaration, probably in a header file. Search for 'foo' (or whatever the symbol actually is) across all project files, using ⇧⌘F (Edit ▸ Find ▸ Find In Project...) in Xcode, and/or examine the headers you're including where MeterTable is declared. Sometimes the compiler gets confused about the actual location of the error, since header files are frequently #imported into other files, so the problem can be manifest in multiple locations.
This might not have applied to this exact situation, but I had this exact error too, which was caused by a bad forward declaration. In Objective-C, make sure your forward-declares begin with the # sign - e.g.
#class MyClass;
Those of us still on autopilot from C++ will forget the #, see that XCode has highlighted class as a reserved keyword, and think all is well with the world. It is not.
It means that you have a syntax error. If you paste the code in question, it's easier to debug.
I had a similar scenario to some of the posts above. I'd written a C++ class based off of the examples in the Audio Queue Services documentation, and had this compilation issue in a test project. This post helped a tremendous amount.
Today, I'm incorporating the C++ class in my project, and got the build error again. In my scenario, I had to also set the type (using the "Get Info" window) to sourcecode.cpp.objcpp for the objective-c class that was calling my C++ class.