33

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.

6
  • clang doesn't have a configure option for this? Commented Dec 20, 2013 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. Commented Dec 20, 2013 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. Commented Jul 2, 2015 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. Commented Jul 3, 2015 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? Commented Jul 6, 2015 at 14:57

2 Answers 2

20

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Finally, after two years of waiting I can close this issue! @Michael Haidl, you are my hero :)
People who are interested in CLANG_DEFAULT_CXX_STDLIB=libc++ might also be interested in CLANG_DEFAULT_RTLIB=compiler-rt.
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>

3 Comments

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.
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.