Library to library links have the following four forms, the following project demonstration:

Dynamic libraries link dynamic libraries

1. Create project MyApp 2. Add a Target, select Framework

In 3.MyAppDirectory creationPodfileIn thetarget 'MyDylib' doAdd the following code:

target 'MyDylib' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for MyDylib
  pod 'AFNetworking'

end
Copy the code

4. Re-use xcWorkspace to open the project and add files to MyDylib:

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyDyLibTest : NSObject
-(void)myDyLibTest;
@end

NS_ASSUME_NONNULL_END
Copy the code
#import "MyDyLibTest.h"
#import<AFNetworking.h>
@implementation MyDyLibTest
-(void)myDyLibTest
{
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    NSLog(@"myDyLibTest===%@",manager);
}
@end
Copy the code

5. Drag the header file to the specified location

6.ViewController.mAdd the calling code to:

#import "ViewController.h"
#import <MyDylib/MyDyLibTest.h>

@interface ViewController (a)

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    MyDyLibTest *libTest = [MyDyLibTest new];
    [libTest myDyLibTest];
    
    // Do any additional setup after loading the view.
}


@end
Copy the code

7.Scheme selects MyApp to run the project

AFNetworking is not found in MyDylib @rPath

MyDylib is linked by App, but AFNetworking is not in the combined path of @rpath saved by MyDylib and install_name of AFNetworking

= @rpath of MyDylib + install_name of AFNetworking

There are two solutions:

  • The first is to copy to a dynamic libraryAFNetworkingTo the specified path.

Add pod ‘AFNetworking’ to the target project. This works because the pods-myapp-frameworks. Sh file is already copied by script

if [[ "$CONFIGURATION" == "Debug" ]]; then
  install_framework "${BUILT_PRODUCTS_DIR}/AFNetworking/AFNetworking.framework"
fi
if [[ "$CONFIGURATION" == "Release" ]]; then
  install_framework "${BUILT_PRODUCTS_DIR}/AFNetworking/AFNetworking.framework"
fi
Copy the code
  • The second is to modify the dynamic libraryMyDylibthe@rpathFor the dynamic libraryAFNetworkingtheinstall_nameThe absolute path before

${BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)/AFNetworking
Copy the code

The problem can be solved in both ways.

The dynamic library calls the App code back

1. Create App project classes and configure them

2. Dynamic library calls App code

#import "MyDyLibTest.h"
#import<AFNetworking.h>
#import "MyAppClass.h"
@implementation MyDyLibTest
-(void)myDyLibTest
{
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    NSLog(@"myDyLibTest===%@",manager);
    
    MyAppClass *cls = [MyAppClass new];
    NSLog(@"MyAppClass===%@",cls);
}
@end
Copy the code

3. Errors were reported in far-away projects

The solution is to tell the linker to search dynamically. usexcconfigConfiguration:

Create it under the dynamic library MyDyLib

Import the XCconfig file created by Pod into the xcconfig file created by yourself

The following parameters can be used:

-undefined

The problem with this approach is that any other symbols defined will not report an error

-U symbol_name

This way only for the specified symbol, ignore the error message after the modification of the project:

App calls dynamic library AFNetworking code

  • The first way is to just goMyAppUnder the engineering podAFNetworking
  • The second way is through SettingsFRAMEWORK_SEARCH_PATHS

${BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)/AFNetworking
Copy the code

Dynamic libraries link static libraries

1. The configuration project is basically the same as the previous example, except that the static library is used in the Podfile

target 'MyDyLib' do
  # Comment the next line if you don't want to use dynamic frameworks
  # use_frameworks!

  # Pods for MyDyLib
 pod 'AFNetworking'
end
Copy the code

use_frameworks! Viewcontroller.m: viewController.m: viewController.m: viewController.m: viewController.m: viewController.m: viewController.m: viewController.m

#import "ViewController.h"
#import <MyDyLib/MyDyLib.h>
@interface ViewController (a)

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    MyDyLibTest *libTest = [MyDyLibTest new];
    [libTest myDyLibTest];
}
Copy the code

The operation succeeds

The reason is that the static libraries it depends on are linked to the dynamic library when compiling the dynamic library, so no errors are reported

3.App uses static library code

#import "ViewController.h"
#import <MyDyLib/MyDyLib.h>
#import <AFNetworking/AFNetworking.h>
@interface ViewController (a)

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    MyDyLibTest *libTest = [MyDyLibTest new];
    [libTest myDyLibTest];
    
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    NSLog(@"myDyLibTest===%@",manager);
}
Copy the code

Header file not found, need to be configuredHeader Search Paths

After the configuration, run the command

Static libraries link static libraries

1. Configuration project, refer to the previous two configurations, when creating the static library to modify this area

2. Related code: MyStaticLibTest code:

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyStaticLibTest : NSObject
-(void)testMyStaticLib;
@end

NS_ASSUME_NONNULL_END

Copy the code
#import "MyStaticLibTest.h"
#import <AFNetworking/AFNetworking.h>
@implementation MyStaticLibTest
-(void)testMyStaticLib
{
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    NSLog(@"testMyStaticLib===%@",manager);
}
@end

Copy the code

ViewController. M code

#import "ViewController.h"
#import <MyStaticLib/MyStaticLib.h>
@interface ViewController (a)

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    MyStaticLib *lib = [MyStaticLib new];
    [lib testMyStaticLib];
}
@end
Copy the code

3. Errors reported on long trips

There is no problem when the App links to the static library, but there is a problem when the static library links to the static library AFNetworking. Because the static library AFNetworking isn’t incorporated into the static library I created myself, the App doesn’t know where it is. Solution:

  • Tell App, static libraryAFNetworkingWhere is the

${BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)/AFNetworking
Copy the code

After the configuration is completed, the remote program

Static libraries link dynamic libraries

2. Related code: MyStaticLibTest code:

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyStaticLibTest : NSObject
-(void)testMyStaticLib;
@end

NS_ASSUME_NONNULL_END
Copy the code
#import "MyStaticLibTest.h"
#import <AFNetworking/AFNetworking.h>
@implementation MyStaticLibTest
-(void)testMyStaticLib
{
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    NSLog(@"testMyStaticLib===%@",manager);
}
@end
Copy the code

ViewController. M code:

#import "ViewController.h"
#import <MyStaticLib/MyStaticLib.h>
@interface ViewController (a)

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    MyStaticTest *lib = [MyStaticTest new];
    [lib testMyStaticTest];
}
@end
Copy the code

3. An error occurs during operation

Where is AFNetworking

Again, a familiar mistake

The dynamic library could not be found under this pathAFNetworking, follow the prompted path to have a look

Missing the red color library above

4. Use the script to copy the dynamic library AFNetworking to the current directory

Copy a file ending in.sh from the project in the POD AFNetworking dynamic library and place it in the project root directory

Configure the correct path in the Run Script

5. Compile and run

It’s been copied and it’s working fine

When the static library is generated, only the name of the dynamic library is saved. After the App links the static library, all the codes of the static library will be linked in, but the App does not know the location of the dynamic library.