Compiling and Debugging the Objective‑C Runtime (objc4) on macOS
This guide explains what the Objective‑C runtime is, where to obtain its source code, the required build environment, step‑by‑step compilation instructions, and detailed solutions for the numerous build errors encountered when compiling objc4 on macOS.
What is Runtime? In C, function calls are resolved at compile time, while Objective‑C uses dynamic message sending, meaning the actual function is chosen at runtime.
How to obtain the Runtime source code – Download the latest objc4 source from the Apple open‑source site (e.g., https://opensource.apple.com/release/macos-10141.html) and locate the objc directory.
Build environment
Xcode version: 11.2.1
macOS version: 10.14.6
Runtime version: objc4-750.1
The article records the errors encountered during compilation and provides corresponding fixes; if you do not see a particular error, you can skip that section.
Compilation process
Open objc.xcodeproj and start building. The first error requires changing the Architecture setting to “Standard Architectures”.
Subsequent errors involve missing header files. The general solution is to download the required libraries (Libc, dyld, libauto, libclosure, libdispatch, xnu, libpthread, launchd, libplatform) from the Apple open‑source page, extract them into a common directory, add that directory to the Header Search Paths, and copy missing headers into the project.
Specific error fixes include:
‘reason.h’ not found – locate it with find . -name 'reason.h' and copy it to a Common/sys folder.
‘dyld_priv.h’ not found – comment out the unexpected comma in the header.
Various other missing headers (e.g., lock_private.h , base_private.h , tsd_private.h , cpu_capabilities.h , tsd.h , spinlock_private.h , pthread_machdep.h , CrashReporterClient.h , Block_private.h , objc‑shared‑cache.h , _simple.h , isa.h ) – each is resolved by locating the file in the downloaded sources and adding it to the project.
‘Use of undeclared identifier CRGetCrashLogMessage’ – add LIBC_NO_LIBCRASHREPORTERCLIENT to Preprocessor Macros.
‘Use of undeclared identifier DYLD_MACOSX_VERSION_10_13’ – add the following definitions: #define DYLD_MACOSX_VERSION_10_11 0x000A0B00 #define DYLD_MACOSX_VERSION_10_12 0x000A0C00 #define DYLD_MACOSX_VERSION_10_13 0x000A0D00 #define DYLD_MACOSX_VERSION_10_14 0x000A0E00
‘can't open order file: /Applications/Xcode.app/…/libobjc.order’ – set the Order File path to $(SRCROOT)/libobjc.order in the target’s Build Settings.
‘library not found for -lCrashReporterClient’ – remove the reference to -lCrashReporterClient .
‘Undefined symbol: _objc_opt_class’ – adjust the Run Script phase (replace macosx.internal with macosx ).
‘no such public header file: /tmp/objc.dst/usr/include/objc/ObjectiveC.apinotes’ – disable “Supports Text‑Based Install API” and set “Text‑Based InstallAPI Verification Model” to “Errors Only”.
After applying all fixes, the project builds successfully. Create a new target named runtimeTest and run it; you can set breakpoints in main.m to inspect the runtime source.
References
https://blog.csdn.net/weixin_34410662/article/details/91395850
https://www.jianshu.com/p/73b37cc5937c
https://blog.csdn.net/yahibo/article/details/94384050
https://juejin.im/post/5d9c829df265da5ba46f49c9
https://github.com/acBool/RuntimeSourceCode
JD Retail Technology
Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.