iOS classic error library not found for -lXXX

We often ibrary not found for -xxxdeal with it during development, especially when we clone a project from a remote warehouse or github. Compilation is likely to report this error. As shown below:

 

Error message


library not found for -lXXX is one of the classic iOS errors. Below we illustrate the solution to this error by reproducing it. Where l means lib (library).

First of all, let me state that my project references Umeng’s statistics SDK, which is called libMobClickLibrary.a. Stored in the third_party directory, as shown below (the left picture shows the location of libMobClickLibrary.a in the project directory/virtual directory, the right picture shows the location of libMobClickLibrary.a in the disk directory/physical directory):

 

Project directory

 

disk directory

Library Search Paths are not configured, resulting in an error

(1) Delete building setting -> Library Search Pathsthe following  $(PROJECT_DIR)/YunFu/third_party/UMAnalytics. As shown below:
So, you may ask, why should we delete this path instead of other paths? Because I know that this path is the directory of the libMobClickLibrary.a static library.

 

(2) command + B compile the project, and then as expected, we will encounter the following error:

 

(3) Click on this error, and then you can see this classic error, as shown below:
This error means that the library named MobClickLibrary cannot be found.

 

At this point, we know that just because we deleted $(PROJECT_DIR)/YunFu/third_party/UMAnalytics under Library Search Paths, the library not found for -lMobClickLibrary error was reported. So next time we encounter similar problems, we will know that the path of a certain static library may not be configured.

Conclusion 1: Failure to configure the search path of the static library causes library not found for lXXX to appear.
Note: The path set must be the path of the static library on the disk (physical directory), and cannot be set to the virtual directory of the project. Take this example, libMobClickLibrary.a is placed in the third_party folder in the project, and its real disk path is the UMAnalytics folder under third_party.


Library Search Paths are not configured correctly, resulting in errors

The above situation is that we have not configured the path of a static library, which will cause the library not found for lXXX error. Sometimes, we do configure the static library path under Library Search Paths, but still get the same error. At this point we have to consider whether the path configuration is correct or not. As shown below (the correct path and incorrect path of libMobClickLibrary.a respectively):
Correct path:

 

correct path

Wrong path (the last level directory "/UMAnalytics" is removed):

 

wrong path

Compiling the project will still report the same error library not found for -lMobClickLibrary.a, as shown below:

 

error message

Ultimately, the reason is that although we specified the directory of libMobClickLibrary.a with him, the static library libMobClickLibrary.a was not found in the specified directory. At this time, there are two situations:

  • Situation 1: If the directory we specify is completely unrelated to the directory of the static library libMobClickLibrary.a, then you need to set it up again under Library Search Paths (note: the disk path of the static library is set) .

  • Case 2: If the directory we specify is related to the directory of the static library. That is, the directory we specify is the superior directory of the directory where the static library is located, or the superior directory. In this case, there is no need to specify the exact path of the static library, just change the non-recursive option to recursive . As shown below:

     

    set recursive

Because the recursive option means recursively searching static libraries in the directory we specify. That is, if the static library is not found in the current directory, then continue searching in the subdirectories of the current directory. The default is non-recursive.

Conclusion 2: Although the search path of the static library is configured in Library Search Paths. However, the incorrect configuration resulted in library not found for lXXX.


Importing third-party framework static library and compiling error xxx.h file not found

What we talked about above is that when our project imports a .a static library, but because the search path of the .a static library is not configured in Library Search Paths, or the path is configured but the path configuration is wrong, library not found for - Solution to xxx error.
But we know that in iOS, static libraries come in two forms: static libraries in .a format and static libraries in .framework format.
So if our project introduces a static library in .framework format, do we need to configure the search path? The answer is yes, no matter how we import the .framework static library (cocoapods or dragging the framework static library directly into the project), if the search path of the framework is not configured, an error will still be reported, but the error will be reported. But it is somewhat different from the .a format static library. If the framework format static library is introduced into our project and the static library is used (the so-called use means importing a certain header file in the framework static library), but the search path is not configured, then An error like xxx.h file not found will be reported . As shown below:

 

xxx file not found

Because my project introduced a static library called PushCenterSDK.framework. At the same time, a certain API in this static library is also used. As shown in the figure below, they are the path and disk path of the static library in the project:

 

path in project

 

path on disk

We still use the method of reproducing the problem to verify the method of solving the error. First, my project can be compiled and passed, and then try to delete the search path (disk path) of PushCenterSDK.framework under build setting -> Framework Search Paths, as follows The pictures are screenshots before and after deletion:

Before deletion:

 

Before deletion

After deletion:

 

After deletion

After deleting the path and compiling the project, I found the following error XXX.h file not found, as shown below: Obviously
, the TBSDKPushCenterEngine.h file under PushCenterSDK was not found.

 

Report an error

To sum up, the path of the framework static library is also necessary. If the path of the framework static library in the project is configured incorrectly or is not configured, an error similar to xxx.h file not found will be reported. Next time you encounter this kind of error, you can start thinking about framework search paths.

Similarly, the search path of the framework static library also supports recursive search. We do not have to configure an exact path. We can specify a parent directory and then select recursive.

Conclusion: No matter how we import the .framework static library (cocoapods or dragging the framework static library directly into the project), if the search path of the framework is not configured, an error will still be reported, but the error reported is the same as The static library in .a format is somewhat different. If the framework format static library is introduced in our project and the static library is used (the so-called use means importing a header file in the framework static library), but the search path is not configured, then an error message will be reported. xxx.h file not found errors.

Guess you like

Origin blog.csdn.net/yx1166/article/details/122079224