Why do I get iOS linker errors with my static libraries?
Asked Answered
C

21

111

I have a problem with linking my mixed language framework to a project.

  1. I create a framework with Swift and Objective-C classes.

  2. The main logic was stored in the Swift file. For example the class with a method that calls NSLog("Swift log was called").

  3. Objective-C file has a class that has a method in which I create an instance of Swift class and call Swift-log method.

  4. I link this framework with my Objective-C project, I can call all that I need in this project, but when I want to build this project I receive error linker command failed with exit code 1 (use -v to see invocation)

    And warnings:

ld: warning: Could not find auto-linked library 'swiftFoundation'
ld: warning: Could not find auto-linked library 'swiftDarwin'
ld: warning: Could not find auto-linked library 'swiftCoreFoundation'
ld: warning: Could not find auto-linked library 'swiftCore'
ld: warning: Could not find auto-linked library 'swiftCoreGraphics'
ld: warning: Could not find auto-linked library 'swiftObjectiveC'
ld: warning: Could not find auto-linked library 'swiftDispatch'
ld: warning: Could not find auto-linked library 'swiftSwiftOnoneSupport'

I also saw the solution with importing empty Swift-file, but it necessary to make a project without any trash.

Christoperchristoph answered 27/9, 2018 at 11:51 Comment(3)
@PhilippHolly I need a solution that will not include empty files.Christoperchristoph
I am sorry, I have not found any solution so far. let me know if you find something.Hyposthenia
Hi Any solution for this ?Decern
M
194
  1. Open ios/YourAppName.xcodeproj in Xcode
  2. Right-click on Your App Name in the Project Navigator on the left, and click New File…
  3. Create a single empty Swift file to the project (make sure that Your App Name target is selected when adding)
  4. when Xcode asks, press Create Bridging Header and do not remove Swift file then. re-run your build.

This should fix the problem

Maestricht answered 8/2, 2019 at 6:16 Comment(3)
What If I already have an existing bridging header and the problem persists? I'm running on MacOS Mojave and XCode 11.3.1Throb
what if you're already have bridging file, can someone explain pleaseTailing
Xcode does not ask 'Create Bridging Header' , Xcode version: 12.4Gaspar
A
99

In my case it was a React-Native project after adding a Swift Pods library. Maybe just the step 2 can work:

  1. Adding a new Swift file and a Brigde header:

1) File -> New -> File <code>File -> New -> File</code>

2) Select Swift File Select <code>Swift File</code>

3) Confirm Create Bridging Header enter image description here

  1. Go to Build Settings and set Always Embed Swift Standard Libraries to YES Always Embed Swift Standard Libraries
Aragats answered 16/5, 2019 at 21:41 Comment(5)
Where will i put the file, under pods or the project?Hodgson
@Hodgson I've put it in the projectAragats
Do not remove the dummy Swift file after the bridging file is created! Keep it or build failsInhaler
Step Create Bridging Header does not appear for me. It says Save File for me.Harshman
@Harshman that steps appears only if your filename contains "bridging-header"Havelock
C
93

Apparently for pure objective-c projects you will need to add this $(TOOLCHAIN_DIR)/usr/lib/swift/$(PLATFORM_NAME) to the library search paths of your target. This worked for me when I was including a library written in swift to a project in objective-c

Clavicorn answered 1/11, 2018 at 12:47 Comment(8)
can you give us an example how we can added, as i am trying it but i don't really work for me , maybe aim placing it in a wrong placeRue
@Clavicorn How to set this in podspec, and not need add it manually?Baht
This worked perfectly! For anyone wondering how exactly to do this; click your xcode project in the file explorer -> under Targets select your target -> select the Build Settings tab -> search for Library Search Paths and add $(TOOLCHAIN_DIR)/usr/lib/swift/$(PLATFORM_NAME) to the debug and release sections.Valrievalry
Unfortunately this trick seems to have stopped working when using Swift 5 with Xcode 10.2 - probably because there are now system-wide Swift libs with iOS 12.2.Bignoniaceous
For those who are new to Xcode make sure "All" is selected and is blue else you might not find Library search pathOneway
@Bignoniaceous so how do fix it?Pentheus
Try adding the following to your target's library search path: $(TOOLCHAIN_DIR)/usr/lib/swift-5.0/$(PLATFORM_NAME)Repeal
It compiles but then I got a crash at the launch of the application: "This copy of libswiftCore.dylib requires an OS version prior to 12.2.0". If you want to solve it just add an empty swift file in the project, keep it forever and it will work. (The problem come from the fact that you are using both objective-c and swift code in your app)Swampland
L
26

I have export my unity project. Earlier it was working just to add empty swift file with press create Bridging Header.

but recently i have updated my xCode to 12.3 and it stop working.

So, tried few other ways.

I have added $(SDKROOT)/usr/lib/swift to Build settings > Search paths > Library search paths.

