-static option for gcc? - gcc

I'm wondering what the -static option on gcc does. I need this option when compiling a certain application, however when I do I get the following error:
gcc -static -O3 -o prog prog.c
/usr/bin/ld: cannot find -lc
collect2: ld returned 1 exit status
What needs installation?
GCC version:
[user#localhost dir]$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.6.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.6.1 20110908 (Red Hat 4.6.1-9) (GCC)

The -static option links a program statically, in other words it does not require a dependency on dynamic libraries at runtime in order to run.
To achieve static linking requires that the archive (.a) versions of your libraries exist on the system. so /usr/lib/libc.a, /usr/lib/crt1.o, etc.
On modern linux systems (as you are using red hat): when a binary links together it 1) either puts the code into the executable via .o and .a files, or 2) puts in references to dynamic libraries (.so) files that is resolved by /lib/ld-linux.so (or /lib64/ld-linux=x86-64.so) which is always at a well known place.
For your particular system, if a program is specifically looking to create a static version of itself then you need to install the static versions of your devel tools. You need, at the minimum, glibc-static package. You may also need libstdc++-static package as well.

The -static flag forces the linker to accept only static libraries and not any shared libraries.
If you want to use -static, you have to ensure that you have a static version of the C library installed, which might be tricky to find (most systems do not have a static C library any more). Or you have to cancel the effect of -static. However, in the example, that would defeat the purpose of -static since the only library linked is (implicitly) the C library.

On systems that support dynamic linking, this overrides -pie and prevents linking with the shared libraries. On other systems, this option has no effect.
Ref: https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

Related

warnings while trying to compile old linux kernel with gcc 12.1.0

I am currently hunting a bug using git bisect in linux staging-testing tree.
Some commits are giving following errors after I try to build them:
subcmd-util.h:58:31: error: pointer may be used after ‘realloc’ [-Werror=use-after-free]
58 | ret = realloc(ptr, 1);
check.c:2867:58: error: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size 9 [-Werror=format-truncation=]
2867 | snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx);
I tried adding -Wno-error=use-after-free to KBUILD_FLAGS in main Makefile, tried adding
KBUILD_CFLAGS += $(call cc-disable-warning, use-after-free)
I even tried removing -Werror from CFLAGS in Makefile in folder where problematic files are located, but nothing worked.
More information:
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.artixlinux.org/ --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror --with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.1.0 (GCC)
Linux staging tree: git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git.
One of the commits that gives following warnings: 2ab9c9675fe892e7fe9fa8c0a6125e2b40d2889d.
To compile older version of kernel I needed older compiler ("Linux kernel sources are in general not forward-compatible with new compilers"). In my case the kernel version was 5.16.0 and I could compile it with gcc 11. If you have gcc 11 specific warnings then try gcc 10 and so on.
Except CC flags, I also needed set to HOSTCC flags to use gcc-11 compiler:
make HOSTCC=gcc-11 CC=gcc-11

I just built boost - and the output is sat on my desktop

I followed these instructions from github to clone and build the boost libraries.
https://github.com/boostorg/boost/wiki/Getting-Started
The end of the ./b2 command tells me
The Boost C++ Libraries were successfully built!
The following directory should be added to compiler include paths:
/home/user/Desktop/boost
The following directory should be added to linker library paths:
/home/user/Desktop/boost/stage/lib
What should I do with these folders? I would like to put boost in the "regular place it should go in order for gcc to see it".
This is probably /usr/include ? Is it safe for me to move the folder boost to this location? What about the boost/stage/lib folder. Where does this go?
gcc -v /dev/null -o /dev/null
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,objc,obj-c++,fortran,ada,go,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --disable-libgcj --with-isl --enable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 6.3.1 20161221 (Red Hat 6.3.1-1) (GCC)
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/:/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/6.3.1/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/6.3.1/:/usr/lib/gcc/x86_64-redhat-linux/6.3.1/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/6.3.1/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' '/dev/null' '-mtune=generic' '-march=x86-64'
/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/collect2 -plugin /usr/libexec/gcc/x86_64-redhat-linux/6.3.1/liblto_plugin.so -plugin-opt=/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/lto-wrapper -plugin-opt=-fresolution=/tmp/cce2dq2i.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id --no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o /dev/null /usr/lib/gcc/x86_64-redhat-linux/6.3.1/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/6.3.1/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/6.3.1/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/6.3.1 -L/usr/lib/gcc/x86_64-redhat-linux/6.3.1/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/6.3.1/../../.. /dev/null -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-redhat-linux/6.3.1/crtend.o /usr/lib/gcc/x86_64-redhat-linux/6.3.1/../../../../lib64/crtn.o
/dev/null: file not recognized: File truncated
collect2: error: ld returned 1 exit status
I would like to put boost in the "regular place it should go in order for gcc to see it".
For gcc run cpp -v /dev/null -o /dev/null. In its output there are include search directories and LIBRARY_PATH with a list of library search directories. These are the regular places. Copying the headers and libraries into the regular places normally requires root permissions.
Alternatively, use -I/home/user/Desktop/boost extra command line option when compiling and -L/home/user/Desktop/boost/stage/lib -Wl,-rpath=/home/user/Desktop/boost/stage/lib when linking to use it from the current location.
Another way to build boost (and other 3rd-party libraries) is to have it installed in a separate directory, e.g. /usr/local with versioned directories, e.g. /usr/local/boost-1.60.0.
To build boost into /usr/local/boost-1.60.0 do the following commands in bash:
$ cd boost-1.60.0 # The directory with boost sources.
$ JOBS=8
$ PREFIX=/usr/local/boost-1.60.0
$ mkdir -p ${PREFIX}
$ ./bootstrap.sh --prefix=${PREFIX} --libdir=${PREFIX}/lib64
$ B2FLAGS="-j${JOBS} --layout=system --disable-icu variant=release link=shared threading=multi runtime-link=shared linkflags=-Wl,-rpath,${PREFIX}/lib64 boost.locale.icu=off boost.locale.iconv=on"
$ ./b2 ${B2FLAGS}
$ ./b2 ${B2FLAGS} install
The linkflags=-Wl,-rpath,${PREFIX}/lib64 bit makes sure that when you run your application boost shared libraries that depend on other boost shared libraries find them in that ${PREFIX}/lib64 directory.
And then use the following flags when builing your applications with boost you just built:
Flags for compiling (CPPFLAGS): -I/usr/local/boost-1.60.0/include.
Flags for linking (LDFLAGS): -L/usr/local/boost-1.60.0/lib64 -Wl,-rpath=/usr/local/boost-1.60.0/lib64.

