It Gives Errors When Using Swift Static Library with Objective-C Project

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:

  1. Create workspace at top folder level

  2. Added mobile_sensor_API.xcodeproj to workspace

  3. Added sim_backend_UI.xcodeproj to workspace

  4. Added dependency of sim_backend_UI to lib via workspace

demo


  1. 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

demo2


  1. 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.

Sample Image

ObjC app build gets build error for call to swift function within ObjC static library

Solution from StackO user: Asperi


  1. Emptied top folder

  2. Created workspace at top folder
    1.1 Copied clean app and lib to top folder

  3. Add ObjC lib .xcodeproj to workspace using Xcode > File > Add files ...

  4. Add ObjC app .xcodeproj to workspace

  5. Added dependency of sim_backend_UI to lib via workspace
    a. app proj > General tab > Frameworks, Libs.. > +
    b. Select lib .a
    c. Add.

  6. 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{}


  1. Set Xcode active scheme to app and target device
  2. 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
settings

if its libc++ then use: lc++ in Other linker flags

else if its libstdc++ then use: lstdc++ in Other 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



Leave a reply



Submit