Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top