Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

-static option for 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)  
like image 511
sj755 Avatar asked Jan 01 '12 06:01

sj755


People also ask

Which GCC option is used for compilation?

GCC stands for GNU Compiler Collections which is used to compile mainly C and C++ language. It can also be used to compile Objective C and Objective C++.

What is compile option?

Compilers options (− x on Linux, and /Qx on Microsoft Windows) control which instructions the compiler uses within a function, while the processor(…) clause controls creation of non-standard functions using wider registers (YMM or ZMM) for passing SIMD data for parameters and results.

What is fPIC option in GCC?

fPIC option in GCC enables the address of shared libraries to be relative so that the executable is independent of the position of libraries. This enables one to share built library which has dependencies on other shared libraries. fPIC stands for "force Position Independent Code".

Which option must be used to inform GCC to stop after the preprocessing stage without running the compiler proper?

If you only want some of the stages of compilation, you can use -x (or filename suffixes) to tell gcc where to start, and one of the options -c , -S , or -E to say where gcc is to stop. Note that some combinations (for example, ' -x cpp-output -E ') instruct gcc to do nothing at all.


1 Answers

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.

like image 108
Ahmed Masud Avatar answered Oct 05 '22 00:10

Ahmed Masud