我想用Terminal的Sublime和clang编写一些代码。如何在clang中使用新的模块(@import)语法?我尝试添加-fmodules标志,但是没有用。启用模块后,是否还可以省略-framework Foundation标志?

   clang -fmodules -framework Foundation test.mm; ./a.out

小测试文件:
#import <stdio.h>
// #import <Foundation/Foundation.h>
@import Foundation;

/*

clang -fmodules -framework Foundation test.mm; ./a.out

*/
int main(int argc, char const *argv[])
{
    NSString *hello = @"Hello";
    printf("%s\n", "hello world");
    return 0;
}

最佳答案

您的输入文件是Objective-C++(来自.mm扩展名),但是模块尚未准备好用于C++。有一个单独的标志-fcxx-modules,但是即使使用它,也很可能会失败。要使用模块,您现在必须坚持使用C和Objective-C。

这对于使用Xcode 5和OS X 10.9的c语言的C和Objective-C应该可以正常工作。

@import Foundation;

int main() {
  NSString *hello = @"Hello";
  NSLog(@"%@", hello);
}

⑆ clang -v
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix

⑆ clang -fmodules main.m && ./a.out
2013-11-20 08:51:37.638 a.out[51425:507] Hello

09-11 06:08
查看更多