Fedora 21 with clang, without gcc

你。 提交于 2019-12-10 20:45:21

问题


Can you (reasonably) get Fedora 21 to where it only has llvm/clang/libc++/libc++abi? (I found some things suggesting no, but they were all about 3 years old, and llvm/clang has come a long way since then.)

With a fresh install, I tried

yum install gcc gcc-c++
(downloaded, built, installed llvm/cfe(clang)/compiler-rt/libcxx/libcxxabi from svn)
yum remove gcc gcc-c++
added to /etc/profile: export CC=/usr/local/bin/clang \ export CXX=/usr/local/bin/clang++
(in case of hard wiring)
ln -s /usr/local/bin/clang /usr/local/bin/gcc
ln -s /usr/local/bin/clang /usr/local/bin/cc
ln -s /usr/local/bin/clang++ /usr/local/bin/g++
ln -s /usr/local/bin/clang++ /usr/local/bin/c++
ldconfig

I was all happy, then went to build something, and I got:

ld: cannot find crtbegin.o
ld: cannot find -lgcc
ld: cannot find -lgcc_s

clang -v includes

Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.9.2

ldconfig && ldconfig -p | grep libgcc does show

libgcc_s.so.1 (libc6,x86-64) => /lib64/libgcc_s.so.1

And /lib64 is a symlink to /usr/lib64. And, /usr/lib64/libgcc_s.so.1 is a symlink to /usr/lib64/libgcc_s-4.9.2-20150212.so.1, which exists as a real file (92816 bytes.)

So, I don't get what ld's problem is on -lgcc_s. crtbegin is nowhere to be found, and gcc (no _s) is nowhere to be found.

yum install libgcc says it's already installed and latest version, nothing to do.

Since I have an installed clang source build, can I re-build clang, this time using clang rather than gcc, to get rid of the dependency? (Maybe then the "candidate GCC installation" bit goes away.)

Can I force -stdlib=c++ and -lc++abi to be default, or at least have libc++ and libc++abi installed without gcc?


回答1:


Having spent some time trying to get clang to work with libc++ and libc++abi without GCC, I have found that it is indeed possible, even if a bit problematic given the current state of LLVM/clang. In addition to small test programs, I've been able to build CMake and some other software packages written in C++ with no GCC installed, and with the resulting binaries being independent of libstdc++; they only depend on libc++/libc++abi according to ldd output. Unfortunately, I haven't been able to build clang itself with clang that was build using GCC. I've been experimenting on different Linux platforms (Fedora 21 32-bit, Amazon Linux release 2015.3 (RPM-based) 64-bit, CentOS 7.1 64-bit, and Ubuntu 14.04 64-bit).

Even though one can build software with clang using libc++/libc++abi without dependency on libstdc++ and without GCC compiler present, a typical Linux installation is so tied to libgcc and libstdc++ that getting rid of these is not practical. Try removing these two packages and you will see how much of the system depends on them. Even on FreeBSD 10.1, with clang being the default compiler and no GCC installed, libgcc.a, libgcc_s.so, and a few crt*.o files are used when building a program as revealed by the -v option. Also, on FreeBSD 10.1, resulting binaries depend on libgcc according to ldd. On Ubuntu, which has dpkg as the package manager, the files

   libgcc.a
   libgcc_s.so
   crtbegin.o
   crtbeginT.o
   crtbeginS.o
   crtendS.o
   crtend.o

are in the libgcc-devel package, while on an RPM-based system, such as Fedora, these are in the gcc package. In addition, you might possibly need these files, even though I didn't need them for the code I tried building:

   crtfastmath.o
   crtprec32.o
   crtprec80.o
   crtprec64.o

Thus one might argue that the aforementioned files better belong in libgcc, rather than in gcc. As far as I can tell, the following needs to be done on an RPM-based system before removing the gcc package:

1) Create the symlink

libgcc_s.so -> libgcc_s.so.1

in whatever directory libgcc_s.so.1 is located.

2) Copy the crt*.o files listed above to that directory.

3) In the same directory create the symlink (libstdc++.so.x should already be there; x is a number):

libstdc++.so -> libstdc++.so.x

You only need this if you are going to use libstdc++; this isn't needed if you only plan to use libc++. On some systems libstdc++.so, which is a symlink to libstdc++.so.x belonging to the libstdc++ package, is placed by the libstdc++-devel package into the GCC library directory, so you can remove that directory after uninstalling GCC and just create the symlink in the same directory where libstdc++.so.x lives.

Now you should be able to do the following:

1) Build a C program:

clang progname.c

2) Build a C++ program using libstdc++ headers/libs:

clang++ -I<location of headers> progname.cpp

On RPM-based systems I've looked at, the libstdc++ headers are part of the libstdc++-devel package and their location can be found from rpm -ql on the package.

3) Build a C++ program using libc++ headers/libs:

clang++ -I/<location of headers> progname.cpp -nodefaultlibs -lc++ -lc++abi -lm -lc -lgcc -lgcc_s

The location of the headers is wherever they were installed when you built LLVM+clang etc.

Please see http://libcxx.llvm.org/ for additional information. When building C++ code using libc++/libc++abi, you may use -stdlib=libc++ instead of the -I flag, but in my testing that only worked with clang built from source, not with clang installed from a repository (you can install clang from repo and use it to build libc++/libc++abi; or you can use gcc to build libc++(abi), then remove gcc and use the libs with the repo-provided clang).

When configuring a software package to build it using clang + libc++, you might need to set the following:

LIBS="-nodefaultlibs -lc++ -lc++abi -lm -lc -lgcc_s -lgcc"
CXX=clang++
CXXFLAGS="-stdlib=libc++"
CC=clang

Please note that to configure CMake source in order to build it I had to use a wrapper script like this:

#!/bin/bash

MYLFLAGS="-nodefaultlibs -lc++ -lc++abi -lm -lc -lgcc_s -lgcc"

# Catch the case when we only want to compile; this helps us avoid some warnings:
if echo "$@" | egrep "(^-c | -c | -c$)" >/dev/null 2>&1; then
MYLFLAGS=""
fi

/usr/local/bin/clang++ -stdlib=libc++ "$@" $MYLFLAGS

It might be useful for other purposes as well.

For more information please see my article at http://www.omniprog.info/clang_no_gcc.html



来源:https://stackoverflow.com/questions/30135425/fedora-21-with-clang-without-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!