Author Topic: LLVM/Clang compiler for MinGW  (Read 52343 times)

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9702
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline golgepapaz

  • Multiple posting newcomer
  • *
  • Posts: 44
Re: LLVM/Clang compiler for MinGW
« Reply #1 on: January 23, 2013, 02:08:17 am »
You can build it yourself. configure or cmake based builds works out of the box..
I do have two modifications to the source code, one pertaining to clang unable to find
mingw header files (4.7.2, 4.7.3 etc) and one about calling convention if you happen upon
some problems..

Interestingly I'd come to report nullptr causing problems with clang because definition in
prep.h does not consider clang.
Code
#if !(__GNUC__ == 4 && __GNUC_MINOR__ >= 6 && defined __GXX_EXPERIMENTAL_CXX0X__)
    // it is a const object...
    const class nullptr_t
    {
    public:
        // constructor
        nullptr_t() {}
        // convertible to any type of null non-member pointer...
        template<typename T> operator T* () const{ return (T*)0; }
        // or any type of null member pointer...
        template<typename C, typename T> operator T C::* () const { return (T C::*)0; }
        // support operator overloading (== and !=)
        template<typename T> bool equals(T const& rhs) const { return rhs == 0; }
    private:
        // can't take address of nullptr
        void operator&() const;
        // can't copyable
        nullptr_t(const nullptr_t&);
        const nullptr_t& operator=(const nullptr_t&);
    } nullptr_;
    #define nullptr nullptr_
#endif
it needs also something like
#if !(__has_feature(cxx_nullptr))

Clang does not use something like GXX_EXPERIMENTAL_CXX0X or version numbers but instead opts for
__has_feature macro to query if certain language features are enabled.

http://clang.llvm.org/docs/LanguageExtensions.html#checking_upcoming_features

Offline vwdvaan

  • Multiple posting newcomer
  • *
  • Posts: 15
Re: LLVM/Clang compiler for MinGW
« Reply #2 on: May 01, 2013, 10:47:22 am »
Win 10, MinGW 122.0, wxWidgets 3.1.7

Offline eranif

  • Regular
  • ***
  • Posts: 256
Re: LLVM/Clang compiler for MinGW
« Reply #3 on: May 01, 2013, 12:57:59 pm »
Quote
I am looking for a LLVM/Clang compiler suite that works under Win32.
These I tried, but they all fail with crashes, missing libraries or alike:
I have it self compiled on my machine with TDM-GCC-4.7.1. i.e. in order for it to work you also need to install TDM-GCC-4.7.1

HOWEVER, (and this is a big _HOWEVER_): gdb (Windows only) can not debug binaries created with clang under Windows
I opened a bug for this here:

http://llvm.org/bugs/show_bug.cgi?id=13636

There is patch for this bug which I am not sure it was applied...( I will test with latest trunk tonight):
http://lists.cs.uiuc.edu/pipermail/llvmdev/2012-November/056009.html

Drop me a message if you want me to upload my compiled version

Eran

Offline xunxun

  • Almost regular
  • **
  • Posts: 187
Re: LLVM/Clang compiler for MinGW
« Reply #4 on: May 01, 2013, 05:16:31 pm »

There is patch for this bug which I am not sure it was applied...( I will test with latest trunk tonight):
http://lists.cs.uiuc.edu/pipermail/llvmdev/2012-November/056009.html


Is the patch effective for debugging after your test?
Regards,
xunxun

Offline eranif

  • Regular
  • ***
  • Posts: 256
Re: LLVM/Clang compiler for MinGW
« Reply #5 on: May 02, 2013, 01:40:30 pm »
Hi xunxun,
I did not apply the patch locally...so I can't really answer your question
I settled for opening a ticket to the clang devs
Eran

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9702
Re: LLVM/Clang compiler for MinGW
« Reply #6 on: May 02, 2013, 03:33:21 pm »
Drop me a message if you want me to upload my compiled version
Drop. :-)

Maybe there is already a 3.2 version? I also read the current dev-version supports c++11 completely - that would be interesting, too.

The thing with gdb is bad, indeed. :-/
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline eranif

  • Regular
  • ***
  • Posts: 256
Re: LLVM/Clang compiler for MinGW
« Reply #7 on: May 02, 2013, 07:17:18 pm »
Updating clang/llvm to their latest trunk and building
I will also check if gdb is working with latest trunk.. (thought I doubt it will work...)

Eran

Offline eranif

  • Regular
  • ***
  • Posts: 256
Re: LLVM/Clang compiler for MinGW
« Reply #8 on: May 03, 2013, 06:01:14 am »
Hi,

