We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
➜ ~ clang --version Apple LLVM version 10.0.0 (clang-1000.11.45.5) Target: x86_64-apple-darwin18.2.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Xcode 中,C/C++/Obj-C/C++ 的编译器是 clang。其加上 -rewrite-objc 参数可以将 Obj-C 代码翻译为 C++ 代码,即 clang -rewrite-objc main.m -o main.cpp。也可以结合 xcrun 设置编译所基于的 SDK,以及通过 -arch 指定编译架构参数,通过 -framework 指定需要依赖的框架,即 xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m -o main-arm64.cpp -framework UIKit。
-rewrite-objc
clang -rewrite-objc main.m -o main.cpp
xcrun
-arch
-framework
xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m -o main-arm64.cpp -framework UIKit
虽然 clang 可以将 Obj-C 代码翻译为 C++,但其实大部分代码都属于 C 语言,比如 Obj-C 的类,其本质是 C 语言中的结构体。而翻译存在的目的是因为 Windows 上的 Visual Studio 中并没有 Obj-C 的编译器,所以 clang 将 Obj-C 转为 C/C++,便于其他 C/C++ 编译器进行编译。在使用 Xcode 构建 Obj-C 项目时并不会将 Obj-C 进行翻译,clang 可以直接编译纯 Obj-C 代码。
The text was updated successfully, but these errors were encountered:
那我的问题是,虽然可以通过clang将OC代码翻译为C++的,很多文章都通过这个来讲解block的实现,翻译后的代码能否等价于真正的block底层实现呢?
Sorry, something went wrong.
kingcos
No branches or pull requests
Tips - 将 Obj-C 代码翻译为 C++ 代码
Solution
Xcode 中,C/C++/Obj-C/C++ 的编译器是 clang。其加上
-rewrite-objc
参数可以将 Obj-C 代码翻译为 C++ 代码,即clang -rewrite-objc main.m -o main.cpp
。也可以结合xcrun
设置编译所基于的 SDK,以及通过-arch
指定编译架构参数,通过-framework
指定需要依赖的框架,即xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m -o main-arm64.cpp -framework UIKit
。虽然 clang 可以将 Obj-C 代码翻译为 C++,但其实大部分代码都属于 C 语言,比如 Obj-C 的类,其本质是 C 语言中的结构体。而翻译存在的目的是因为 Windows 上的 Visual Studio 中并没有 Obj-C 的编译器,所以 clang 将 Obj-C 转为 C/C++,便于其他 C/C++ 编译器进行编译。在使用 Xcode 构建 Obj-C 项目时并不会将 Obj-C 进行翻译,clang 可以直接编译纯 Obj-C 代码。
Reference
The text was updated successfully, but these errors were encountered: