Static library

Obtaining third-party static library methods (reference)

Take getting AFNetworking as an example:

1. Install AFNetworking using cocoaPods and add it to target in your Podfile

#use_frameworks!
pod 'AFNetworking'
Copy the code

Note # use_frameworks! Do not omit the # sign, otherwise you are using a dynamic library

2. Open the terminal after the installation command is successfully executedxcworkspaceFile, New Scheme, selectedPodsThe followingAFNetworking

Compile and find the libAFNetworking. A file that has turned black. Right-click show in Finder

4. Copy the AFNetworking file from the AFNetworking file in the Pods directory for later use

5. Copy the libAFNetworking. A file you just found to the new AFNetworking folder

Linked static library

1. Prepare a main.m file and add the following code:

#import <Foundation/Foundation.h>
#import <AFNetworking.h>

int main(a) {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSLog(@"test---%@",manager);
    return 0;
}
Copy the code

2. Put the AFNetworking folder and main.m in the same directory, for example:

3. Run the clang command to compile the main.m file into the main.o file

-x: specifies the language type of the compiled file. -g: generates debugging information. -C: generates the target file, running preprocess, compile, assemble without linking. -l <dir> Specify the library file path (.a\.dylib library file) library search path 3. -l<library_name> Specify the linked library file name (.a\.dylib library file) other link flags -lafnetworking -f <directory> Find framework Framework search in the specified directory Path-framework <framework_name> Specifies the framework name of the link other Link flags - Framework AFNetworkingCopy the code

CD Go to the directory where main.m resides and run the following command to compile main.m to main.o:

Clang-x objective-c \ -target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ -i/AFNetworking \ -c main.m -o main.oCopy the code

To compile main.m to main.o:

Big Sur is: x86_64-apple-macOS11.1, previously: x86_64-apple-macOS10.15 3. Use ARC 4. Use the SDK path in: Big Sur is: / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK before is: / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX10.15 SDK 5. Specify the header file path. 6. Output the target fileCopy the code

4. Link static libraries to generate executable files

Clang-target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - l. / AFNetworking \ -lAFNetworking \ main.o -o mainCopy the code

– name of libAFNetworking lAFNetworking links/AFNetworking dynamic library or static library

Lib +<library_name> static library lib+<library_name> static library lib+<library_name> static library

The executable file main is generated

Double click to execute:Print AFNetworking content. The link is successful

Compile the link using a script file

1. Create a script file

2. Run the preceding compilation and connection commandbuild.shFile:

O ======start" clang-x objective-c \ -target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ -i/AFNetworking \ - the main c. M - o main o echo "= = = = = =. The main m into the main o = = = = = = end" echo "= = = = = = link static library generated executable file = = = = = = start" clang - target X86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - l. / AFNetworking \ -lafNetworking \ main.o -o main echo "====== link static library generation executable ======end"Copy the code

3. CD Run the command in the current directory

./build.sh
Copy the code

May come up

Execute at this time

chmod +x build.sh
Copy the code

And then execute it again

./build.sh
Copy the code

The process is as follows:

The execution result

Static library principle

througharCommand can seelibAFNetworking.aThe library file is.oFile collection. Next link one through an example.oFile renamed library file

1. Prepare the project directory as follows:

TestStaticLib file code:

#import <Foundation/ foundation. h> NS_ASSUME_NONNULL_BEGIN @interface TestStaticLib: NSObject -(void)testStaticLib; #import "testStaticlib. h" @implementation TestStaticLib -(void) TestStaticLib { NSLog(@"---testStaticLib---"); } @end NS_ASSUME_NONNULL_ENDCopy the code

The main.m file code

#import <Foundation/Foundation.h>
#import "TestStaticLib.h"
int main() {
    TestStaticLib *lib = [TestStaticLib new];
    NSLog(@"test---%@",lib);
    return 0;
}
Copy the code

2. CD Go to the directory where testStaticlib. m resides and run the following command:

Clang-x objective-c \ -target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - c TestStaticLib. M -o TestStaticLib.oCopy the code

usefileViewing file Types

Lib +<library_name> <library_name> <library_name> <library_name> <library_name> <library_name> <library_name> <library_nameTestStaticLib.oThe file is renamed tolibTestStaticLib.dylib

usefileViewing file Types

Changed his name, orobjectType of file, if linked successfully and executed successfully, also indicates that the static library is.oA collection of files.

4. CD Go to the main.m directory and run the following command:

compile

Clang-x objective-c \ -target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ -i/StaticLib \ - c main.m -o main.oCopy the code

link

Clang-target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - l. / StaticLib \ -lTestStaticLib \ main.o -o mainCopy the code

4. Double-click

It can also be executed successfully

The dynamic library

Methods for obtaining third-party dynamic libraries (see)

