swift package manager link dynamic lib - swift

i used swift on linux, made the Package.swift file including some libraries, but when i built the project with swift build command, the libraries were static linked with the executable file. i tried to generated a Xcode project used the swift package generate-xcodeproj command, built the project in Xcode, then opened the Product directory, i found that the libraries were dynamic linked with the executable file, what did the Xcode doing? and how can i build it as a dynamic link use swift build command, could you help meπŸ˜”πŸ˜”

SwiftPM version 3 was only able to build executables with statically linked libraries. In SwiftPM version 4 (currently in beta, will be released this fall) you can also build with dynamically linked libraries. See SE-0146 for more information.

Related

Xcode 12, Workspaces and the new "XCFrameworks" not working together

I have a project, made up multiple smaller projects. I manage this mess generally through Carthage, but for development, I make use of a xcworkspace, to make it easier and faster to make changes, and Xcode to build the child and master projects via the workspace.
I've recently started updating all the projects to make use of the new XCFrameworks format (instead of the "fat frameworks" which Carthage/Xcode use to generate).
If I'm just working on the root project (xcproject) and linking the XCFrameworks directly, it all works fine. The moment I switch to the xcworkspace and replace any of the XCFrameworks with it's XCProject, the build fails
Multiple commands produce '/Users/.../Library/Developer/Xcode/DerivedData/gnuwolzggqlzhlfiipjzbigitmvu/Build/Products/Debug-iphoneos/....framework':
1) Command: ProcessXCFramework /Users/.../Development/.../.../Carthage/Build/....xcframework /Users/.../Library/Developer/Xcode/DerivedData/gnuwolzggqlzhlfiipjzbigitmvu/Build/Products/Debug-iphoneos/....framework ios
2) Command: ProcessXCFramework /Users/shanew/Development/.../Libraries/.../Carthage/Build/....xcframework /Users/.../Library/Developer/Xcode/DerivedData/gnuwolzggqlzhlfiipjzbigitmvu/Build/Products/Debug-iphoneos/....framework ios
The sub project frameworks DO NOT embedded their dependencies (as if I recall, this caused other issues) and dependency management is managed through Carthage, so the root project AND the child project will share dependencies, which I think is the cause of the issue.
I had some "minor" luck, if I built the sub project first and then remove its "Frameworks, Libraries, and Embedded Content", I could get the root project to work, but when I tried to do this with multiple sub projects I started to run into issues.
I did note that when I remove the xcframeworks and replace it with the project, it displays the project with the "fat framework" icon, instead of the xcframeworks icon ... I doubt this part of the issue, but thought I'd mention it...
So, the question...
How do we include "sub projects"/"frameworks", which have xcframeworks dependencies (shared with other projects) into a xcworkspace and have it build without triggering the "Multiple commands produce ... output" errors.
Not sure this totally maps to OP scenario, but here's what worked for us.
Initial, working setup with .framework:
Workspace
Framework project
Depends on 3rd-party framework ZZZ, through Carthage
Has its own Carthage cartfile and Carthage directory
Linked to ZZZ .framework binary from Carthage\Build
Application project
Depends on Framework, through Xcode target dependency
Depends on 3rd-party framework ZZZ, through Carthage
Has its own Carthage cartfile and Carthage directory
Embeds and linked to ZZZ .framework binary from Carthage\Build
Then we moved 3rd-party dependency ZZZ to XCFramework, still through Carthage, and the same setup was not working anymore. Our framework was building fine, but building our application failed because multiple commands where trying to produce ZZZ.
What worked for us, in the end, was to modify Application project and the way it includes the 3rd-party framework ZZZ. Instead of having its own Carthage-managed dependency, build directory, built binary, etc. for the 3rd-party dependency, the Application project embedded and linked the same binary taken from our Framework Carthage\Build directory.
So a setup like the following:
Workspace
Framework project
Depends on 3rd-party framework ZZZ, through Carthage
Has its own Carthage cartfile and Carthage directory
Linked to ZZZ XCFramework binary from Carthage\Build
Application project
Depends on Framework, through Xcode target dependency
Does not explictly depend on 3rd-party framework ZZZ, through Carthage
Has no own Carthage cartfile and Carthage directory
(or anyway the 3rd-party framework is not listed there)
Embeds and linked to ZZZ XCFramework binary from Framework\Carthage\Build
Hope that makes sense.
The story is different when an Application project depends on our Framework not through Xcode target dependency, but through the raw XCFramework file copied somewhere. In that case, Application project must again explicitly depend on 3rd-party framework ZZZ through Carthage, and embed/link the XCFramework binary from its own Carthage\Build directory.

Xcode does not generate DYSM for SPM dependencies

Xcode is not generating dSYM files for my three SPM dependencies.
I have tried creating a new Swift only project and including a SPM package like CocoaLumberJack and even there I am not given a dSYM for release.
I am using Xcode 12.0.1, I have looked through all kinds of build settings with no luck to force them to be generated or find where Xcode puts them if it is generating them.
Swift packages by default are treated as static library, that is why you don't see the embed options for them in Frameworks, Libraries, and Embedded Content section:
Static libraries don't require separate dSYM files are they don't have any executable. They are directly contained in consuming app executable so the static library symbols will be available in application's dSYM file. If you inspect you app's ipa you won't find the swift package module in Frameworks directory.
You can also make your swift package exposed product dynamic library by specifying library type as .dynamic:
.library(
name: "SwiftLibrary",
type: .dynamic,
targets: ["SwiftLibrary"]
),
This will make your package module dynamic library and will allow you to choose embed option in XCode:
If you make your package module dynamic and you can inspect your ipa to see this embedded in the Frameworks folder. After making your package module dynamic you separate dSYM file will be generated for it.

