Using @import from clang?

2019-03-31 19:41发布

问题:

I'd like to write some code with Sublime and clang from the Terminal. How can I use the new module (@import) syntax with clang? I tried adding the -fmodules flag but it didn't work. With modules enabled can I also omit -framework Foundation flag?

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

Little test file:

#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;
}

回答1:

Your input file is Objective-C++ (from the .mm extension) but modules aren't yet ready for C++. There's a seperate flag, -fcxx-modules, but even if you use that you're likely to get failures. To use modules you'll have to stick with C and Objective-C for now.

This should work fine for C and Objective-C with the clang from Xcode 5 and on OS X 10.9.

@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


标签: ios clang