I'm interested in Swift programming, but I'd prefer not to use Xcode as it is too bloated for my practical use.
I know this is possible to do with Objective-C with something like
clang -fobjc-arc main.m myclass.m -o prog1
where the different .m files are just listed out, but I don't know how to do this with Swift. I can use the basic Swift command-line tool, like swift MyProgram.swift which just produces a binary called MyProgram.
As far as I can tell, this command won't let you compile multiple .swift files together.
Ideally, a system like makefiles for C++ would be ideal. Is there any way to do this with Swift currently from the command line?
While you can use swiftc, the recommended way after Swift 2.1 is open-sourced is to use swift build as described at Swift Package Manager documentation.
An important note from that document:
Please note, that the 2.2 release snapshot does not come with
swift-build-tool, either download the development snapshot or build
your own copy of swift-llbuild.
By developer snapshot they mean a developer snapshot installer from the official package or anything that's newer than Swift 2.2 at the moment.
I recommend swift build for convenience, if however you want to do it by hand still, then run swift build -v and you will see the commands that are run by swift build so you can construct your own command lines.
Related
Xcode supports custom Build Rules for different source files. I am particularly interested in Swift build rule.
What should be correct script to compile the sources and what to specify in Output Files?
Please, answer the question if you did have experience with that.
Swift files are compiled with swiftc implicitly, as well as Obj-C with clang. Afaik it's not possible to substitute that call with a Build Rule, as you also would need to pass tons of arguments for the optimizer, libraries for linker e.t.c. The output of source code compilation is object files (*.o), which are then linked together to form a resulting binary.
You can read about this process here and here.
So it used to be that from the command line, swift build --clean would clean the build by removing the .build directory. You could also pass --clean=dist to get rid of the packages as well.
I just upgraded to Swift 4 and these are apparently gone. Has it been rolled into a subcommand or something? Can anyone point me to some docs on this? I couldn't find them...
Actually, I couldn't find docs on the subcommands at all. I know swift run and swift package were introduced. Anyone know where a complete documentation of these and other commands are? I find bits and pieces described in various places, but haven't found the complete doc (that hopefully exists somewhere)
swift build --clean has been replaced with swift package clean in swift 4. We made that change here: https://bugs.swift.org/browse/SR-2082
Documentation for stuff like this should be at https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md, but it's lacking in some respects. We've just filed an issue to track that:
I also went ahead and filed:
Swift usage message should mention swiftpm commands
SwiftPM command usage messages should mention other top-level commands
Thanks for bringing this up!
I'm following this tutorial on using OpenGL in Swift. It apparently depends on some GitHub Repo for GLFW and uses a Package.swift file to build it into your executable. Sadly, while running the instructed build command swift build -Xcc -I/usr/local/include -Xlinker -L/usr/local/lib I get the following error:
error: Empty manifest file is not supported anymore. Use swift package init to autogenerate.
When I run the suggested init command, I get:
error: a manifest file already exists in this directory
What should I do?
That tutorial is written for Swift version 2, and it’s been outdated for some time. The only reason you need the library in that repository is because it defines some OpenGL function loaders necessary to make your program link properly. I currently use a patched version of that SGLOpenGL library that I hacked to make it compile under Swift 3. I believe this is the only way currently to use OpenGL with Swift. You will have to correct the Swift UnsafePointers and rewrite the function parameter lists to use (or remove) named arguments, among other things, as Swift 3 was a source-breaking release. The compiler will generally tell you what you have to do, it’s just a lot of tedious work.
Note that you don’t need any of Turnbull’s other libraries to use OpenGL with Swift. You can easily (“easily”) write your own implementations of the Math functions for the quaternions, vectors, matrices, camera frusta, etc. Google is your friend, as most of these routines are well-studied problems in computer graphics, and there is a great deal of pseudocode and C++ implementations on the web to help you get started.
The Image PNG library is non-trivial to replace, but luckily there already exists a native Swift PNG decoder MaxPNG† designed to work with graphics frameworks.
Good luck!!!
† (Disclosure, I am the author of MaxPNG; I wrote it because I was in the exact same position as you a few months ago.)
I'm exploring Swift with Xcode-6 and so far so good although I think the new collections need a little bit of work as I've managed to break the compiler a few times.
Problem is I'm now stuck trying to create the framework package to then use in another project. The project builds without issue and all tests pass successfully. When I go to create Archive (which I assume is what is required) I receive the error:
:0: error: underlying Objective-C module 'Sample' not found
Now I assume this has something to do with the contents of my Sample.h which tells me
// In this header, you should import all the public headers of your framework using statements like #import <Sample/PublicHeader.h>
which is fine except I have only used swift enums, structs and classes for this framework so therefore no .h files exist.
Can anyone shed some light on this one as I can't find any documentation available yet?
EDIT (7/27/2018)
The information in the answer below may no longer be accurate. Your mileage may vary.
I sat down with an engineer and asked this exact question. This was their response:
Xcode 6 does not support building distributable Swift frameworks at this time and that Apple likely won't build this functionality into Xcode until its advantageous for them to do so. Right now, Frameworks are meant to be embedded in your app's project, rather than distributed. If you watch any of the session videos or see the sample projects, this is always the arrangement. Another reason is that Swift is very new and likely to change. Thus your Swift-based framework is not guaranteed to work on the platform you originally built it on.
Slight Update
The engineer mentioned that it's not impossible to make a framework for distribution, just that it's not supported.
I was able to get past the error by going to the target for the framework and on the Build Phases tab under Headers, remove the MyFramework.h file
However I was getting the "Underlying Objective-C module not found" error when I was using a framework to share code between a containing app and an app extension, both of which were pure Swift.
Assuming you are creating a truly pure Swift module, you must disable the Objective-C Compatibility Header and any generated interface headers so the compiler doesn't go off assuming it can find an Objective-C module for the framework.
Do Not remove your public framework header. You'll get a module-map warning at link time if you do.
You might find this useful: Creation of pure swift module
In short: it's possible to make static framework, but there is one issue: it doesn't work in end user' project if "ProjectName-Swift.h" header included.
But, it works fine if you use only swift.
I think it's a bug in XCode 6, or that Apple does not allow archiving the Framework from XCode while in beta.
If you compile to profile XCode generates the framework correctly. (See the Release folder created in DerivedData)
I'm trying compile the lastest WebRTC version for iPhone. I not need to compile the entire solution, I only need to compile the VAD module.
To do that, I have created a Xcode project and I have tried to compile the source necessary, but I have a problem with the *.s files and its assembler.
Like in the FFMPEG library, I know that I must "translate" the assembler code to an assembler code that the gcc for iPhone understand, but I don't know how I do this manually.
I have tried to create a configure file and set in it "as=gas-preprocessor.pl" (like in FFMPEG), but does not work.
Any idea? How do I run the gas-preprocessor.pl manually?
Thanks.
I'm just finishing it on iOS and has built standalone static libraries of NS/VAD/AECM and AGC, here's some tips for you, and hope you success:
1. Source File List
for standalone VAD build, you should make sure your project has all of these files(no .s file needed), and I'm not listed the header files here, you will get some header file can not be found errors, just fixing it and things will be done.)
webrtc_vad.c
vad_core.c
vad_filterbank.c
vad_gmm.c
vad_sp.c
real_fft.c
division_operations.c
complex_bit_reverse.c
cross_correlation.c
complex_fft.c
downsample_fast.c
vector_scaling_operations.c
get_scaling_square.c
energy.c
min_max_operations.c
spl_init.c
2. Adding a macro called WEBRTC_MAC
I'm not sure why the Xcode environment does not provide this macro, but it should be defined to ensure that WEBRTC_POSIX is enabled. To define this macro, adding it to a new header file or just define it in the webrtc-header-files.
3. Following these steps to build and setup a static library of WebRTC-VAD module on iOS
notice, do not use LLVM 2.0 to compile the VAD module(use GCC or LLVM GCC). cus' it can throw you lots of errors when compiling some webrtc variable declarations.
4. Using the libwebrtc_vad.a
if you got this far, things are easy to go, just include webrtc_vad.h and using the API provided by this module. and vad is working fine in my case.
hope i helped.
try to check this link https://groups.google.com/forum/?fromgroups=#!topic/discuss-webrtc/VJg-fk2-i_0 i believe you have to set inline assembly correctly. I am also onto this so let me know if you want to switch emails or something.