What's the difference between just open Package.swift & use `swift package generate-xcodeproj` then open the generated xcodeproj file?

I followed the Vapor Website Docs, created hello project.
VaporDocs
under hello project path, inputed swift build in terminal. But when open Package.swift, Xcode start to fetch vapor again.
Why? all dependencies repository is in .build path isn't? Why Xcode begains fetch Vapor over again?
swift package generate-xcodeproj can help generate xcode project, it seems like all dependencies is not package anymore. just groups.
What's the difference between just open Package.swift & use swift package generate-xcodeproj then open the generated xcodeproj file?
open Pacakge.swift with Xcode, fetch Vapor very slow. is there any better way?
like tell the Xcode everything is just in ./build path.
When you work with a project in terminal you use commands like
swift package update
swift build
swift run
swift package generate-xcodeproj
these commands works with hidden .build folder and Xcode project generated by swift package generate-xcodeproj works with that .build folder too. The only disadvantage of xcodeproj that if you change Package.swift or manually(outside of Xcode) add/delete some files from Sources folder then you have to run either just swift package generate-xcodeproj or swift package update && swift package generate-xcodeproj.
When you open project by double clicking on Package.swift it not uses classic xcodeproj anymore, now it is fully dynamic and you could edit Package.swift and files on-the-fly, doesn't matter in Xcode or in Finder, it will track all the changes. Though it sounds cool it works not perfectly and I still prefer classic xcodeproj cause it is not really hard to execute swift package generate-xcodeproj when needed. As far as I know there is no way to say Xcode to use .build folder in dynamic mode, it uses DerivedData folder to store dependencies.

How to update Swift dependencies in Xcode

After I create a new swift package with dependencies I call swift package generate-xcodeproj. If my dependencies have a new version I call swift package update. The new version of my dependency gets downloaded and built but Xcode now has a bad reference to the old version of the dependency. I can call swift package generate-xcodeproj again but this will remove any custom targets I have setup.
How do I update my swift dependencies and my Xcode project without deleting my custom targets?
I just stumbled upon this question and found that the solution to "How to update swift dependencies in Xcode" has probably changed now that Swift Packages have been around for a few years.
For me, the solutions was to simply go to File -> Swift Packages -> Update to Latest Package Versions.
Many of the problems with packages not updating are because the swift package version rules limit the automatic package updates to the current major version only, i.e v3.3.1 of a package will update to v3.4.0, but will not update automatically to v4.0.1. Therefore using the update options in Xcode does not necessarily get the latest major version of a package.
To resolve, Open the project from the project panel, select the project (not the targets), then select the "Swift Packages" tab. Double click on the package you want to update and change the minimum version to the next major version.
- Update a single dependency using Xcode
This method will save a lot of resources and time. Because it won't force other packages to redownload.
Double click on the package in the tab you mentioned and change the version to anything else. It will then recheck the remote repo. The benefit of doing this is to only update the selected package. (Also, it's better to have the current using version be set in the package.)
- Update ALL dependencies using Xcode
From File -> Swift Packages -> Update to Latest Package Versions
🀨 Single package with GUI (Xcode 12 and above)
Right-click on the package from the left navigation pan and select Update Package
note that you will see Xcode saying update all packages but it will update only the selected one
Instead of trying to preserve your changes to generated project, you can manage dependencies in a separate project, like CocoaPods does.
When starting new project:
create Xcode project for your app MyApp.xcodeproj
save as a workspace MyApp.xcworkspace
create package for your dependencies
mkdir MyDeps && cd MyDeps
swift package init --type library
add dependencies to Package.swift
generate Xcode project for the dependencies package
swift package generate-xcodeproj
add generated project MyDeps.xcodeproj to your workspace MyApp.xcworkspace
add target MyDeps.framework to Linked Frameworks of your app MyApp.xcodeproj
With this setup you can freely update dependencies in Package.swift and regenerate dependent project as needed.
I had an issue with CryptoSwift using Xcode 12.5. My version was 1.3.1 and the issue was fixed in 1.4.0.
I tried :
updating by File -> Swift Packages -> Update to Latest Package Versions.
xcodebuild -resolvePackageDependencies
But both did not work.
I manually went and changed target -> Swift packages -> Version rules
and Xcode automatically installed the newer version and I was out of my error.
If the library is used by a particular target, the library won't be updated to the latest version, at least in my case (Xcode 14.2).
I was using OneSignal which requires to add a target (OneSignalNotificationServiceExtension) which depends on the OneSignal library in the "Frameworks and libraries".
So:
remove the required lib voice from the dependency in the extension,
update the library with right click -> "update package"
re-add to the extension the updated library in the "Frameworks and libraries"
Screenshot of which row to delete to let the library update properly (remember to read later).

How to run cpp file in Xcode?

I am trying to run the code from below link which contains C++ codes.
https://github.com/cjcliffe/CubicVR
This link contains the BeatDetektor app for iPhone. But this is developed using C++. How can i run this project?? There are no xcode project file. Also i am trying to run main.cpp file but there are lots of package files error...
below is link for BeatDetektor app
http://www.cubicvr.org/index.php?option=com_content&view=article&id=67&Itemid=82
Can anyone tell me how to run C++ file in Xcode or on terminal???
Xcode is an IDE, not a compiler. It builds targets of various types by using a compiler like GCC internally. If there is no Xcode project file, you can create one yourself and add the source files to it. As far as I know Xcode will not compile source files on their own.
Using CMake software i am able to convert my cpp project to Xcode project... As github code contains .make extension file...