CodeSourcery ARM cross-compilation

Compiling a simple test.c:
#include <stdio.h>
void main() {
printf("Hello World!\n");
while(1);
}
using CodeSourcery's arm-none-eabi-gcc I got the following errors:
arm-none-eabi-gcc -static test.c -o mytest
/opt/Sourcery_CodeBench_Lite_for_ARM_EABI/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/bin/ld: warning: cannot find entry symbol _start; defaulting to 00008018
/opt/Sourcery_CodeBench_Lite_for_ARM_EABI/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/lib/libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text+0x18): undefined reference to `_sbrk'
/opt/Sourcery_CodeBench_Lite_for_ARM_EABI/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/lib/libc.a(lib_a-writer.o): In function `_write_r':
writer.c:(.text+0x20): undefined reference to `_write'
/opt/Sourcery_CodeBench_Lite_for_ARM_EABI/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/lib/libc.a(lib_a-closer.o): In function `_close_r':
closer.c:(.text+0x18): undefined reference to `_close'
/opt/Sourcery_CodeBench_Lite_for_ARM_EABI/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/lib/libc.a(lib_a-fstatr.o): In function `_fstat_r':
fstatr.c:(.text+0x1c): undefined reference to `_fstat'
/opt/Sourcery_CodeBench_Lite_for_ARM_EABI/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/lib/libc.a(lib_a-isattyr.o): In function `_isatty_r':
isattyr.c:(.text+0x18): undefined reference to `_isatty'
/opt/Sourcery_CodeBench_Lite_for_ARM_EABI/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/lib/libc.a(lib_a-lseekr.o): In function `_lseek_r':
lseekr.c:(.text+0x20): undefined reference to `_lseek'
/opt/Sourcery_CodeBench_Lite_for_ARM_EABI/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/lib/libc.a(lib_a-readr.o): In function `_read_r':
readr.c:(.text+0x20): undefined reference to `_read'
collect2: error: ld returned 1 exit status
But if i used my Ubuntu (12.04 LTS 32-bit) 's arm gcc:
arm-linux-gnueabi-gcc -static -o mytest test.c
arm-linux-gnueabi-gcc -v
Using built-in specs.
COLLECT_GCC=arm-linux-gnueabi-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabi/4.6/lto-wrapper
Target: arm-linux-gnueabi
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/arm-linux-gnueabi/include/c++/4.6.3 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --enable-multilib --disable-sjlj-exceptions --with-arch=armv7-a --with-float=softfp --with-fpu=vfpv3-d16 --with-mode=thumb --disable-werror --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=arm-linux-gnueabi --program-prefix=arm-linux-gnueabi- --includedir=/usr/arm-linux-gnueabi/include --with-headers=/usr/arm-linux-gnueabi/include --with-libs=/usr/arm-linux-gnueabi/lib
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
I have no error generating the output file.
can someone explain?
My best nearest reference (but still I have no success):
Linker error on a C project using eclipse
arm-none-eabi-gcc is a compiler for bare metal arm. I.e. for running on an arm processor where you have no operating system, or where you make your own operating system. That compiler contains the newlib C library, which will provide you with most of a standard C library, but you will have to provide the low level implementations (e.g. somewhere for printf() to actually print, memory management, a file system if you need it). In short, you're much on your own having to bootstrap the processor, C runtime and load the produced code (See e.g. this question))
arm-linux-gnueabi-gcc is a compiler for targeting linux running on arm, a toolchain that'll produce executables running on linux, using the familiar glibc C library and header files.

Troubleshooting warning from "make -j N"

When I issue a command like (for example) make -j 4, I get the following error:
warning: jobserver unavailable: using -j1. Add `+' to parent make rule
I am using the developer toolkit from Scientific Linux 6 under RHEL6:
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-2/root/usr/libexec/gcc/x86_64-redhat-linux/4.8.2/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/opt/rh/devtoolset-2/root/usr --mandir=/opt/rh/devtoolset-2/root/usr/share/man --infodir=/opt/rh/devtoolset-2/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-languages=c,c++,fortran,lto --enable-plugin --with-linker-hash-style=gnu --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-x86_64-redhat-linux/cloog-install --with-mpc=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-x86_64-redhat-linux/mpc-install --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.2 20140120 (Red Hat 4.8.2-15) (GCC)
I have libmpc 0.8.3 installed:
$ rpm -qa | grep mpc
libmpcdec-1.2.6-6.1.el6.x86_64
libmpc-0.8-3.el6.x86_64
Is there a way to troubleshoot my project's makefile or gcc to determine what is causing the warning and fix it? I do not seem to need the + symbol added to rules when building from OS X via Clang/LLVM. Google searches on the warning text are not returning much information that is apparently useful.
Whenever you run make from inside a makefile you should always use the $(MAKE) variable. Never use the raw, literal command make.

