clang: error: linker command failed with exit code 1 (use -v to see invocation) 解决办法

时间:2023-02-13 20:14:55

下面是*的原话摘录

The fact that it’s a linker error should point you into the right direction. Compilation errors usually mean an error in your syntax. Linker error means that although your source files have been compiled correctly, but when the time comes to be linked with other frameworks (system frameworks, or 3rd party ones), clang cannot find them in place.

Usually, this is because a 3rd party library is missing from your system, and your project depends on it, and although it can find the header files, it cannot find the actual library file to connect to. The fact that transferring your project to other computers is giving you problems, while at your computer compiles and links correctly also strengthens the assumption that probably it’s a problem with the environment you are trying to compile with and you need to install something.

To find out more about the issue, go into Xcode, go into the report navigator, and you will see logs of all the builds you have made. Check the first one (which is the last build). Scroll to the bottom, and see exactly why it didn’t compile. The most common scenario is that some symbol could not be found. Check in which library this symbol belongs, then install this library to your system.

我就不翻译了,有点英语基础的都能看懂!

这种问题,通常出现在添加第三方库文件或者多人开发时,导致找不到文件而出现的链接错误,我们可以从下面着手解决。

1】以如下错误,如果是多人开发,你同步完成后发现出现如下的错误。

 Undefined symbols for architecture armv7:  
  "_OBJC_CLASS_$_MyPageLogViewController", referenced from:  
      objc-class-ref in BaiduMobStatAppDelegate.o  
ld: symbol(s) not found for architecture armv7  
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

错误中出现了“MyPageLogViewController”这个类,你可以找到这个类的.m文件, 查看他的Target Membeship, 如下图
clang: error: linker command failed with exit code 1 (use -v to see invocation) 解决办法
如果没有勾选上,点击勾选,clean 后 build一下。

2】如果是新添加的第三方库,且是动态库。

先重复第一步过程,然后找到Build settings->Linking->Other Linker Flags,将此属性中添加你增加的动态库,如RxSwift,该库需添加RxSwift和 RxCocoa,如图:clang: error: linker command failed with exit code 1 (use -v to see invocation) 解决办法。 或者,简单直接一点将该属性修改成 -all_load 或者 -Objc(Objective-C),总之可以多试几次。

4】如果添加的是静态库



 Undefined symbols for architecture armv7: "_OBJC_CLASS_$_BaiduMobStat", referenced from: objc-class-ref in BaiduMobStatAppDelegate.o objc-class-ref in MyPageLogViewController.o (maybe you meant: _OBJC_CLASS_$_BaiduMobStatAppDelegate) ld: symbol(s) not found for architecture armv7 clang: error: linker command failed with exit code 1 (use -v to see invocation) 

在用到这个库的所有文件中都出现了错误, 如上 BaiduMobStatAppDelegate 类和 MyPageLogViewController类

这种情况就可能是这个静态库路径混乱导致的链接错误

解决方法:Build settings->Search Path->Library Search Paths 添加静态库的相应路径。如下图
clang: error: linker command failed with exit code 1 (use -v to see invocation) 解决办法

如果上面的所有方法都不管用。你可以再试下下面的方法:
1,看看是不是有新添加的文件跟之前文件同名

2,错误信息中出现了某个类的名字,去原文件中看看#import了哪些第三方库,把这些库挨个注释排除,找到出错的那个库,然后按照官方提供的步骤重新添加一遍。