CharlesZhang
CharlesZhang

Reputation: 158

Clang on Windows fails to compile code

I built the LLVM and Clang (version 3.2) on Windows with the help of CMake and MinGW. The building is easy and successful. However, Clang failed to work with the sample code.

#include <stdarg.h>
#include <stdio.h>

int main()
{
    printf("BAD: %lld\n", 1);
    return 0;
}

When I compiled it with clang as

clang -o printf.exe printf.c -v

On Windows, it failed with messages



    clang version 3.2 (branches/release_32 172788)
    Target: i686-pc-mingw32
    Thread model: posix
     "D:/llvm/Build/bin/clang.exe" -cc1 -triple i686-pc-mingw32 -S -disable-free -main-file-name printf.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -mconstructor-aliases -target-cpu pentium4 -momit-leaf-frame-pointer -v -resource-dir "D:/llvm/Build/bin\\..\\lib\\clang\\3.2" -fmodule-cache-path "C:\\Users\\usrname\\AppData\\Local\\Temp\\clang-module-cache" -fno-dwarf-directory-asm -ferror-limit 19 -fmessage-length 140 -mstackrealign -fno-use-cxa-atexit -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -o C:/Users/usrname/AppData/Local/Temp/printf-976141.s -x c printf.c
    clang -cc1 version 3.2 based upon LLVM 3.2svn default target i686-pc-mingw32
    ignoring nonexistent directory "/usr/local/include"
    ignoring nonexistent directory "D:/llvm/Build/bin/../lib/clang/3.2/../../../i686-w64-mingw32/include"
    ignoring nonexistent directory "D:/llvm/Build/bin/../lib/clang/3.2/../../../x86_64-w64-mingw32/include"
    ignoring nonexistent directory "/mingw/include"
    ignoring nonexistent directory "/usr/include"
    #include "..." search starts here:
    #include  search starts here:
     D:/llvm/Build/bin/../lib/clang/3.2/include
     D:/llvm/Build/bin/../lib/clang/3.2/../../../include
     c:/mingw/include
    End of search list.
    printf.c:6:24: warning: format specifies type 'long long' but the argument has type 'int' [-Wformat]
            printf("BAD: %lld\n", 1);
                         ~~~~     ^
                         %d
    1 warning generated.
     "C:/MinGW/bin/gcc.exe" -v -c -m32 -o C:/Users/usrname/AppData/Local/Temp/printf-976142.o -x assembler C:/Users/usrname/AppData/Local/Temp/printf-976141.s
    Using built-in specs.
    COLLECT_GCC=C:/MinGW/bin/gcc.exe
    COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.6.1/lto-wrapper.exe
    Target: mingw32
    Configured with: ../gcc-4.6.1/configure --enable-languages=c,c++,fortran,objc,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgomp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --build=mingw32 --prefix=/mingw
    Thread model: win32
    gcc version 4.6.1 (GCC)
    COLLECT_GCC_OPTIONS='-v' '-c' '-m32' '-o' 'C:/Users/usrname/AppData/Local/Temp/printf-976142.o' '-mtune=i386' '-march=i386'
     c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/bin/as.exe -o C:/Users/usrname/AppData/Local/Temp/printf-976142.o C:/Users/usrname/AppData/Local/Temp/printf-976141.s
    C:/Users/usrname/AppData/Local/Temp/printf-976141.s: Assembler messages:
    C:/Users/usrname/AppData/Local/Temp/printf-976141.s:7: Error: bad expression
    C:/Users/usrname/AppData/Local/Temp/printf-976141.s:7: Error: junk at end of line, first unrecognized character is `\'
    clang: error: assembler (via gcc) command failed with exit code 1 (use -v to see invocation)

The generated temporary assemble file is:



        .def     _main;
        .scl    2;
        .type   32;
        .endef
        .text
        .globl  _main
        .align  16, 0x90, "\357\276\255\336"
    _main:

It seems that Clang on Windows generats a wrong format assemble file. But on Linux, it generats an obejct file directly instead of an assemble file and succeeds to compile.

How can I fix this problem? Thanks very much!

Upvotes: 6

Views: 5271

Answers (2)

Chawathe Vipul S
Chawathe Vipul S

Reputation: 1696

Observe the line of the failing invocation

"C:/MinGW/bin/gcc.exe" -v -c -m32 -o C:/Users/usrname/AppData/Local/Temp/printf-976142.o -x assembler C:/Users/usrname/AppData/Local/Temp/printf-976141.s

clang defaults to AT&T syntax and not Intel, if I read the docs correctly. For your version of gcc the arguments may require looking into for missing/incorrect ones. If running successful build has priority then alternatively try compiling to .o, then linking to be an exe with llvm's lld first, and then gnu's ld second.

Upvotes: 0

rubenvb
rubenvb

Reputation: 76519

There is something wrong with your installation.

Please find my Clang builds here: Clang package and required GCC package. As shown in the Clang package download directory, extract both to the same directory, and add "mingw32-dw2/bin" to PATH. I tested you code and it works.

Note your code has an error:

#include <stdarg.h>
#include <stdio.h>

int main()
{
    printf("BAD: %lld\n", 1LL); // NOTE the suffix specifying "long long"
    return 0;
}

Upvotes: 6

Related Questions