Compiling my project on new xcode4 using llvm 2.0 compiler I get a strange error coming from standard <sqlite3.h> header. Problem is with the following line:
//<sqlite3.h>
typedef struct sqlite3 sqlite3;
Error message:
In file included from /Projects/trunk/MyProj/Classes/StatsProvider.m:14:
Elaborated type refers to a non-tag type in /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr/include/sqlite3.h
Using GCC 4.2 project compiles with no problem
How can I fix that error?
My guess: you are compiling sqlite as C++ code, whereas you should compile it as plain C code. class/struct keywords implicitly introduce a typedef in C++, but not so in C.
I interpret the error message as the compiler complaining about using struct sqlite3 when it hasn't seen a struct declaration with that name. Struct names are in a special "tag-space".
My next guess is that the new compiler is stricter than the old one, and has found a bug.
Related
I'm puzzled by a situation I'm observing and would love some insight. First, I'm using Xcode 5, with LLVM 5 compiler options set to defaults.
I have a line in a .m file such as:
static NSArray * const kSchemaVersions = #[#"1"];
And, as expected, I see a compiler error saying Initializer element is not a compile-time constant.
However, if I place this same line in a .mm (Objective C++) file, the compiler does not complain.
I completely understand why it shouldn't work, but I'm baffled as to why it appears to.
Thoughts?
As you mentioned, in C and Objective-C static variables can only be initialised with compile-time constants. In C++ (and therefore Objective-C++) on the other hand, static variables are assigned at run time, before main runs.
For more details have a look at Eli Bendersky's Non-constant global initialization in C and C++
I have the source code for a video decoder which is written in C.
The code was successfully compiled and executed on MAC terminal (which uses GCC compiler). Now I'm trying to create an application on Xcode with the same source code. Only the GUI for the application is written in Objective-C.
When I tried to execute on Xcode (which uses LLVM compiler), I'm getting a lot of errors in the C code.
The only other compiler option that Xcode provides is LLVM GCC 4.2. I compiled using that and found that it is not able to recognize the code written in Objective-C.
Is there any compiler that can be used for both Objective-C and C?
Can GCC 4.2 compiler be used for compiling Objective-C code?
How to tell Xcode to compile using GCC 4.2 compiler?
Kindly help. Thanks in advance!
Both LLVM and GCC can be used to compile C, C++ and Objective-C code.
My guess is that some compiler flags are set incorrectly.
Objective-C is a strict superset of ANSI C, so any compiler that can compile Objective-C code can also compile (ANSI) C code. If your library does not use standard C, then you might run into problems.
The same is true for Objective-C++, which is a strict superset of C++.
If, like in your case, you have a conflicting typedef in your header file, you can use conditional compilation to hide the typedef when compiling Objective-C code:
#ifndef __OBJC__
typedef unsigned char BOOL;
#endif
My problem is a little complex:
Library:
I have a library written in C and C++. It compiles perfecly in XCode with Apple LLVM.
The work of this library itself is done by the C code while the C++ part is just a C++ interface because I prefer C++. In theory I need to use only C++ to comunicate with it.
The application:
I have an application in Objective-C that uses the library above. Of couse, as the communication with my library is done via C++ I need to have a .mm file in order to call my library from "Objective-C++".
During the compilation... several issued of "C" languages happen:
error: non-const static data member must be initialized out of line
error: statement expression not allowed at file scope
etc...
It only happens with Objective-C++
Objective-C++ (app) -> C++ headers (lib) -> C headers (lib, with extern "C") -> NOT OK!!! WHY?
If I make another C++ application for test of library, it goes ok
C++ (app) -> C++ (lib) -> C header (lib, with extern "C") -> OK
Detail: I am always using the Apple LLVM compiler here
The question:
How do I compile C code from C++ from Objective-C++ code? Why it's different than compile from a regular C++ code?
Don't forget to do
#ifdef __cplusplus
extern "C" {
#endif
<Some C method declaration>
#ifdef __cplusplus
}
#endif
in your header files that contain C method declarations.
After a long period of tries and Google I finally could figure out.
Before I call my C headers, I should undefine MAX and MIN macros. For some reason they were previous defined. The compiler doesn't show the error with precision. That's why it was difficult. The C framework I am using has it's own declaration of MAX and MIN macros...
Another very interesting thing that might be useful for others is that before we need to do the same thing: undefine max and min (lower case) if we are using some C framework that implements it's own max and min.
It solved both problems.
I'm running in to some strange errors when dabbling in some socket programming using Xcode 4. I get the error that addrinfo is undeclared, despite me simply copying the code from another project that did work (when using Xcode 3). The project is mainly in Objective-C, but I've tested creating another framework with plain C, and the error still remains.
I have the following frameworks included:
ApplicationServices.framework
Cocoa.framework
AppKit.framework
Foundation.framework
No added linker flags either.
However, other functions such as getaddrinfo (that uses addrinfo itself!) exists. Any ideas?
This issue wasn't IDE-related, it was a language issue. How structs are treated is apparently different in C (and thus Objective-c) and C++ (which the previous projects were=. So I changed the line
addrinfo hints;
To:
struct addrinfo hints;
Have you got the correct imports?
#import <netinet/in.h>
#import <sys/socket.h>
A quick grep shows that struct addrinfo is declared in <netdb.h>. Try explicitly including that. (Your Xcode 3 project may have included that, or some other header that includes it, in its prefix file.)
g++ compiler complains about conversions between related types (from int to enum, from void* to class*, from const char* to unsigned char*, etc.). Compiler handles such convertions as errors and won't compile furthermore. It occurs only when I compile using Dev-C++ IDE, but when I compile the same code (using the compiler which Dev-C++ uses) such errors (even warnings) do not appears.
How to mute errors of such types?
I suspect that in one case you are compiling your code as C and the other as C++. In C++, there is no implicit conversion from void * to any other type of pointer, and a C++ compiler which did not diagnose this as an error would be broken. You need to supply more details of how you are compiling your code.
Also, DevC++ is a pretty awful piece of code. It's buggy and no longer being actively developed, as well as being butt-ugly. You should seriously consider switching to a more modern and capable IDE, such as Code::Blocks.
All of your implicit conversions are disallowed in standards conforming C++. G++ is simply enforcing these rules.