Study harmoniously! Don’t be impatient!! I’m your old friend, Xiao Qinglong

preface

Today I share a solution to a compilation error in the official ObjC source project.

I’m ashamed to say that many of my previous articles have been using the source code library prepared by Mr. Cooci. As a programmer who loves learning (pooh, smelly shame), I should learn to drive myself (to the kindergarten) and solve problems by myself.

  • The compiled source code is available for download at the end of the article.

[Environment Description]

  • Mac system:MacOS Big Sur 11.5.2
  • Xcode version:13.2.1

The official objc source download: opensource.apple.com/tarballs/ob…

I downloaded version 818.2

Download it and unzip it

Open the objC4-818.2 project and we find several missing files:

Let’s ignore them and just run them.

Unable to find SDK ‘macosx.internal’ 1, Unable to find SDK ‘macosx.internal’

Build Settings-> Search the Base SDK

The Targets to modify

Project changes

If this error is still reported, remember to clean before running.

2, ‘sys/reason.h’ file not found

Run discovery fails again because the file reason.h is missing.

[Solution]

Open the web site: opensource.apple.com/source/xnu/

Copy the latest version name (xnu-7195.81.3 in my case)

Download address: opensource.apple.com/tarballs/xn… (You can refer to the format to modify the version number)

After downloading, you’ll get a file: xnu-7195.85.3.tar.

Xnu-7195.81.3 -> BSD -> sys -> reason.h

Create the file structure lostFiles -> sys -> reason.h in the project root directory

Targets -> Build Setting -> Header Search Paths-> Add $(SRCROOT)/lostFiles

3, ‘mach-o/ dyLD_priv. h’ file not found

The operation continues with an error

Solution 】 【 opened: opensource.apple.com/tarballs/dy…

Unzip to find the file dyLD_priv.h

Create a folder under lostFiles Mach -o and drag dyLD_priv. h to the folder Mach -o

2, dyLD_priv. h

Just comment this line of code.

5, ‘OS /lock_private.h’ file not found

Open the web site: opensource.apple.com/tarballs/li…

Decompression find file: libplatform – 177.270.1 / private/OS/lock_private. H

The root directory file is added

6, ‘OS /base_private.h’ file not found

Find files: libplatform – 177.270.1 / private/OS/base_private. H

The root directory file is added

7, ‘pthread/tsd_private.h’ file not found

Open the web site: opensource.apple.com/tarballs/li…

Find the file: libpthread-416.100.3/private/tsd_private.h

The root directory file is added

8, ‘System/machine/ CPU_capabilities. H ‘file not found

Open the web site (it has already been download) : opensource.apple.com/tarballs/xn…

Find files: xnu 7195.81.3 / osfmk/machine/cpu_capabilities. H

The root directory file is added

9, ‘OS /tsd.h’ file not found

Find files: xnu – 7195.81.3 / libsyscall/OS/TSD. H

The root directory file is added

10, ‘pthread/spinlock_private.h’ file not found

(The libpthread-416.100.3 library was downloaded earlier.)

Find files: libpthreads – 416.100.3 / private/spinlock_private. H

The root directory file is added

11, ‘System/pthread_machdep.h’ file not found

Open the web site: opensource.apple.com/tarballs/Li…

Find files: the Libc – 825.40.1 / pthreads/pthread_machdep. H

The root directory file is added

12, ‘crashreporterclient. h’ file not found

Find files: the Libc – 825.40.1 / include/CrashReporterClient. H

The root directory file is added

