Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 958
CharlesZhang Avatar asked Jan 27 '13 18:01

CharlesZhang


People also ask

Can Clang compile for Windows?

For best IDE support in Visual Studio, we recommend using the latest Clang compiler tools for Windows. If you don't already have the tools, you can install them by opening the Visual Studio Installer and choosing C++ Clang tools for Windows under Desktop development with C++ optional components.

How do you compile in Clang?

To compile a C++ program on the command line, run the clang++ compiler as follows: $ scl enable llvm-toolset-6.0 'clang++ -o output_file source_file ...' This creates a binary file named output_file in the current working directory. If the -o option is omitted, the clang++ compiler creates a file named a.

Can I use Clang instead of GCC?

You can use Clang instead of GCC as a compiler for any package by overriding stdenv , which contains the compilation toolchain, with: stdenv = pkgs.

Can Clang ++ compile C?

The Clang project provides a language front-end and tooling infrastructure for languages in the C language family (C, C++, Objective C/C++, OpenCL, CUDA, and RenderScript) for the LLVM project. Both a GCC-compatible compiler driver (clang) and an MSVC-compatible compiler driver (clang-cl.exe) are provided.


1 Answers

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;
}
like image 167
rubenvb Avatar answered Sep 22 '22 07:09

rubenvb