How to access pure Swift Static Library from pure Swift project?
I've passed your steps and all works, at least at Xcode 11.3.1 / iOS 13.3. The only thing... it seems... see comment
import StaticLib // << looks like you've forgot this one !!!
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
StaticLib.test("How do you do?") // error
return true
}
}
Update: Here are my steps -
- Click project in Xcode Project Navigator
- Click + to add new target
- Select Frameworks & Libraries > Static Library > Next
- Enter name
StaticLib
(in Swift) > Finish - Open StaticLib.swift and enter
public class StaticLib {
public class func printme() {
print("I'm swfit static lib!")
}
}
- Select
StaticLib
schema, Build > OK - Select project in Project Navigator > click main application target > select General
- In section Framework, Libraries, and Embedded Content click + and select
libStaticLib.a
> Add - Select main app schema > Build > OK
- Select AppDelegate.swift > add
import StaticLib
- Add anywhere in code
StaticLib.printme()
- Build > OK > Run ... see output
Update2: for external Lib project
+1 It is needed to copy StaticLib.swiftmodule
(it is created at the same place as libStaticLib.a) into target project folder (I placed it at the level of .xcodeproj
file)
+2 In main application target Build Settings
set SWIFT_INCLUDE_PATHS = ${SRCROOT}
+3 Clean > Build
Note: the indicator that module is loaded is autocompletion for import - it should show StaticLib
How to use my static library (.a) in swift 3
I think you have omitted a lot of information about what you did, which makes it difficult to provide an answer with certainty. Did you do something along the lines of https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md? What is your directory structure? Where is hello.h?
Anyway, judging from the error message, one problem is that you use
link "libhello"
in module.modulemap
. It is unclear what the name of the static library is. It cannot be called helloLib.a
, its name must start with lib
. If it is called libhelloLib.a
, then in the module map it must be
link "helloLib"
You may also want to add the -Xlinker -L/usr/local/lib
option as suggested in another answer.
Hope this helps.
Static Library and Swift
As of Xcode 9 beta 4, Xcode natively supports static libraries with Swift sources.
How to use Swift static library (.a) in C++ project?
Summary: this can be made to work (probably), but if this is intended to be used in production code, then the only correct approach is to consult the Swift documentation and, if it is still the case there is no official support, ask the Swift team how to approach the problem.
You should follow whatever documentation Swift has for exporting functions in the C ABI convention. It is doubtful doing it blindly works, and if it does, it is probably by chance and may stop working at any point in the future.
Sadly, there does not seem to be official support for such thing, at least according to questions like:
- Passing a Swift string to C
- What is the best way to call into Swift from C?
To make any kind of FFI to work unofficially, there are several things to take into account:
Figure out which calling convention Swift follows, including if it uses hidden parameters or things like that. The best case scenario is that Swift uses the usual one in your system.
Check what are the names of the actual symbols exported by Swift. Perhaps they are mangled.
Research if there are any semantics to hold that the other language runtime does automatically. For instance, if some code needs to be called before/after, or perhaps something need to be initialized, etc.
On the C++ side, you should write your declaration in an extern "C"
block so that the symbol is not expected to be C++-mangled:
extern "C" {
void StartWatcher();
}
There should be no need to declare it as a function pointer either.
Related Topics
How to Compress/Resize Image on iOS Before Uploading to a Server
How to Implement Re-Ordering of Coredata Records
Reason: No Suitable Image Found
Setting Custom Uitableviewcells Height
iOS 7 Tableview Like in Settings App on iPad
Swift Native Functions to Have Numbers as Hex Strings
Uicollectionview Flowlayout Not Wrapping Cells Correctly
Display Uiviewcontroller as Popup in Iphone
Layout Issues After Updating to Xcode 8
Interface Builder: What Are the Uiview's Layout iOS 6/7 Deltas For
iPhone Sdk: Differencebetween Loadview and Viewdidload
Displaying Splash Screen for Longer Than Default Seconds
Com.Apple.Webkit.Webcontent Drops 113 Error: Could Not Find Specified Service
Swift 3.1 Deprecates Initialize(). How to Achieve the Same Thing
Core Data and iOS 7: Different Behavior of Persistent Store
Uibutton: Making the Hit Area Larger Than the Default Hit Area