In the same way as the static library, the difference is that #use_frameworks! The # sign in

1. Install AFNetworking using cocoPods and add it to target in your Podfile

use_frameworks!
pod 'AFNetworking'
Copy the code

2. After running the installation command successfully, open the xcworkspace file and run command+B to compile AFNetworking

3. Right-click Show in Finder to find AFNetworking and Headers

4. Rename the AFNetworking file to libAFNetworking. Dylib, name the Headers folder AFNetworking, and place libAFNetworking.

Lib + static library static library static library

Linked dynamic library

Use of rPATH and executable_path

The main project links to a dynamic library demo:

1. The static library will be linkedmain.mBring the file over and theAFNetworkingPut folders in the same directory: 2.cdGo to this directory (here is the link dynamic library) and execute the following command to change themain.mCompiled intomain.o:

Clang-x objective-c \ -target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ -i/AFNetworking \ -c main.m -o main.oCopy the code

3. Link static libraries to generate executable files

Clang-target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - l. / AFNetworking \ -lAFNetworking \ main.o -o mainCopy the code

4. Click the generated executable filemain, the results are as follows:

From the above error, the dyld in @ rpath/AFNetworking framework Versions/A/AFNetworking path can’t find the library

Run the otool command to view the main executable file

otool -l main | grep 'DYLIB' -A 5
Copy the code

Analysis: the (LC_LOAD_DYLIB) path is the same as the error message, need to modify the path. This path is provided by libAFNetworking. Dylib, so you need to modify libAFNetworking.

CD In the AFNetworking folder, run the otool command to view the executable file libAFNetworking

otool -l libAFNetworking.dylib | grep 'ID' -A 5
Copy the code

To be able to seemainIn theLC_LOAD_DYLIBislibAFNetworking.dylibIn theLC_ID_DYLIB, this path is the default path, we have changed the file directory, so now we need to change it

5. Go to the AFNetworking folder and run install_name_tool to change the directory. So we’re going to use at sign rpath, so the way to think about it is, if you want to link to the dynamic library, you have to provide at sign rPath, so that’s where it is, and this is main, so that’s where main is, Need to modify the path is @ rpath/AFNetworking/libAFNetworking dylib

Terminal execution:

install_name_tool -id @rpath/AFNetworking/libAFNetworking.dylib libAFNetworking.dylib
Copy the code

Run the otool command again

otool -l libAFNetworking.dylib | grep 'ID' -A 5
Copy the code

Modified successfully.

6. Repeat step 2.3 to associate the previous modification on main. Then double-click the executable file main

The library is still not found, but the path has been modified. In fact, the path here is only the back section, but also need to put the front section together to complete the path. This path exists in main

7. CD Go to the main directory and run the otool command to view the RPATH

otool -l main | grep 'RPATH' -A 5
Copy the code

There is no content output, that is, there is none, now I need to add, add command

install_name_tool -add_rpath @executable_path main
Copy the code

Executable_path Provides the previous path of the current executable file main. Run the otool command again to view RPATH

It has been added successfully

7. Double-click the executable filemain Note:

  1. Modify the library fileLC_ID_DYLIB
  2. Compile the link to generate the main project file
  3. Modify main project file addedLC_LOAD_DYLIB

Compile the link using a script file

Corresponding script file code:

/AFNetworking echo "====== "====== start" install_name_tool -id . @ rpath/AFNetworking/libAFNetworking dylib libAFNetworking dylib echo "= = = = = = modified dynamic library path = = = = = = end" # back to a directory on the popd echo "====== compile main.m to main.o======start" clang-x objective-c \ -target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ -i/AFNetworking \ - the main c. M - o main o echo "= = = = = =. The main m into the main o = = = = = = end" echo "= = = = = = link static library generated executable file = = = = = = start" clang - target X86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - l. / AFNetworking \ - lAFNetworking \ main. O - o main echo "= = = = = = link static library to generate an executable file = = = = = = end" echo "= = = = = = to modify the main path for executable files = = = = = = start" Install_name_tool -add_rpath@executable_path main echo "====== Modify the main path of the executable file ======end"Copy the code

The use of load_path

Main project link dynamic library A, dynamic library A link dynamic library B demo:

1. Create a project directory

2. Add the test file and the code main.m

#import <Foundation/Foundation.h>
#import "myLibA.h"
int main(a){
    myLibA *libA = [myLibA new];
    NSLog(@"----libA:%@",libA);
    [libA mylibAClass];
    return 0;
}
Copy the code

myLibA

//myLibA.h
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface myLibA : NSObject
-(void)mylibAClass;
@end

NS_ASSUME_NONNULL_END

//myLibA.m
#import "myLibA.h"
#import "myLibB.h"
@implementation myLibA
-(void)mylibAClass
{
    NSLog(@"=====MylibAClass");
    myLibB *lib = [myLibB new];
    [lib MylibBClass];
}
@end
Copy the code

