Search code examples
assemblygcc

Is there alternative way of expressing .s files in gcc?


I realize this may get rejected by Stack Overflow, but:

I use a compiler that generates .s files to be assembled by gcc. I mix in C files as well, so the final gcc command line contains a mix of .s and .c files. Because .s files are just an intermediate file to me, I want to erase all of them in a make clean. Thus the problem. There are .s files, assembly files, that contain hand written code and other .s files that are generated. It would be an ideal solution if (say), .asm were a proper alternative to .s files, that way I could have the assembly files I want to keep as .asm files, and .s files are junk that can be removed. Is there another alternative to .s files? (the extension)?

A search turned up .S and .sx extensions, but each of those has special meanings. I (ideally) want a proper alias to .s files.

I actually tried .asm as an extension, I got:

$ gcc test.asm -o test
/usr/bin/ld:test.asm: file format not recognized; treating as linker script
/usr/bin/ld:test.asm:1: syntax error
collect2: error: ld returned 1 exit status

From Mike below I got:

gcc test1.c -x assembler test.asm -o test

Which works, and allows mixed C and assembly source.


Solution

  • Yes, you can keep the assembly files as e.g. file.asm and just tell gcc to treat them as assembly source:

    gcc -c -x assembler -o file.o file.asm
    

    Or tell your build system to tell gcc to do that.

    See the GCC Manual, 3.2 Options Controlling the Kind of Output:

    -x language
    Specify explicitly the language for the following input files 
    (rather than letting the compiler choose a default based on the 
    file name suffix). This option applies to all following input files 
    until the next -x option. Possible values for language are:
    
        c  c-header  cpp-output
        c++  c++-header  c++-system-header c++-user-header c++-cpp-output
        objective-c  objective-c-header  objective-c-cpp-output
        objective-c++ objective-c++-header objective-c++-cpp-output
        assembler  assembler-with-cpp
        ada
        d
        f77  f77-cpp-input f95  f95-cpp-input
        go
    

    You would want -x assembler-with-cpp if your assembly sources contain pre-processing directives.

    And if you want to turn off -x <lang> further down the commandline and resume business as usual:

    -x none
    Turn off any specification of a language, so that subsequent 
    files are handled according to their file name suffixes 
    (as they are if -x has not been used at all).