Skip to content
New issue

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

将 Obj-C 代码翻译为 C++ 代码 #72

Open
kingcos opened this issue Mar 17, 2019 · 1 comment
Open

将 Obj-C 代码翻译为 C++ 代码 #72

kingcos opened this issue Mar 17, 2019 · 1 comment
Assignees
Labels
[Tips] 即提示

Comments

@kingcos
Copy link
Owner

kingcos commented Mar 17, 2019

Tips - 将 Obj-C 代码翻译为 C++ 代码

Platform Notes
macOS 10.14.2 clang

Solution

➜  ~ 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

clang --help | grep -rewrite-objc

虽然 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

@kingcos kingcos added the [Tips] 即提示 label Mar 17, 2019
@kingcos kingcos self-assigned this Mar 17, 2019
@songgeb
Copy link

songgeb commented Oct 24, 2022

那我的问题是,虽然可以通过clang将OC代码翻译为C++的,很多文章都通过这个来讲解block的实现,翻译后的代码能否等价于真正的block底层实现呢?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Tips] 即提示
Projects
None yet
Development

No branches or pull requests

2 participants