error: declaration of instance variables in the interface is deprecated [-Werror,-Wobjc-interface-ivars] - objective-c

I just cloned a project from github and I went to build it and I ran into this error over and over again. Any idea what the problem is?
Here is some of my error output and some of the code that causes the error:
NOTE:
_indexSetFlags and _internal are the variables that are causing the error in this code example
#import <Foundation/NSObject.h>
#import <Foundation/NSRange.h>
NS_ASSUME_NONNULL_BEGIN
#interface NSIndexSet : NSObject <NSCopying, NSMutableCopying, NSSecureCoding> {
#protected // all instance variables are private
struct {
NSUInteger _isEmpty:1;
NSUInteger _hasSingleRange:1;
NSUInteger _cacheValid:1;
NSUInteger _reservedArrayBinderController:29;
} _indexSetFlags;
union {
struct {
NSRange _range;
} _singleRange;
struct {
void * _data;
void *_reserved;
} _multipleRanges;
} _internal;
}
ERROR OUTPUT:
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSAutoreleasePool.h:14:11:
error: declaration of instance variables in the
interface is deprecated [-Werror,-Wobjc-interface-ivars]
void *_reserved2;
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSString.h:543:9:
error: declaration of instance variables in the interface is
deprecated [-Werror,-Wobjc-interface-ivars]
int numBytes;
^ /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSString.h:545:9:
error: declaration of instance variables in the interface is
deprecated [-Werror,-Wobjc-interface-ivars]
int _unused;
^
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSNotification.h:39:11:
error: declaration of instance variables in the
interface is deprecated [-Werror,-Wobjc-interface-ivars]
void *_impl;
^
fatal error: too many errors emitted, stopping now [-ferror-limit=] 20
errors generated.
The following build commands failed: CompileC
/Users/bananaman123/Desktop/eecs481/hw6/wz_build/lib/sdl/warzone2100.build/Debug/sdl-backend.build/Objects-normal/x86_64/cocoa_wz_menus.o lib/sdl/cocoa_wz_menus.mm normal x86_64 objective-c++
com.apple.compilers.llvm.clang.1_0.compiler (1 failure)
I left out a lot of the error output, but all of the error messages contain "error: declaration of instance variables in the interface is deprecated [-Werror,-Wobjc-interface-ivars]"

As the repository README.md suggests at the very last line, go and read
https://github.com/Warzone2100/warzone2100/blob/master/macosx/README.md
then go step by step and fulfil the requirements.
It's mainly written in C++ and so it needs according treatment.
You need CMake 3.15+,
find it here https://cmake.org/download/#latest
also Gettext via Homebrew
and Asciidoctor via Homebrew as well.
As you obviously use OSX 10.15+ you don't need GCC, Xcode > 11 is fine.
Your errors go back to a bogus project setup.
NSIndexSet is a class that belongs to Foundation Framework. Usually nothing to repair for you in this code. Quite sure this comes up because Objective-C++ is assumed to work which is a simple way to include C++ into your Xcode project. Read more about working with .mm files if you want to change the way it is added to your project because..
Somewhere between the lines in the Readme.md is also written..
"Run CMake to generate the Xcode project"

Related

What does “typedef void (^Something)()” mean [duplicate]