myLibB

//myLibB.h
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface myLibB : NSObject
-(void)MylibBClass;
@end

NS_ASSUME_NONNULL_END

//myLibB.m
#import "myLibB.h"

@implementation myLibB
-(void)MylibBClass
{
    NSLog(@"=====myLibB");
}
@end

Copy the code

3. Connect mylibb. framework CD to mylibb. framework directory and execute

Compile:

Clang-target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ -i/Headers \ -c myLibB.m -o myLibB.oCopy the code

Links:

Clang-dynamiclib \ -target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - Xlinker -install_name -Xlinker @rpath/myLibB.framework/myLibB \ myLibB.o -o myLibBCopy the code
Parameter Description: -Xlinker passes parameters to the linkerCopy the code

Connect myliba. framework CD to myliba. framework directory and execute compilation:

Clang-target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ -i/Headers \ -I./Frameworks/myLibB.framework/Headers \ -c myLibA.m -o myLibA.oCopy the code

Links:

Clang-dynamiclib \ -target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - Xlinker -install_name -Xlinker @rpath/myLibA.framework/myLibA \ -F./Frameworks \ -framework myLibB \ myLibA.o -o myLibACopy the code

Leave a hole here and do not add an executor path linking myLibB

5. Go to the directory where main.m is located, and connect main.m to compile:

Clang-target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ -I./Frameworks/myLibA.framework/Headers \ -c main.m -o main.oCopy the code

Links:

Clang \ -target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - F/Frameworks \ -framework myLibA \ main.o -o mainCopy the code

Add performer path to main:

install_name_tool -add_rpath @executable_path/Frameworks main
Copy the code

6. Double-click the main executable file

7. To modifymyLibAtheLC_RPATH

install_name_tool -add_rpath  @executable_path/Frameworks/myLibA.framework/Frameworks myLibA
Copy the code

8. Repeat Step 5 and double-click MainThe successful running@executable_path/Frameworks/myLibA.frameworkIt looks a little bit longer. It works@loader_pathReplace step 7

install_name_tool -add_rpath  @loader_path/Frameworks myLibA
Copy the code

Compile the link using a script file

Echo '= = = = = = = = = = processing myLibB = = = = = = = = = = start' pushd. / Frameworks/myLibA framework/Frameworks/myLibB framework clang - target X86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ -i/Headers \ -c Mylibb. m -o mylibb. o clang-dynamiclib -target x86_64-apple-macos11.1 -fobjc-arc -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - Xlinker - install_name - Xlinker @ rpath/myLibB. Framework/myLibB \ myLibB o - o myLibB popd echo '= = = = = = = = = = processing myLibB = = = = = = = = = = end' Echo '= = = = = = = = = = processing myLibA = = = = = = = = = = start' pushd. / Frameworks/myLibA framework clang - target x86_64 - apple - macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ -i/Headers \ - i. / Frameworks/myLibB. Framework/Headers \ - c myLibA. M - o myLibA o clang dynamiclib \ - target x86_64 - apple - macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - Xlinker -install_name -Xlinker @rpath/myLibA.framework/myLibA \ -F./Frameworks \ -framework myLibB \ myLibA.o -o myLibA Install_name_tool -add_rpath@loader_path /Frameworks myLibA popd echo '========== handle myLibB==========end' echo '= = = = = = = = = = to deal with the main = = = = = = = = = = start' clang - target x86_64 - apple - macos11.1 \ - fobjc - arc \ - isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - i. / Frameworks/myLibA. Framework/Headers \ - the main c m - the main o o clang \ - target x86_64 - apple - macos11.1 \ - fobjc - arc \ - isysroot/Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ -F./Frameworks \ -framework myLibA \ main.o -o main install_name_tool -add_rpath @executable_path/Frameworks main echo '= = = = = = = = = = processing myLibB = = = = = = = = = = end'Copy the code

– use reexport_framework

Based on the previous example, we know that their link relationship looks like this: main-myliba-mylibb. What if Main wants to call the method changes in myLibB? Just take the following 3 steps:

  1. Modify themain.mThe code of
#import <Foundation/Foundation.h>
#import "myLibA.h"
#import "myLibB.h"
int main(a){
    myLibA *libA = [myLibA new];
    NSLog(@"----libA:%@",libA);
    [libA mylibAClass];
    myLibB *libB = [myLibB new];
    NSLog(@"----libB:%@",libB);
    [libB MylibBClass];
    return 0;
}

