Can I get clang to use custom classes for Objective-C literal objects?

谁都会走 提交于 2019-12-23 15:53:12

问题


The gcc documentation makes reference to an -fconstant-string-class argument, that lets you change the class used for @"Objective-C string literal" instances. I find that this doesn't appear to work with clang, using this source:

#import <stdio.h>
#import <objc/runtime.h>

@interface ConstantString
{
    Class isa;
    char *c_string;
    unsigned int len;
}
@end
@implementation ConstantString

- (Class)class {return isa;}

@end

int main(int argc, char *argv[])
{
    fprintf(stderr, "%s\n", class_getName([@"foo" class]));
    return 0;
}

Compiled with this compiler in this way:

$ clang -v

Apple clang version 4.0 (tags/Apple/clang-421.10.60) (based on LLVM 3.1svn)

Target: x86_64-apple-darwin12.0.0

Thread model: posix

$ clang -fconstant-string-class=ConstantString -lobjc constantstring.m

Undefined symbols for architecture x86_64:

"___CFConstantStringClassReference", referenced from:

  CFString in constantstring-C7icsk.o ld: symbol(s) not found for architecture x86_64 

clang: error: linker command failed with exit code 1 (use -v to see invocation)

If I link against Foundation, then I find it's still using the default constant string class:

$ clang -fconstant-string-class=ConstantString -lobjc constantstring.m -framework Foundation

$ ./a.out

__NSCFConstantString

So can you actually define a custom class for string literals using clang? And can the class of array, dictionary and number literals be changed?

来源:https://stackoverflow.com/questions/11916753/can-i-get-clang-to-use-custom-classes-for-objective-c-literal-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!