Cannot find -lgfortran though gfortran is installed [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
lgfortran not found
I must admit that my question can be considered to be very similar to lgfortran not found, but as I am a newbie in Linux I need more help and I couldn't find what I am looking for in the above post.
I have a fresh linux mint Debian edition installed and I am trying to make a lapack binding for Ada to work.
If I check for gfortran, I get:
gfortran-4.6 -v
Using built-in specs.
COLLECT_GCC=gfortran-4.6
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i486-linux-gnu/4.6/lto-wrapper
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.6.3-1' --with- bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --enable-targets=all --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.6.3 (Debian 4.6.3-1)
And if I locate libgfortran, I get:
/usr/lib/gcc/i486-linux-gnu/4.6/libgfortran.a
/usr/lib/gcc/i486-linux-gnu/4.6/libgfortran.so
/usr/lib/gcc/i486-linux-gnu/4.6/libgfortran.spec
/usr/lib/gcc/i486-linux-gnu/4.6/libgfortranbegin.a
/usr/lib/i386-linux-gnu/libgfortran.so.3
/usr/lib/i386-linux-gnu/libgfortran.so.3.0.0
/usr/share/doc/libgfortran3
/var/lib/dpkg/info/libgfortran3.list
/var/lib/dpkg/info/libgfortran3.md5sums
/var/lib/dpkg/info/libgfortran3.postinst
/var/lib/dpkg/info/libgfortran3.postrm
/var/lib/dpkg/info/libgfortran3.shlibs
/var/lib/dpkg/info/libgfortran3.symbols
When I run a makefile, I get:
make all
gcc -c cxbbase.ads
gcc -c cxbbase.ads
gnatmake cxbi.adb -largs ifinc.o -lgfortran -lm
gnatbind -x cxbi.ali
gnatlink cxbi.ali ifinc.o -lgfortran -lm
/usr/gnat/libexec/gcc/i686-pc-linux-gnu/4.5.3/ld: cannot find -lgfortran
collect2: ld returned 1 exit status
gnatlink: error when calling /usr/gnat/bin/gcc
gnatmake: *** link failed.
make: *** [cxbi] Error 4
The makefile that I'm using is here:
http://12000.org/my_notes/ada/lapack_and_blas/lapack_interf_makefile.txt
As I am very new to Linux, I would be very grateful if I could get detailed instructions on how to fix this.
Thank you
The solution was to /usr/lib/gcc/i486-linux-gnu/4.6 to LIBRARY_PATH. This solution is discussed here on Comp Lang Ada:
https://groups.google.com/forum/#!topic/comp.lang.ada/Ii7YljKqu5M
The versions of your GNAT and your GCC don't match. Please use the same version for both.
Your GNAT seems to be version 4.5.3, while your GCC is 4.6.3.
My guess would be that you have to update GNAT (gnat package on Debian). How did you even get your GNAT installed? Debian seems to only have 4.4 and 4.6 in the repos. Additionally, the official packages don't use /usr/gnat at all.

Resources