I built clang latest trunk + I did some minor change to allow debugging - with the my build you can compile and debug a C program.
For C++: Its not usable.

- Exceptions are not supported (you need to compile your code with -fno-exceptions)
- You need to explicitly set -lstd++ in the linker
- After you successfully link, your program will crash ;) ( in debug and release mode with no optimizations ) with non helpful backtrace. Note that the crash happens with or without my change

To summarize:
- Is usable for C
- Don't bother with it for C++ / Windows

Linux is another story:
- Works like a charm (I even got a separate codelite build which compiles with clang)
- Its x2 faster than g++ on linux (for comparison, compiling codelite ~1200 sources with PCH enabled takes about 3 mins on my 8 core machine with g++ and ~1.3 min with clang++)
- A drop in replacement for g++/gcc (just set CXX=clang++ / CC=clang and you are done)

Eran

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5514
Re: LLVM/Clang compiler for MinGW
« Reply #9 on: May 03, 2013, 07:39:19 am »
how do you build it on linux ? I know it is in the repos, but I think their new libstd++ is missing for linux, and that one should be build , right ?
And does the C++11 stuff work ?

Offline eranif

  • Regular
  • ***
  • Posts: 256
Re: LLVM/Clang compiler for MinGW
« Reply #10 on: May 03, 2013, 08:08:37 am »
Building on Linux is quite straight forward:

http://codelite.org/Developers/HomePage#toc2

Once you have completed the above steps - just run
Code
sudo make install

To build their libc++ (this is what I am using to build it on my linux box):

Code
svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx
cd libcxx
mkdir build-release
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release ..
make -j8
sudo make install

You can get more details here:
http://libcxx.llvm.org/

Eran

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9702
Re: LLVM/Clang compiler for MinGW
« Reply #11 on: May 03, 2013, 10:11:53 am »
For C++: Its not usable.
So: Am I getting it right that I am still lost on Windows? :-(
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline eranif

  • Regular
  • ***
  • Posts: 256
Re: LLVM/Clang compiler for MinGW
« Reply #12 on: May 03, 2013, 10:16:04 am »
Yes you are ;)

Maybe building clang with Visual Studio will help - but you mentioned that you wanted a MinGW version...


Eran



Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9702
Re: LLVM/Clang compiler for MinGW
« Reply #13 on: May 03, 2013, 10:29:55 am »
Maybe building clang with Visual Studio will help - but you mentioned that you wanted a MinGW version...
Yes, that's explicitely what I am looking for. The VS version I use works flawlessly. I am using the one from here:
http://www.ishani.org/web/articles/code/clang-win32/

BTW:
i686-w64-mingw32-clang-3.1-release-win32_rubenvb.7z with i686-w64-mingw32-gcc-dw2-4.6.3-2-release-win32_rubenvb.7z
Create a dir LLVM and copy in that dir.
...does not work, too. I can compile a simple main doing nothing, but as soon as you start to use C++ it fails miserably as all others.

Nevermind... seems there is no prime time yet... But thanks anyways...
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: LLVM/Clang compiler for MinGW
« Reply #14 on: May 03, 2013, 04:00:17 pm »
...does not work, too. I can compile a simple main doing nothing, but as soon as you start to use C++ it fails miserably as all others.
Some time ago, I built it with MinGW and got C++ to work.  There are some hard-coded search paths for STL headers that I had to modify first (sorry, I cannot recall which file they were in).

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5514
Re: LLVM/Clang compiler for MinGW
« Reply #15 on: May 04, 2013, 07:40:35 pm »
on your instructions for building libcxx :
Code
mkdir build-release
cd  build-release  <======================
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release ..

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5514
Re: LLVM/Clang compiler for MinGW
« Reply #16 on: May 04, 2013, 07:59:19 pm »
build and installed clang and libc++ with your instructions, then let CB autodetect it, it finds it in /usr/local/clang.

