30

I installed clang from scratch following the instructions here. Afterwards, I installed libc++ using libsupc++ according to the instructions here.

Now, whenever I compile & link a program with clang and libc++, I need to issue a command like that:

clang++ -stdlib=libc++ -Wl,-rpath,/path/to/libcxx/lib <...>

Is there a way to configure/compile clang in a way that it uses libc++ by default, without me having to specify the library and/or the path on the command line each time? Putting it into LD_LIBRARY_PATH is not a preferred option either, neither is using a custom wrapper script.

  • clang doesn't have a configure option for this? – Ignacio Vazquez-Abrams Dec 20 '13 at 15:54
  • Not one that I know of. Usually libcxx is built with the new clang version, so in that case the library is not even there yet. But I would settle for an answer that works with a pre-existing libcxx library. – Michael Schlottke-Lakemper Dec 20 '13 at 15:55
  • 4
    How about a bash alias? And what's wrong with LD_LIBRARY_PATH? If you don't want that, just install libc++ to /usr/lib. – rubenvb Jul 02 '15 at 21:06
  • I am looking at a network setup with multiple users on multiple hosts, where clang/libcxx are installed on an NFS share. Setting `LD_LIBRARY_PATH` for all users is not feasible, installing to `/usr/lib` is not possible. – Michael Schlottke-Lakemper Jul 03 '15 at 12:58
  • 3
    Wouldn't this be a good use case for a Makefile, where you specify the flags inside the makefile so you can just use the make command next time? – Maurice Jul 06 '15 at 14:57
  • For a single project, yes. However, I'm looking for a generic solution that works also for ad-hoc compilations of small test programs from the command line. – Michael Schlottke-Lakemper Jul 06 '15 at 15:11

2 Answers2

17

Clang's CMake build system learned the CLANG_DEFAULT_CXX_STDLIB to set the default C++ standard library.

However, I don't know how practicable this solution is for you, because you have to use a top of tree build until next clang/llvm release.

Michael Haidl
  • 5,384
  • 25
  • 43
2

There are three ways I can think to do it. The first is for a single project using Unix makefiles, the second will serve as many projects as you want, but requires editing an arbitrary number of files to serve an arbitrary number of users, and the third will work for any number of projects or users. You probably want to skip to the third option, but the rest are there for anyone else with somewhat similar needs.

  1. A good way to do this is to use a makefile. This will enable you to build your project by simply typing make. If you're using *nix this shouldn't require an install, most systems come with it. Here's a sample makefile that does what you're requesting (just replace <progname> with your program name and <filename> with the source file name). Just paste this into a file named 'makefile' in the same dir as your source file.

    FLAGS=-stdlib=libc++ -Wl,-rpath,/path/to/libcxx/lib
    
    all: <progname>
    
    progname: 
        clang++ $FLAGS progname
    

    Disclaimer: I don't use clang++, so this may be an incomplete invocation. In gcc, you would also need to specify -o outfile_name, for example.

  2. Alternatively (since I just read the comments), you could run the following command (assuming you use bash):

    echo 'alias stdclang="clang++ -stdlib=libc++ -Wl,-rpath,/path/to/libcxx/lib"' >> ~/.bashrc
    

    and from then on you could build with the libc++ library linked by just typing stdclang <progname>

  3. One last thing I could think of is similar to the last one, but with a more permanent twist. As root, run the following: touch /usr/bin/stdclang && chmod a+x /usr/bin/stdclang then edit the file /usr/bin/stdclang with whatever editor you want and add these lines:

    #!/bin/bash
    clang++ -stdlib=libc++ -Wl,-rpath,/path/to/libcxx/lib $@
    

    Then you can run stdclang <other_args> to have it automatically expand to clang++ -stdlib=libc++ -Wl,-rpath,/path/to/libcxx/lib <other_args>

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
ocket8888
  • 1,060
  • 12
  • 31
  • Unfortunately, this is not what I'm looking for. In your solutions, you always specify e.g. the `-stdlib` flag, even where it does not make sense (e.g. if just running the preprocessor stage). Also, I am looking for user-transparent solution, i.e. no wrapper scripts, no changed executable names etc. – Michael Schlottke-Lakemper Feb 09 '16 at 08:09
  • Unfortunately, if clang itself doesn't offer these configuration options, then all you can do is what I listed, edit the clang source files (I think that's legal?) or write your own compiler. Or use a different compiler that does support stdlib linking without explicitly stating it *cough* gcc *cough*. – ocket8888 Feb 09 '16 at 11:44
  • 1
    Also, if you're only running the preprocessor, you'd just invoke `clang` normally, not `stdclang`. I just now realized it may not be completely clear, but this won't eliminate your ability to use clang by itself, it's effectively just an alias that every user can use. – ocket8888 Feb 17 '16 at 17:49