How to produce an object file from an assembly file?


I have an assembly file test.s

shell # swiftc -emit-assembly test.swift -o test.s

I got an error while trying to produce an object file

shell # swiftc test.s -emit-object -o test.o

:0: error: unexpected input file: test.s


Nikolai Kinash

That's not supported at the moment. You could compile the individual swift files to object files, but I'm guessing that's not what you're looking for.

Does swiftc's -emit-assembly option not produce an assembly file that could be compiled by clang?

Both Clang and Swift should be using the ~same assembler, so I don't think it should matter.

$ cat main.swift
$ xcrun swiftc main.swift && ./main
$ xcrun swiftc main.swift -emit-assembly -o main.S && xcrun clang++ main.S -c -o main.o && xcrun swiftc main.o -emit-executable && ./main

It does seem to work, but I don't know if this will always work or if there are situations where it may fall over.

1 Like

But, you had to use clang/clang++ to generate the actual object file. I think the OP thought he could use swiftc for the whole toolchain.

I already answered OP's question in my first comment; you can ask swiftc to go from source code → object code, or source code → assembly, but assembly → object code is not supported.

My previous comment was meant as an answer to @.Jumhyn's question, not to OP's question.


Unfortunately it doesn't always works.

file 4.bc
4.bc: LLVM bitcode, wrapper arm

/Applications/ -frontend -emit-assembly -target armv7-apple-ios10.0 -O -disable-llvm-optzns -module-name SwiftTester 4.bc -o 4.s

/Applications/ -cc1as -triple armv7-apple-ios10.0  -o 4.o 4.s
4.s:1307:17: error: non-local symbol required in directive
        .no_dead_strip  "L___ir_hidden#98_"


/Applications/ -frontend -emit-object -target armv7-apple-ios10.0 4.s -o 4.o
<unknown>:0: error: unable to load standard library for target 'armv7-apple-ios10.0'

But anyway thanks everybody for the answers

Terms of Service

Privacy Policy

Cookie Policy