Typedef redefinition with different types (‘int’ vs ‘volatile OSSpinLock’ (aka ‘volatile int’)

The literal redefines pthread_lock_t with different types

Global search pthread_lock_t

We find that pthread_lock_t is defined in both spinlock_private.h and pthread_machdep.h.

The pthread_lock_t annotation in pthread_machdep.h defines this line of code.

14. Multiple declaration errors

// Error message is as follows

Static declaration of '_pthread_has_direct_tsd' follows non-static declaration

Static declaration of '_pthread_getspecific_direct' follows non-static declaration

Static declaration of '_pthread_setspecific_direct' follows non-static declaration
Copy the code

Comment out all three functions and their implementation

15, Use of undeclared identifier ‘OS_UNFAIR_LOCK_ADAPTIVE_SPIN’

[Solution]

// uint32_t opts = OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION | OS_UNFAIR_LOCK_ADAPTIVE_SPIN;

/ / changed to
uint32_t opts = OS_UNFAIR_LOCK_DATA_SYNCHRONIZATION;
os_unfair_lock_lock(&mLock);
Copy the code

16, ‘objc-shared-cache.h’ file not found

Find the file: dyld-852.2/include/objc-shared-cache.h

The root directory file is added

17, ‘objc-probes. H ‘file not found

18, ‘OS /feature_private.h’ file not found

Comment out the #include < OS /feature_private.h> line

Ld_fall_2020_os_versions’, ‘objC4’, ‘preoptimizedCaches’ without declaration

// An error message was reported

Use of undeclared identifier 'dyld_fall_2020_os_versions'

Use of undeclared identifier 'objc4'

Use of undeclared identifier 'preoptimizedCaches'
Copy the code

[Solution]

20, ‘objc-BP-assist. h’ file not found

Comment this line of reference code

21, Use of undeclared identifier ‘dyld_platform_version_macOS_10_13’

[Solution]

22, Use of undeclared identifier ‘dyld_platform_version_macOS_10_11’

[Solution]

29, Use of undeclared identifier ‘dyLD_FALL_2018_OS_VERSIONS’

[Solution]

24, ‘_simple.h’ file not found

Find the file: libplatform-177.270.1/private/_simple.h

The root directory file is added

25, ‘block_private. h’ file not found

Open the web site: opensource.apple.com/tarballs/li…

Unzip and find the file: libclosure-79/ block_private.h

The root directory file is added

26, ‘OS /linker_set.h’ file not found

Find the file: libC-1439.40.11 / OS /linker_set.h

The root directory file is added

27, Use of undeclared identifier ‘CRGetCrashLogMessage’

[Solution]

Build Settings -> Search for Preprocessor Macros and add LIBC_NO_LIBCRASHREPORTERCLIENT

After adding and compiling, I will not report this error

28, ‘Cambria/ traps. h’ file not found

Cambria/ cambria. h’ file not found

29, ‘kern/restartable.h’ file not found

Find files: xnu 6153.81.5 / osfmk/kern/restartable. H

The root directory file is added

30, Use of undeclared identifier ‘oah_is_current_process_translated’

Before the change

The modified

31, ‘OS /feature_private.h’ file not found

Comment this line of code

Reason_private. h’ file not found | ‘OS /variant_private.h’ file not found

// An error message was reported

'os/reason_private.h' file not found

'os/variant_private.h' file not found
Copy the code

[Solution]

33, Use of undeclared identifier ‘dyLD_platform_version_xx_x_x’

// An error message was reported

Use of undeclared identifier 'dyld_platform_version_bridgeOS_2_0'

Use of undeclared identifier 'dyld_platform_version_iOS_10_0'

Use of undeclared identifier 'dyld_platform_version_macOS_10_12'

Use of undeclared identifier 'dyld_platform_version_tvOS_10_0'

Use of undeclared identifier 'dyld_platform_version_watchOS_3_0'
Copy the code

[Solution] Comment the selected code

34, Can ‘t open the order file: / Applications/Xcode. The app/Contents… OrderFiles/libobjc.order

// An error message was reported
Can't open order file: / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX12.1 SDK/AppleInternal/OrderFi les/libobjc.orderCopy the code

Build Settings-> search orderFile -> add $(SRCROOT)/ libobjC.order

35、library not found for -lCrashReporterClient

Build Settings-> Search other Link -> Delete -lCrashReporterClient

36、library not found for -loah

As with 35, Build Settings-> search other Link -> Delete -loah

37, ‘_static_assert’ declared as an array with a negative size

[Solution]

38, SDK “Macosx. internal” cannot be located.

TARGETS->Build Phases->Run Script(markGC)-> modify macosx.internal to Macosx

39, Expected function body after function declarator

Declarator = function body after function Declarator = function declarator

Finally compiled

After a series of fixes, Build Successded(😭)

Create a new debug

[Click the + sign]

Select macOS – Command Line Tool

[Enter name]

【 Relational dependency 】

[run ing]

The error is in this line

lock.lock();
Copy the code

The error message is

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
Copy the code

The error message doesn’t give us any useful information, but we know that something must be wrong with the Lock method.

Os_unfair_lock_lock (&mLock); This line of code

[Run again]

Description NSLog logs are successfully printed.

Reference article links

Lock method error resolved

I compared objC-os. h file in the Demo with objC-os. h file in my own project, and found that the lock method was different. Finally, I realized that there was an extra line of code.

Objc4 source code compiling and debugging www.jianshu.com/p/bffba0169…

Compilation error resolution

Reference article:

Build the iOS – objc4-781 source configuration process www.jianshu.com/p/4f15c551a…

Build objc source www.jianshu.com/p/5fc26fed9…

Since it was compiled on Xcode version 13.2.1, I chose the latest one as much as possible to configure some missing library files.

Attached is a lostFiles folder structure diagram

Compiled source code

Link: pan.baidu.com/s/1xt2YuoZm… Extraction code: OV85 — from Baidu network disk super member V3 share