Copy the code
  1. Will myLibBThe symbolic path joinsMyLibA `, use

-reexport_framework

Go to the myliba. framework folder and run

Clang-dynamiclib \ -target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - Xlinker -install_name -Xlinker @rpath/myLibA.framework/myLibA \ -Xlinker -reexport_framework -Xlinker myLibB \ -F./Frameworks \ -framework myLibB \ myLibA.o -o myLibACopy the code

mainthroughLC_REEXPORT_DYLIBfindmyLibB1. After telling the compiler where myLibB’s header file is found, generate a new onemain cdtomainCompile:

Clang-target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ -I./Frameworks/myLibA.framework/Headers \ -I./Frameworks/myLibA.framework/Frameworks/myLibB.framework/Headers \ -c main.m -o main.oCopy the code

Links:

Clang \ -target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - F/Frameworks \ -framework myLibA \ main.o -o mainCopy the code

Add performer path to main:

install_name_tool -add_rpath @executable_path/Frameworks main
Copy the code

Finally, check the running results of the program:

Dynamic library principle

1. Prepare the project directory

TestDyLib code

/ / TestDyLib. H code

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface TestDyLib : NSObject
-(void)TestDyLib;

@end

/ / TestDyLib. M code

#import "TestDyLib.h"

@implementation TestDyLib
-(void)TestDyLib
{
    NSLog(@"---TestDyLib---");
}
@end

Copy the code

The main m code

#import <Foundation/Foundation.h>
#import "TestDyLib.h"
int main(a) {
    TestDyLib *lib = [TestDyLib new];
    NSLog(@"test---%@",lib);
    return 0;
}
Copy the code

2. CD to the directory where the testdylib. m file is stored and compile the testdylib. m file as a target file:

Clang-target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - c TestDyLib. M - o TestDyLib.oCopy the code

3. Use libtool to compile testdylib. o into a static library

libtool -static -arch_only x86_64 TestDyLib.o -o libTestDyLib.a
Copy the code
Parameter description: -static: compiles static libraries. - ARCH_only X86_64: architectureCopy the code

4. Use the LD linker to link libtestdylib. a to a dynamic library

Ld-dylib-arch x86_64 \ -macosx_version_min 11.1 \ -syslibroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - lsystem - framework  Foundation \ libTestDyLib.a -o libTestDyLib.dylibCopy the code
Parameter description: -dylib: linked dynamic library -arch_only X86_64: architecture -macOSX_version_min 11.1: minimum supported version Syslibroot: used SDK path -lsystem: dependent system frameworkCopy the code

5. Go to the main.m directory and compile it into a target file

Clang-target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ -i/dylib \ - c main.m -o main.oCopy the code

6. Link to generate executable files

Clang-target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - l. / dylib \ -lTestDyLib \ main.o -o mainCopy the code

An error was reported. The symbol could not be found. 7. Go back todylibDirectory, modify linker parameters, and then run

Ld-dylib-arch x86_64 \ -macosx_version_min 11.1 \ -syslibroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - lsystem - framework  Foundation \ -all_load libTestDyLib.a -o libTestDyLib.dylibCopy the code
-all_load: tells the compiler to load all symbols regardless of whether they are usedCopy the code

8. Perform Step 6. The main executable file is successfully generated

This error is a path problem, which is addressed in the link dynamic library section above:

  • Modify the library fileLC_ID_DYLIB

  • Compile the link to generate the main project file, and perform steps 5 and 6

  • Add LC_LOAD_DYLIB to main project file

  • Double-click the executablemain

Able to see, able to link static libraries into dynamic libraries.

Conclusion: dynamic library is the product after the.o file link, is the final product of the link, it is more than static library to go through a link process.

Compile the link using a script file

Script file code:

Pushd./dylib echo "======TestDyLib compile into target file ======start" clang-target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - c TestDyLib. M - o Testdylib. o echo "======TestDyLib compile into target file ======end" echo "====== compile testdylib. o into static library ======start" # Xcode-> static library libtool O -o libtestdylib. a echo "====== compile testdylib. o into static library ======end" echo "====== link testdylib. a into a dynamic library ======start" ld-dylib-arch x86_64 \ -macosx_version_min 11.1 \ -syslibroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - lsystem - framework Foundation \ -all_load \ libtestdylib. a -o libtestdylib. dylib echo "====== link to testdylib. a dynamic library ======end" Install_name_tool - id @ rpath/dylib/libTestDyLib dylib libTestDyLib. Dylib popd echo "= = = = = = the main compiled into object file = = = = = = start" clang -target x86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ -i/dylib \ - c Main. M - o main. O echo "= = = = = = the main compiled into object file = = = = = = end" echo "= = = = = = link lTestDyLib. Dylib = = = = = = start" clang - target X86_64-apple-macos11.1 \ -fobjc-arc \ -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX11.1 SDK \ - l. / dylib \ -ltestdylib \ main.o -o main echo "====== link ltestdylib. dylib======end" install_name_tool -add_rpath@executable_path mainCopy the code