What does “typedef void (^Something)()” mean [duplicate] - c++-cli

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.

Related

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

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"

How can I get sizeof class based on NSObject [duplicate]

This question already has answers here:
Checking the size of an object in Objective-C
(6 answers)
Closed 8 years ago.
I'm new to obj-c . During these day's practices I noticed that every class based on NSObject can't have an entity like : NSObject en; in c++ but NSObject* en instead.
But, sometimes I need to know the Size of an Object.I can't simply write sizeof(en) because en is a pointer var.I can't simply use sizeof(NSObject) neither for the compiler telling me Application of sizeof to interface 'XXXX' is not supported on this architecture and platform.
I want to know if there is a way to get sizeof(NSObject) .If not,what the syntax is designed this for & any other ways to get the size.
From doc
class_getInstanceSize
Returns the size of instances of a class.
size_t class_getInstanceSize(Class cls)
Parameters cls A class object.
Return Value The size in bytes of instances of the class cls, or 0 if
cls is Nil.
But I doubt this is what you really want. Because I never found it useful and can't think a case it may be useful. (other than learning memory layout of objects and low level implementation details)
First, you should import malloc.h
If you use Non-ARC:
malloc_size(myObject);
if you are using ARC:
malloc_size((__bridge const void *) myObject));
This linker is a question similar to yours.

what does the caret sign mean in Objective-C? [duplicate]

This question already has answers here:
Caret in objective C
(3 answers)
Closed 9 years ago.
There is a piece of code like
typedef void (^SignIn) (NSString *email, NSString *password);
What does the ^ mean before SignIn? Is this Objective-C specific usage?
It's the syntax for blocks.
That typedef declares SignIn to mean a block which takes two NSString* arguments and returns void (i.e. nothing).
It is a block.
For a guide to understanding blocks, see this tutorial
Unless, you already know what a block is, and you just didn't know what the caret was for.

Objective C compiler not complaining about syntax error [duplicate]

This question already has answers here:
Semicolon after the method name in Objective-C implementation file
(6 answers)
Closed 9 years ago.
I'm trying to figure out why gcc is OK with the following:
- (void) methodname: (id) sender;
{
// do stuff
}
Notice what's wrong here: There is a semicolon that should not be there.
I'm also finding that execution of the method is having bizarre results.
Anybody know what effect the semicolon is supposed to have or not have here?
Thanks.
The semi-colon is optional in the context of the #implementation. Some teams standardize on requiring it, some don't.
In general ; makes it a function prototype. But the compiler must be smart enough to ignore it in this case. I don't think it's the ; that's causing your odd issues but I'm not sure.

Redefinition of 'struct __block_literal_1' in Obj-C

I've been working around this problem for a while now and it's really driving me insane. Whenever I declare a block, I get a compile error in Xcode 4.1 compiling with LLVM GCC 4.2:
Redefinition of 'struct __block_literal_1'
Even examples of blocks copy-pasted from the documentation throw the error:
int multiplier = 7;
int (^myBlock)(int) = ^(int num) {
return num * multiplier;
};
I've had several attempts to find a solution to this error to no avail. Any help would be appreciated.
Update:
I know it's been a while since I posted, but I will need to tackle this problem sooner or later. I think this has something to do with using static libraries. When a static library with blocks are added to a project, the error is thrown. Will post more details soon.