Gcc

From Omnia
Revision as of 19:24, 4 April 2016 by Kenneth (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Subpage Table of Contents

gcc

http://en.wikipedia.org/wiki/GNU_Compiler_Collection GNU Compiler Collection - Wikipedia]
"The GNU Compiler Collection (usually shortened to GCC) is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain. As well as being the official compiler of the GNU system, GCC has been adopted as the standard compiler by most other modern Unix-like computer operating systems, including GNU/Linux, the BSD family and Mac OS X. GCC has been ported to a wide variety of processor architectures, and is widely deployed as a tool in commercial, proprietary and closed source software development environments. GCC is also available for most embedded platforms, for example Symbian, AMCC and Freescale Power Architecture-based chips. The compiler can target a wide variety of platforms, including videogame consoles such as the Playstation 2 and Sega Dreamcast. Several companies make a business out of supplying and supporting gcc ports to various platforms, and chip manufacturers today consider a gcc port almost essential to the success of an architecture.
Originally named the GNU C Compiler, because it only handled the C programming language, GCC 1.0 was released in 1987, and the compiler was extended to compile C++ in December of that year. Front ends were later developed for Fortran, Pascal, Objective C, Java, and Ada, among others.
The Free Software Foundation (FSF) distributes GCC under the GNU General Public License (GNU GPL) and the GNU Lesser General Public License (GNU LGPL). GCC is free software."


GCC, the GNU Compiler Collection - GNU Project - Free Software Foundation (FSF)

Hello World

hello.c:

#include <stdio.h>

int main() {
  printf("Hello World\n");
  return 0;
}

Compile:

gcc hello.c -o hello
./hello

Library

mylib.c:

#include <stdio.h>

void toast(char * msg) {
  printf("Message: %s\n", msg);
}

mylib.h:

void toast(char * msg);

Compile shared library:

gcc -c -fpic mylib.c   # output: mylib.o, can also explicitly use '-o mylib.o'
gcc -fPIC -Wall -g -c mylib.c  # PIC for larger libraries

gcc -shared -o libmylib.so mylib.o
# or
ld -shared -soname libmylib.so.1 -o libmylib.so.1.0 mylib.o

Or you can specify Soname: (otherwise none will be included)

gcc -shared -Wl,-soname,libmylib.so.1 -o libmylib.so.1.0.1  mylib.o

# find with:
objdump -p libmylib.so.1.0.1 | grep SONAME
SONAME      libmylib.so.1

Note: For the record, pic stands for "position-independent code", an object-code format required for shared libraries. You might need to use the option -fPIC instead of -fpic if your library is very large.

Test:

mkdir -p test test/lib test/include
cp mylib.h test/include
cp libmylib.so test/lib
cd test

test/test.c:

#include <mylib.h>

int main() {
  toast("This is a test");
}

Compile:

gcc -I./include -L./lib -lmylib test.c -o test

Note: If you try running as is, you will get this error:

# ./test
./test: error while loading shared libraries: libmylib.so: cannot open shared object file: No such file or directory

Test:

LD_LIBRARY_PATH=lib ./test

To install in the system copy to /usr/lib and run ldconfig (not sure ldconfig is required, maybe creates the top .so.1 link?)

cp libmylib.so.1.0 /usr/lib
ldconfig -n /usr/lib/
ln -sf /usr/lib/libmylib.so.1 /usr/lib/libmylib.so

For static library:

ar rcs libmylib.a mylib.o
gcc -static main.c -L. -lmylib -o static_linked_app

Good References:

Shared Libraries

Also known as Dynamic Libraries, as they are dynamically loaded when an application is executed.

-

Shared Libraries - http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

Shared libraries are libraries that are loaded by programs when they start. When a shared library is installed properly, all programs that start afterwards automatically use the new shared library. It's actually much more flexible and sophisticated than this, because the approach used by Linux permits you to:
  • update libraries and still support programs that want to use older, non-backward-compatible versions of those libraries;
  • override specific libraries or even specific functions in a library when executing a particular program.
  • do all this while programs are running using existing libraries.

-

The Linux GCC HOWTO: Linking - http://netwinder.osuosl.org/pub/netwinder/docs/misc/GCC-HOWTO-html/GCC-HOWTO-6.html

Between the two incompatible binary formats, the static vs shared library distinction, and the overloading of the verb `link' to mean both `what happens after compilation' and `what happens when a compiled program is invoked' (and, actually, the overloading of the word `load' in a comparable but opposite sense), this section is complicated. Little of it is much more complicated than that sentence, though, so don't worry too much about it.
To alleviate the confusion somewhat, we refer to what happens at runtime as `dynamic loading' and cover it in the next section. You will also see it described as `dynamic linking', but not here. This section, then, is exclusively concerned with the kind of linking that happens at the end of a compilation.

-

Short Course on Linux Libraries - http://jrv.oddones.org/lib.html

nm - can list the symbols defined in a library

-

segfault.in » How to create shared libraries with gcc? - http://segfault.in/2008/05/how-to-create-shared-libraries-with-gcc/

Shared libraries are libraries that are loaded by programs when they start. All programs that start afterwards automatically use the new shared library.

-

Library - LQWiki - http://wiki.linuxquestions.org/wiki/Library

If you are a programmer, you will want to re-use code written by others and design your program to be modular. You build up libraries containing re-usable code

Dissecting shared libraries

Summary: Shared libraries use version numbers to allow for upgrades to the libraries used by applications while preserving compatibility for older applications. This article reviews what's really going on under the book jacket and why there are so many symbolic links in /usr/lib on a normal Linux™ system.

Compatibility's not just for relationships

This means that the library you end up being linked to had better be compatible with the code that's calling it. With a statically linked executable, there is some guarantee that nothing will change on you. With dynamic linking, you don't have that guarantee.

What happens if a new version of the library comes out? Especially, what happens if the new version changes the calling sequence for a given function?

Version numbers to the rescue -- a shared library will have a version. When a program is linked against a library, it has the version number it's designed for stored in it. The dynamic linker can check for a matching version number. If the library has changed, the version number won't match, and the program won't be linked to the newer version of library.

One of the potential advantages of dynamic linking, however, is in fixing bugs. It'd be nice if you could fix a bug in the library and not have to recompile a thousand programs to take advantage of that fix. So sometimes, you want to link to a newer version.

Unfortunately, that creates some cases where you want to link to the newer version and some cases where you'd rather stick with an older version. There is a solution, though -- two kinds of version numbers:

  • A major number indicates a potential incompatibility between library versions.
  • A minor number indicates only bug fixes.

So under most circumstances, it is safe to load a library with the same major number and a higher minor number; consider it an unsafe practice to load a library with a higher major number.

To prevent users (and programmers) from needing to track library numbers and updates, the system comes with a large number of symbolic links. In general, the pattern is that

libexample.so

will be a link to

libexample.so.N

in which N is the highest major version number found on the system.

For every major version number supported,

libexample.so.N

will be a link in turn to

libexample.so.N.M

in which M is the largest minor version number.

Thus, if you specify -lexample to the linker, it looks for libexample.so which is a symbolic link to a symbolic link to the most recent version. On the other hand, when an existing program is loaded, it will try to load libexample.so.N in which N is the version to which it was originally linked. Everyone wins!

reference:

Notes

The Linux GCC HOWTO

"This document covers how to set up the GNU C compiler and development libraries under Linux, and gives an overview of compiling, linking, running and debugging programs under it. Most of the material in it has been taken from Mitch D'Souza's GCC-FAQ or the ELF-HOWTO - it replaces both documents."


GNU toolchain - Wikipedia

"The GNU toolchain is a blanket term for a collection of programming tools produced by the GNU Project. These tools form a toolchain (suite of tools used in a serial manner) used for developing applications and operating systems.

The GNU toolchain plays a vital role in development of Linux kernel, BSD, and software for embedded systems. Parts of the GNU toolchain are also directly used with or ported to other platforms such as Solaris, Mac OS X, Microsoft Windows (via Cygwin and MinGW/MSYS) and Sony PlayStation 3.

Projects included in the GNU toolchain are:

   * GNU make: Automation tool for compilation and build;
   * GNU Compiler Collection (GCC): Suite of compilers for several programming languages;
   * GNU Binutils: Suite of tools including linker, assembler and other tools;
   * GNU Debugger (GDB): Code debugging tool;
   * GNU build system (autotools):
         o Autoconf
         o Autoheader
         o Automake
         o Libtool


Linux Tutorial - Static, Shared Dynamic and Loadable Linux Libraries

"This tutorial discusses the philosophy behind libraries and the creation and use of C/C++ library "shared components" and "plug-ins". The various technologies and methodologies used and insight to their appropriate application, is also discussed. In this tutorial, all libraries are created using the GNU Linux compiler. "



To compile static: [1]

To compile a package (that comes with configure script) in a static way I use these commands:
  configure LDFLAGS=-static
  make LDFLAGS=-all-static

keywords