but still it doesn't work. So, i have added in same path i.e Build settings > Search paths > Library search paths.

$(TOOLCHAIN_DIR)/usr/lib/swift/$(PLATFORM_NAME)

$(TOOLCHAIN_DIR)/usr/lib/swift-5.0/$(PLATFORM_NAME)

Now it working perfect. Didn't get exact problem or solution but it works well with this way.

I just add these three in Build settings > Search paths > Library search paths.

  1. $(SDKROOT)/usr/lib/swift
  2. $(TOOLCHAIN_DIR)/usr/lib/swift/$(PLATFORM_NAME)
  3. $(TOOLCHAIN_DIR)/usr/lib/swift-5.0/$(PLATFORM_NAME)

Separately TOOLCHAIN_DIR directories doesn't work. If i add $(SDKROOT)/usr/lib/swift then only it work.

Loverly answered 6/1, 2021 at 19:49 Comment(4)
Tested in Xcode 12.5, $(SDKROOT)/usr/lib/swift needs to be the firstEmanative
Thanks, it work for Xcode 13.1Logogram
Thanks, it work for Xcode 13.2Aerator
Xcode 13.4.1 - it worked. Thanks! I tried empty swift files, creating bridging headers manually, removing those lines - nothing worked. The final solution included adding these 3 lines to the main target and removing those lines from another target which was a dependency to the main oneBede
I
22

For XCode 11 beta 4 Library Search Paths should be:

$(TOOLCHAIN_DIR)/usr/lib/swift-5.0/$(PLATFORM_NAME)
Interrupt answered 20/7, 2019 at 16:24 Comment(3)
I had to add this to each of my targets Library Search Paths property. Not just the main app. So, including my watch kit and Siri Intents targets.Gritty
Any targets with Swift code need to have the above mentioned path for XCode 11Repeal
Note if you have this in Xcode 12 still, it can f* your build.]\Siliceous
T
15
  1. Right click project name and add a new swift file leave it as File.swift
  2. it will ask you to create a new bridging header accept
  3. Run the project again
Ticktacktoe answered 6/5, 2020 at 4:18 Comment(0)
D
13

Try this workaround https://mcmap.net/q/82796/-it-gives-errors-when-using-swift-static-library-with-objective-c-project

Basically just add a dummy swift file in your project, and re-run your build.

Diphenylhydantoin answered 22/10, 2018 at 23:29 Comment(1)
You are the second who suggest this solution :). As I said in comments to description of my question, I need solution without empty Swift-files.Christoperchristoph
G
10

If you have issue after update xCode to version 12 and errors

Could not find or use auto-linked library 'swiftWebKit'

and two more.

Just addd $(SDKROOT)/usr/lib/swift to the Build settings > Search paths > Library search paths.

Hope its help :)

Godforsaken answered 24/9, 2020 at 6:44 Comment(4)
@SehiiVolynets, Please use italics (use * brackets) for pathways.Sentence
@maciejwww, what do you mean by "italics" and "asterisk brackets"? Which one should be used?Biller
I mean formatting policy on StackOverflow. Using asterisk brackets formats text into italics: *text* --> text. For pathways in GUI, it's recommended to use italics or bold (double asteriks: **text**) font. However, in this case is better to use bold font than italics, I suppose. Now, my comment is no longer needed because @Niko have edited yours answer. Check it out and compare with your version. Short info about text fotmatting and example you can find here.Sentence
Worked for me! Thanks Xcode 12.5 iOS 14.5Fumed
P
9

If your Xcode version is >=12.2

Go to

BuildSettings -> Linking -> runpath search path

and add /usr/lib/swift as your 1st argument like this:

enter image description here

Piscatory answered 1/11, 2019 at 8:42 Comment(3)
Can someone explain to me why the -1? Because I tried this and now I no longer need to constantly Clean => Rebuild whenever I change a single line. Note: I also needed to set "Enable Bitcode" as "No"Inefficacy
Worked fine for me, thanks a lot!!Diacritical
When linking on command line, -L /usr/lib/swift worked great. It's much shorter than using the equivalent of -L $(SDKROOT)/usr/lib/swift. Thanks!Imprison
H
7

Xcode 12.5, React Native.

Change: 'Library Search Paths' from Swift-5.0 to Swift-5.2enter image description here

Hypertonic answered 30/5, 2021 at 12:15 Comment(1)
Hey thanks a lot for this! Changing the Library Search Paths from swift-5.0 to swift-5.2 made my app finally build and run (it has nothing to do with watch apps or widgets).Pepin
H
6

What worked for me in a React Native project is to move $(inherited) to the bottom of the list in Build Settings -> Library Search Paths

enter image description here

Humming answered 21/3, 2022 at 10:1 Comment(2)
worked for me too. can you tell us why it works?Frankiefrankincense
I have no idea, maybe an iOS dev can answer that.Humming
H
3

I've got the same errors when accidentally compiling a project with a Swift 5.0 Snapshot configured in the Xcode preferences » Components » Toolchain.

