Search code examples
xcodeclangaddress-sanitizer

xcode build error: Could not find path to clang binary to locate Address Sanitizer library


When I have the Address Sanitizer option turned off in Xcode(version 15.3), the project builds successfully. However, as soon as I enable the option, the build fails with the error message: 'Could not find path to clang binary to locate Address Sanitizer library.'

xcode asan option

xcode build error

I found libclang_rt.asan_ios_dynamic.dylib in the Xcode directory and added it to the build Phases -> Link Library with Libraries. However, I still get the same error. But when I create a new project and enable the Address Sanitizer option, it builds successfully.


Solution

  • This problem was caused by incorrect CC and CXX macro settings. When I checked the Pods-[TargetName]-debug.xcconfig file, I found that CC was set to clang and CXX was set to clang++.

    Open a ternimal, enter xcodebuild -find clang and xcodebuild -find clang++ , then I can get the real path of clang and clang++. I manually modified the Pods-[TargetName]-debug.xcconfig file:
    CC = /Applications/Xcode-15.3.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
    CXX = /Applications/Xcode-15.3.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++.
    Then the xcode build error was gone.

    Furtherly, I want to know why the wrong settings of CC=clang CXX=clang++ appear. By looking at the cocoapods source code, I founded that CC and CXX were not hard coded. After some exploration, I finally founded that this problem could repeat if the Snappy pod library was referenced in the project.
    Snappy Podspec Details

    Final solutions(add these lines to your Podfile):

    pre_install do |installer|
      fix_cc_cxx_macro_path installer
    end
    
    def fix_cc_cxx_macro_path(installer)
      installer.pod_targets.each do |pod_target|
        pod_target.specs.each do |spec|
          next if spec.attributes_hash['xcconfig'].nil?
          if spec.attributes_hash.dig('xcconfig', 'CC')
            spec.attributes_hash['xcconfig']['CC'] = `xcodebuild -find clang`
          end
          if spec.attributes_hash.dig('xcconfig', 'CXX')
            spec.attributes_hash['xcconfig']['CXX'] = `xcodebuild -find clang++`
          end
        end
      end
    end