Reputation: 63576
I import:
#import <sys/types.h>
#import <sys/sysctl.h>
When I use the function, sysctlbyname("hw.machine", NULL, &size, NULL, 0);
outside dispatch_once
, the compiler finds its definition in . When I use it inside the block inside the second argument to dispatch_once
, I get this error:
No matching function for call to 'sysctlbyname'
Here's the full code:
#import <sys/types.h>
#import <sys/sysctl.h>
NSString *GetMachineName(void) {
static NSString *machine = nil;
if (nil == machine) {
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0); // DEFINED IN SYSCTL.H
static dispatch_once_t once;
dispatch_once(&once, ^ {
// Set 'oldp' parameter to NULL to get the size of the data
// returned so we can allocate appropriate amount of space
sysctlbyname("hw.machine", NULL, &size, NULL, 0); // DEFINED IN SYSCTL.H
// Allocate the space to store name
char *name = (char *)malloc(size);
// Get the platform name
sysctlbyname("hw.machine", name, &size, NULL, 0);
// Place name into a string
machine = [NSString stringWithCString:name encoding:NSASCIIStringEncoding];
// Done with this
free(name);
});
if ([machine isEqualToString:@"iPhone1,1"]) return IPHONE_1G_PLATFORM;
if ([machine isEqualToString:@"iPhone1,2"]) return IPHONE_3G_PLATFORM;
if ([machine isEqualToString:@"iPhone2,1"]) return IPHONE_3GS_PLATFORM;
if ([machine isEqualToString:@"iPhone3,1"]) return IPHONE_4G_PLATFORM;
if ([machine isEqualToString:@"iPod1,1"]) return IPOD_1G_PLATFORM;
if ([machine isEqualToString:@"iPod2,1"]) return IPOD_2G_PLATFORM;
if ([machine isEqualToString:@"iPod3,1"]) return IPOD_3G_PLATFORM;
if ([machine isEqualToString:@"iPad1,1"]) return IPAD_1G_PLATFORM;
if ([machine isEqualToString:@"iPad2,1"]) return IPAD_2G_PLATFORM;
if ([machine isEqualToString:@"i386"] || [machine isEqualToString:@"x86_64"]) return SIMULATOR_PLATFORM;
return NULL;
}
return machine;
}
Upvotes: 3
Views: 1215
Reputation: 63576
To fix, I needed to put the declaration of size
inside the block, because as it was, the argument wasn't decorated with __block
, so it compiles to a const
, mismatching the argument list of sysctlbyname
. Because of function overloading, the compiler found that the function didn't exist.
TLDR: The compiler thought the function I was using was
int sysctlbyname(const char *, void *, const size_t*, void *, size_t);
rather than
int sysctlbyname(const char *, void *, *size_t, void *, size_t);
Upvotes: 1