Hornstein answered 30/1, 2019 at 13:57 Comment(1)
I've changed it on purpose...any way I can compile it on arm64 (works fine on simulator)?Kranz
A
2

I am getting this error after installing Lottie-react-native?

The solution only works if it is caused by adding Lottie package.

This worked for me:

yarn add lottie-react-native
yarn add [email protected]

And then must run:

pod install or pod update

I am using:

"lottie-ios": "3.2.3",
"lottie-react-native": "^5.0.1",
"react": "17.0.2",
"react-native": "0.67.3"
Arluene answered 12/4, 2022 at 10:17 Comment(0)
R
1

XCode 11.3 beta, Swift 5.0

I just add my targets to the podfile:

platform :ios, '9.0' 
use_frameworks! 
inhibit_all_warnings!

def shared_pods
    # Pods for NamaIOS
    pod 'Alamofire'
    pod 'RxSwift'
end

target 'MyApp' do
  use_frameworks!

  shared_pods

end

target 'MyApp-Test' do
  use_frameworks!

  shared_pods

end

target 'MyApp-Development' do
  use_frameworks!

  shared_pods

end
Relinquish answered 16/12, 2019 at 11:29 Comment(0)
B
1

if you come from lottie-react-native install instructions just add use_frameworks! to podFile #536 (comment)

Braiding answered 23/2, 2020 at 7:5 Comment(0)
A
1

I am android programmer. It took me a while understand what they were doing. I have currently Xcode 13.2.1 And flutter 1 awful combination I know. Please edit in other side xcode will take the enter like "I end" I change Build settings > Search paths > Library search paths.: From:

$(inherited) $(PROJECT_DIR)/Flutter

to:

$(inherited) $(PROJECT_DIR)/Flutter $(SDKROOT)/usr/lib/swift $(TOOLCHAIN_DIR)/usr/lib/swift/$(PLATFORM_NAME) $(TOOLCHAIN_DIR)/usr/lib/swift-5.0/$(PLATFORM_NAME)

Thats all, I hope to help some folk.

Aerator answered 2/3, 2022 at 15:28 Comment(0)
Z
0

In case you encounter this error while using Xcode 11 beta 4 in combination with carthage see this workaround:

https://github.com/Carthage/Carthage/issues/2825

Basically it consists of creating a xcconfig with the fixes and inject these into each build.

Zobkiw answered 22/7, 2019 at 11:59 Comment(0)
L
0

I only forget to Add the new Framework to my pod file :)

 target 'Data' do
inherit! :search_paths
 pod 'AlamofireObjectMapper', '~> 5.2'

end

Linguistician answered 25/4, 2020 at 19:7 Comment(0)
O
0

for Xcode 12.2

Create an Empty Swift file using Xcode, such as named Void.swift, and every issue will be solved by Xcode automatically.

Outfoot answered 27/11, 2020 at 9:37 Comment(0)
B
0

I ran into this issue with our project and the issue was that there were orphaned references to lottie in our xcode project file. Opening that in a text editor and removing them fixed the issue for me.

Balladeer answered 31/8, 2021 at 14:48 Comment(0)
C
-1

What solved it for me when trying to integrate Lottie in my React Native project was to uninstall the lottie-ios and lottie-react-native modules with npm.

npm uninstall lottie-react-native lottie-ios

Make sure the files are unlinked as well

npx react-native unlink lottie-react-native lottie-ios

I also had the bridging-file in my project from previously trying that method without success so in order to replicate my process, follow the suggested answer and add that file to your project as well.

After these steps, I went ahead and deleted my Pods folder, cleaned my build from xcproject and xcworkspace (maybe one of these is enough). After that I went ahead and installed the modules again

npm i --save lottie-react-native [email protected]

(Make sure to check the current docks over at the lottie repo for the up-to-date versions.)

After you have reinstalled the modules, cd into your ios folder and run pod install.

Open up your xcworkspace in Xcode and run your project (make sure your build is clean).

This solved my issue.

Best of luck.

Caprification answered 3/5, 2020 at 22:38 Comment(5)
what lottie-react-native version you installInsulation
Hello @AftabAmin , thanks for the downvote 😏. If you read my post you'll see I follow the documentation over at the lottie RN docs: github.com/lottie-react-native/lottie-react-native At the time of posting this a year ago I'm not too sure the version but give it a go with the latest stable release. Good luck!Caprification
I have tried all the updated version but non is working. when I remove these libraries it works fine. tow months later my app was running. today I was updating my app and suddenly got this issue. also faced this a month ago but then I solved it.Insulation
@AftabAmin I see, that's too bad. let the devs over at lottie RN know about this by opening a through issue perhaps. Might help more people than yourself. Too bad I can't be of anymore help. Been a while since I worked with lottie.Caprification
@AftabAmin, how you fix it??Arluene

© 2022 - 2024 — McMap. All rights reserved.