swift build errors from Objective-C app due to import of Objective-C static library that contains a .swift file
The simplest solution is to allow Xcode to make all dependencies and include all required system libs.
Here is what I did:
Create workspace at top folder level
Added mobile_sensor_API.xcodeproj to workspace
Added sim_backend_UI.xcodeproj to workspace
Added dependency of sim_backend_UI to lib via workspace
- Add
Some.swift
(any swift file you want) to sim_backend_UI, just for the purpose Xcode add required system swift dynamic libraries (which will be needed for swift part in static library as well)... and confirm creating bridge in appeared dialog
- Build >>> Succeeded!
UPDATE 3 .................................. AFTER I DID THIS ANSWER thru step 6, I got "succeeded" but sim_backend_UI.app is RED and won't run on iphone.
ObjC app build gets build error for call to swift function within ObjC static library
Solution from StackO user: Asperi
Emptied top folder
Created workspace at top folder
1.1 Copied clean app and lib to top folderAdd ObjC lib .xcodeproj to workspace using Xcode > File > Add files ...
Add ObjC app .xcodeproj to workspace
Added dependency of sim_backend_UI to lib via workspace
a. app proj > General tab > Frameworks, Libs.. > +
b. Select lib .a
c. Add.Add Some.swift (any swift file you want) to sim_backend_UI, just for the purpose Xcode add required system swift dynamic libraries (which will be needed for swift part in static library as well)... and confirm creating bridge in appeared dialog
a. new file > Swift > Some.swift
b. Create Bridging Header
c. Added to Some.swift ...
import Foundation
struct Some{}
- Set Xcode active scheme to app and target device
- Build
"succeeded"
Swift Static Library based on C++ sources: linker command failed error: ld: symbol(s) not found for architecture x86_64 clang
TLDR: Use linker flag: -lc++ or -lstdc++
// You can skip to the "The Endgame" section below
Initial Workaround
Background
While going through lot of forums for solution I came across someone's thoughts:
NOTE: This text is quoted from a thread of similar question (as mine), but the difference being: Importing Swift Static Library in ObjC Project
This is because the main target (app) is trying to build solely against Objective-C and isn't told by the static library that it needs to include Swift libraries. This was because there weren't any Swift files in the Compile Sources section of our Build Phases for the app target.
So basically all you have to do is add at least one .swift file to
that compile list and it will include the Swift libraries for you. It
doesn't even need to have any code or values in it, it can be an empty
file.
The workaround:
So considering same logic, I thought, my client App Project, which Swift based:
How will it know that static library
libFooCppBasedSwiftLibrary.a
has used or needs (not sure what's correct word here) standard C++ libraries, like for eg:std::
So what is did to solve this is:
- In my client app project clicked on root/any folder
"New File..."
-> Select"C++ File"
-> Set any file name -> Check"Also create a header file"
->Next
->Create
-> You will get Prompt saying "Would you like to configure an Objective-C bridging header?", click on"Create Bridging Header"
button
And you are DONE, now build you app it should be working fine!
This is a workaround at best, but not sure if there is any Build Setting that enforces this rahter than adding empty C++ file.
The Endgame:
After some more digging I found perfect solution to this
Firstly, ignore/undo that inital workaround, we don't need it any more
Go to to client app project's build settings (For app target) and in Other linker flags
add -lc++
or -lstdc++
and build the app, done!
What to chooese
-lc++
or-lstdc++
?Check
"C++ standard library"
/CLANG_CXX_LIBRARY
setting in build
settingsif its
libc++
then use:lc++
inOther linker flags
else if its
libstdc++
then use:lstdc++
inOther linker flags
I'm assuming this is explicitly telling linker to properly link .a binary of static library with given Standard C++ library
Related Topics
Removing Object from Array in Swift 3
How to Dynamically Format a Number to Have Commas in a Uitextfield Entry
iOS 10 Rich Media Push Notification (Media Attachment) in Objective-C
Intercept Request with Wkwebview
Custom View Which Looks Like Uialertview
How to Make Uipageviewcontroller Reuse Controller Like Tableview Reuse Cell
Converting Uiimageview Touch Coordinates to Uiimage Coordinates
Becomefirstresponder Not Working in iOS 8
Core Data Store Included in App Bundle
What the Meaning of Question Mark '' in Swift
How to Tell If Blocks in Loop All Have Completed Executing
Are There Any Analogues of [Nsstring Stringwithformat:] for Nsattributedstring
iPad iOS7 - Uiimagepickercontroller in Uipopovercontroller Has Wrong Preview Image
iOS - Uisplitviewcontroller with Storyboard - Multiple Master Views and Multiple Detail Views
Why Are Animations on Bounds of an Uilabel Only Working When Increasing the Size