This question already has answers here:
Caret in objective C
(3 answers)
Closed 4 years ago.
I was trying to compile stk. During the configuration I get the error
System/Library/Frameworks/CoreAudio.framework/Headers/AudioHardware.h:162:2:
error: expected identifier or '(' before '^' token
(^AudioObjectPropertyListenerBlock)(
When I see the code I see ^ inside the function pointer declaration at line 162 in here. I know we can have * but what does ^ mean?
Code snippet :
#if defined(__BLOCKS__)
typedef void
(^AudioObjectPropertyListenerBlock)( UInt32 inNumberAddresses,
const AudioObjectPropertyAddress inAddresses[]);
As other answerers here say, it could be in C++/CLI.
But also, if you are on a macOS (like you hinted in one comment), this is an Objective-C block.
Its syntax is very very weird.
The block is like a C++ closures and Java anonymous inner classes, it can capture variables.
__block int insider = 0;
void(^block)() = ^{
// print insider here using your favourite method, printf for example
};
This is a complete NSObject (base Objective-C class), but is callable, this is not a mere function pointer.
Refer to this Apple document: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html
Now, we go to the important question, I want to run this on Linux, how ???
LLVM supports block syntax, but you should refer to this StackOverflow question for more: Clang block in Linux?
So, you should compile your code in the LLVM compiler, and use -fblocks and -lBlocksRuntime.
Don't forget to install those Linux packages:
llvm
clang
libblocksruntime-dev
If you are already on macOS, just use -fblocks.

NS_DESIGNATED_INITIALIZER expected : (colon)

I am trying to declare a designated initializer like this:
- (instancetype)initWithDelegate:(id <MyDelegate>)delegate NS_DESIGNATED_INITIALIZER;
But it is showing me this compilation error:
Expected ':'
Interestingly when I try to write it like this (reference link: Adopting Modern Objective-C) -
- (instancetype)init NS_DESIGNATED_INITIALIZER;
It shows this error:
Expected ';' after method prototype.
Any ideas on how to properly use NS_DESIGNATED_INITIALIZER?
NS_DESIGNATED_INITIALIZER macro is not defined in the library headers for Xcode 5 - you need Xcode 6 to use it. Note your link says "Pre-release".
The macro is defined in the following way (quoting NSObjCRuntime.h)
#ifndef NS_DESIGNATED_INITIALIZER
#if __has_attribute(objc_designated_initializer)
#define NS_DESIGNATED_INITIALIZER __attribute__((objc_designated_initializer))
#else
#define NS_DESIGNATED_INITIALIZER
#endif
#endif
Note you can still use
- (instancetype)initWithDelegate:(id <MyDelegate>)delegate __attribute__((objc_designated_initializer));
in Xcode 5 or you can add that macro explicitly to your precompiled header.
Bear in mind you can only add this to interface or class extension declarations otherwise you'll get this error:
'objc_designated_initializer' attribute only applies to init methods of interface or class extension declarations

Linker error for global variable [duplicate]

This question already has answers here:
Accesing global variable giving linker error in objective C
(2 answers)
Closed 9 years ago.
I'm making a small, simple application, so I decided to use global variables over Singletons. I'm also only using one.
My app pulls an int from a small preference file, and that is set to the global variable as an NSInteger. The global variable may be changed while the app is running.
AppController.h
#import <Cocoa/Cocoa.h>
extern NSInteger preferenceNumber;
#interface ....
App Controller.m
-(void)someMethod {
...
//fileContents is a string containing the int that is inside the file
preferenceNumber = [fileContents intValue]
...
}
The Linker Errors (2):
Undefined symbols for architecture x86_64:
"_preferenceNumber", referenced from:
-[AppController someMethod1] in AppController.o
-[AppController someMethod2:] in AppController.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The bolded parts are the two errors.
What is causing this? What is the simplest, best way to solve it?
Just add one line in your implementation class:-
AppContollr.m
#implementation AppContoller
NSInteger preferenceNumber;

I am having all the time this issue after running my app

I don't know why, but after a while working without problems I added some buttons, then I launched my app and this error appeared:
ld: duplicate symbol _x in
/Users/alexbarco/Library/Developer/Xcode/DerivedData/RecolectaDatos-ayjpqqcajbhuzvbkvernzsyunpbe/Build/Intermediates/RecolectaDatos.build/Debug-iphonesimulator/RecolectaDatos.build/Objects-normal/i386/SeconViewController.o
and
/Users/alexbarco/Library/Developer/Xcode/DerivedData/RecolectaDatos-ayjpqqcajbhuzvbkvernzsyunpbe/Build/Intermediates/RecolectaDatos.build/Debug-iphonesimulator/RecolectaDatos.build/Objects-normal/i386/ViewController.o
for architecture i386 clang: error: linker command failed with exit
code 1 (use -v to see invocation)
Whenever I have duplicate symbol errors, it is almost always because I have a circular #import in my headers. The solution is quite simple, use forward declarations where possible, and #import .h files from .m files instead.
There are just two cases where you need to #import one .h from another:
if you are extending the class in the #import
you are implementing a protocol in the #import
Specifically, you do not need to import files just to use a class name or protocol in your signatures; instead use forward declarations.
For example, this (in Bar.h):
#import "Foo.h"
might become this (Bar.h):
#class Foo;
#protocol FooDelegate;
and bar.m:
#import "Foo.h"
Here is a link to the documentation on forward declarations.
The "duplicate symbol" message means that you're declaring some name (in this case, _x) twice in the same scope. Say you had code like this:
int _x = 1;
int _x = 2;
You'd expect to get an error then, right? You can use the same name for two things at the same time.
The error you're getting is essentially the same. You're declaring _x somewhere, and from the compiler's point of view you're doing it twice. There are a few ways to deal with this, depending on what _x represents.
chrahey's answer explains about forward class declarations. I won't cover that again here except to say that a forward declaration helps you resolve circular references, where the definition of class A depends on class B and vice versa.
If _x is a variable, it's likely that you're trying to declare it in a header file. The compiler basically copies the contents of each header file that you import into the source file, so if you declare a variable in a header file and then import that header into two or more implementation files, you'll end up with multiple declarations of that variable. To get around that, use the extern keyword to tell the compiler "this name will be declared somewhere else" and then put the real declaration in an implementation file:
Foo.h:
extern int _x;
Foo.m
int _x;
Pretty much the same thing goes for functions. It doesn't appear that _x is a function, but if it were, and if you were silly enough to put the function definition in a header file, then you'd again get an error if that file were imported into more than one implementation file. This is why header files contain prototypes, not definitions:
Foo.h:
int foo(int a);
Foo.m
int foo(int a)
{
return a + 10;
}

How do I print the result of an objective-c class method in gdb?

When using gdb (via the debug console) to debug an iPad program in Xcode 4, I'm trying to print out the result of running a class method:
(gdb) po [MyClass foo:#"bar"]
gdb outputs the following:
No symbol "MyClass" in current context.
Is there some way to print the result of +(NSString *)foo:(NSString *)string using gdb in Xcode 4?
I had the same problem here. The solution in my case was to use NSClassFromString like this:
po [NSClassFromString(#"MyClass") foo:#"bar"]
The problem is you have not declared anything of type MyClass in your targets source. If your MyClass is only designed to have static methods you can try something like
#if DEBUG //gdb Static Method Fix
MyClass *mc = nil; //This makes the symbol available
[mc class]; //supress unused warning
#endif
My guess is that by not declaring a type of the class anywhere in your code it has been optimized out of the lookup symbols. From my testing that call above does not even have to be called for it to work. If you look at printcmd.c of gdb line # 1250 this is where the error is printed from and this occurs after a call to lookup_minimal_symbol. And although gdb is unable to find the symbol in context it is still fine to only use static methods of MyClass in your source code without the fix above.