But when trying a "hello world" , it fails to compile since it doesn't ind the headers (iostream).
What should be the correct location ?
Timestamps wise I think this was a result of the build too : /usr/local/include/c++/v1/ ? => when using this , compiles but now linker problems (eg . :  main.cpp:4: undefined reference to `std::__1::cout')

Also my linux shell doesn't seem to know  about clang (yet), though I did ldconfig, and /usr/local/bin is already in my path (this is also where codeblocks executable is).

EDIT : on the linking problem : added to Cb for clang compiler settings :
linker path : /usr/local/lib
link lib : c++

==> links but at run time :  error while loading shared libraries: libc++.so.1: cannot open shared object file: No such file or directory
« Last Edit: May 04, 2013, 08:13:07 pm by killerbot »

Offline eranif

  • Regular
  • ***
  • Posts: 256
Re: LLVM/Clang compiler for MinGW
« Reply #17 on: May 05, 2013, 07:27:57 am »
I forgot to mention that you will need libc++abi as well.., building it on linux does not work "out-of-the-box" but requires a small tweak:

First, checkout libcxxabi:

Code
 svn co http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi

Next:
Code
cd libcxxabi/lib

Now you need to add some include paths... otherwise it will not compile:
run:
Code
 g++ -v  -x c++ /dev/null -fsyntax-only

Locate the output line which is similar to this:
Code
/usr/lib/gcc/x86_64-linux-gnu/4.7/include

Adjust CXX environment variable so it will be something similar to this:
Code
export CXX='clang++ -I/usr/local/include/c++/v1 -I/usr/lib/gcc/x86_64-linux-gnu/4.7/include'
and compile libcxxabi by simply typing:
Code
./buildit

once the compilation is done, you should have a new lib: libc++abi.so.1.0
I could not find a simple way of installing so I copied it manually to /usr/local/lib and created 2 symlinks:

Code
ln -sf /usr/local/lib/libc++abi.so.1.0 /usr/local/lib/libc++abi.so
ln -sf /usr/local/lib/libc++abi.so.1.0 /usr/local/lib/libc++abi.so.1

Now compiling with clang++/libc++ should be like this:

Code
clang++ -stdlib=libc++ -c test.cpp -I/usr/local/include/c++/v1
clang++ -stdlib=libc++ test.o -o TestMe -lc++ -lc++abi -I/usr/local/include/c++/v1/

Eran

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5514
Re: LLVM/Clang compiler for MinGW
« Reply #18 on: May 05, 2013, 09:49:39 am »
Thanks Eran, but not yet ok for me :

Code
error while loading shared libraries: libc++abi.so.1: cannot open shared object file: No such file or directory

Offline eranif

  • Regular
  • ***
  • Posts: 256
Re: LLVM/Clang compiler for MinGW
« Reply #19 on: May 05, 2013, 09:52:10 am »
Did you remember to add /usr/local/lib to LD_LIBRARY_PATH?

Code
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

In addition, note that you should create 2 symlinks

Code
ln -sf /usr/local/lib/libc++abi.so.1.0 /usr/local/lib/libc++abi.so
ln -sf /usr/local/lib/libc++abi.so.1.0 /usr/local/lib/libc++abi.so.1

Eran

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5514
Re: LLVM/Clang compiler for MinGW
« Reply #20 on: May 05, 2013, 10:45:20 am »
I did not do the export (export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH) , but it is in my normal path, is that not sufficient ?
And in the compiler settings of clang in CB, I even added that path as the dirs where the linker can go find libs.

I did create the 2 symlinks.

Offline eranif

  • Regular
  • ***
  • Posts: 256
Re: LLVM/Clang compiler for MinGW
« Reply #21 on: May 05, 2013, 11:16:51 am »
Quote
but it is in my normal path, is that not sufficient
Do you mean its in your PATH environment variable? LD_LIBRARY_PATH is for locating libraries during runtime
PATH is used to locate binaries (unlike Windows...)

Quote
I even added that path as the dirs where the linker can go find libs
And from what I understood the linker did _find_ it. Its just that the runtime linker did not find them (it needs LD_LIBRARY_PATH)

Eran

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5514
Re: LLVM/Clang compiler for MinGW
« Reply #22 on: May 05, 2013, 12:14:03 pm »
yes, it is not in my PATH ;-) /usr/local/bin is

In a console I did :
Code
killerbot@XIII:~/Projects/TryOUt/ClangC11/Deliv/Debug> ./ClangC11 
./ClangC11: error while loading shared libraries: libc++abi.so.1: cannot open shared object file: No such file or directory
killerbot@XIII:~/Projects/TryOUt/ClangC11/Deliv/Debug> export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
killerbot@XIII:~/Projects/TryOUt/ClangC11/Deliv/Debug> ./ClangC11
Segmentation fault

for a simple
Code
#include <iostream>

int main()
{
    std::cout << "Hello world!" << std::endl;
    return 0;
}

In /usr/local/lin I also have the boost libs from my distro. I think these can be found by programs that need it ?

In /usr/local/lib64, the codeblocks so's end up. And when CB is launched (which is located in /usr/local/bin) that one is however able to find it's so files, while that path is also not in $LD_LIBRARY_PATH

for me it is :

Code
echo $LD_LIBRARY_PATH
/usr/lib64/mpi/gcc/openmpi/lib64

So it seems there might be a few things going wrong, explicit path specification is bneeded, while other libs in there or in lib64 can be found, and in the end crash

Offline eranif

  • Regular
  • ***
  • Posts: 256
Re: LLVM/Clang compiler for MinGW
« Reply #23 on: May 05, 2013, 01:15:53 pm »
BTW, which distro do you use?

I am using Ubuntu 12.10/64
and it is running without a problem...

Can you post the compiler output and the linker ?


Eran

Offline osdt

  • Multiple posting newcomer
  • *
  • Posts: 63
Re: LLVM/Clang compiler for MinGW
« Reply #24 on: May 05, 2013, 04:21:53 pm »
build and installed clang and libc++ with your instructions, then let CB autodetect it, it finds it in /usr/local/clang.
...
Also my linux shell doesn't seem to know  about clang (yet), though I did ldconfig, and /usr/local/bin is already in my path (this is also where codeblocks executable is).
It's probably not installed in /usr/local. Try:
Code
find /usr/local -name clang++

EDIT : on the linking problem : added to Cb for clang compiler settings :
linker path : /usr/local/lib
link lib : c++
clang++ searches it's install-path/lib to find default libs.

==> links but at run time :  error while loading shared libraries: libc++.so.1: cannot open shared object file: No such file or directory
A single call to ldconfig [/usr/local/lib] shoud do it. Check if '/usr/local/lib' is in the default lib-path:
Code
grep 'usr/local' /etc/ld.so.conf

- osdt


Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7256
Re: LLVM/Clang compiler for MinGW
« Reply #25 on: May 05, 2013, 04:23:49 pm »
I can compile C::B with clang (except for thre contrib-plugins, LibFinder, Profiler and NassiShneiderman) without any tweaking or self-compiling of clang on Fedora 18.
And it seems to run (at least it starts without issues.
C::B from inside C::B.
Compiler is detected as gcc 4.1.2 on starthere-page.

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: LLVM/Clang compiler for MinGW
« Reply #26 on: May 05, 2013, 05:38:53 pm »
If you do not have a particular reason to self-compile Clang, installing it through apt-get works fine.

I can compile C::B with clang (except for thre contrib-plugins, LibFinder, Profiler and NassiShneiderman) without any tweaking or self-compiling of clang on Fedora 18.
And it seems to run (at least it starts without issues.
C::B from inside C::B.
Also works in Ubuntu 12.04 and 13.04.

Offline eranif

  • Regular
  • ***
  • Posts: 256
Re: LLVM/Clang compiler for MinGW
« Reply #27 on: May 05, 2013, 07:39:54 pm »
Quote
If you do not have a particular reason to self-compile Clang, installing it through apt-get works fine
I think that the problem here is not compiling clang (from what I understand killerbot we are beyond it)
We are trying to make clang to use a different std++ library named "libc++" which on linux also requires libc++abi
both needs to be self compiled (libc++abi build instructions are only available for OSX and not for Linux not to mention Windows...)

Eran

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5514
Re: LLVM/Clang compiler for MinGW
« Reply #28 on: May 06, 2013, 08:00:32 pm »
all paths are in :

Code
find /usr/local -name clang++
/usr/local/bin/clang++

Code
grep 'usr/local' /etc/ld.so.conf
/usr/local/lib64
/usr/local/lib

so why does it not find the .so's

Offline osdt

  • Multiple posting newcomer
  • *
  • Posts: 63
Re: LLVM/Clang compiler for MinGW
« Reply #29 on: May 07, 2013, 01:07:36 am »
Code
grep 'usr/local' /etc/ld.so.conf
/usr/local/lib64
/usr/local/lib

so why does it not find the .so's

Hmm ... did you run ldconfig after installation of libc++ successfully? Try ...
Code
# run ldconfig
sudo /sbin/ldconfig
# check if libc++ has been found
sudo /sbin/ldconfig -p | grep 'libc++'

- osdt

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5514
Re: LLVM/Clang compiler for MinGW
« Reply #30 on: May 07, 2013, 07:40:28 am »
one step further, I indeed forgot to rerun ldconfig, after I had build the libc++abi !

So now it links, and runs from within CB, and shell without LD_LIBRARY_PATH execution.

Well the "runs" ==> crashes

According to the debugger : in libcxxabi/src/cxa_exception.cpp

Code
LIBCXXABI_NORETURN
void
__cxa_throw(void* thrown_object, std::type_info* tinfo, void (*dest)(void*))
{  /// <====================================== on this line stack ponter

during the call of :     std::cout << "Hello world!" << std::endl;


just having a main that does return 0 ==> that doesn't crash