Linux Compile Code __linux

Source: Internet
Author: User
Tags sin

Turn from: http://www.iteye.com/topic/240578

GNU Compiler setThe GNU Compiler set (formerly the GNU C compiler) was born in 1987. At that time, Richard Stallman (founder of the GNU Project) wanted to create a compiler that would meet his defined "free software" concept and be used to compile other software published by the GNU Project. The GNU C compiler is quickly popular in the free software community and is known for its robustness and portability. It has become the foundation of many integrated development tools, and has been applied by publishers around the world on Linux and other operating systems.
GCC is no longer a small C-language compiler for software designed primarily for the GNU project itself. Today, it has supported many different languages, including C, C + +, Ada, Fortran, objective C, and even Java. In fact, modern Linux systems support a large number of other languages in addition to being proud to show off languages that are directly supported by GNU tools. The increasingly popular scripting languages Perl, Python, and Ruby, and the evolving mono portable C # implementations do help dilute the traditional view of Linux programming, but this is a completely different issue.

The Linux kernel and many other free software and open source applications are written in C and compiled using GCC.

1. Compiling a single source file

To test, you can create a "Hello World" program:

C code #include <stdio.h> #include <stdlib.h> int main (int argc, char **argv) {printf ("Hell           o world!\n ");   Exit (0); }


Compile and test the code using the following command:
Reference # Gcc-o Hello hello.c
#./hello
Hello wordl!

The executable program name generated by default is A.out, but you can usually specify your own executable name through GCC's "-o" option.

2. Compile multiple source files

The source file message.c contains a simple message printing function:
C code #include <stdio.h> void Goodbye_world (void) {printf ("Goodbye, world!\n"); }

Compile the Support library code using GCC's "-C" tag:
Reference # Gcc-c MESSAGE.C

The output of this process is a file called MESSAGE.O that contains the compiled target code that is appropriate to connect to a larger program.

Create a simple sample program that contains a main function that calls Goodbye_world

C code #include <stdlib.h> void Goodbye_world (void): int main (int argc, char **argv) {Goodbye_           World ();   Exit (0); }

Compile this program with GCC:
Reference # Gcc-c MAIN.C

There are now two target files: MESSAGE.O and MAIN.O. They contain target code that can be executed by Linux. To create a Linux executable from this target code, you need to call GCC again to perform the connection phase:
Reference # Gcc-o Goodbye MESSAGE.O MAIN.O

To run the compilation results:
Reference #./goodbye
Goodbye, world!

These individual steps can also be reduced to a single command because GCC has built-in rules for how to compile multiple source files into an executable program.
Reference # Gcc-o Goodbye message.c main.c
#./goodbye
Goodbye, world!

3. Using external function Libraries
GCC is often used in conjunction with external software libraries that contain standard routines, and almost every Linux application relies on the GNU C function library glibc.
Examples of applying external function libraries:
C code   #include  <stdio.h>   #include  <stdlib.h>   #include  < math.h>      #define  MAX_INPUT 25      int main (int &NBSP;AGRC,&NBSP;CHAR&NBSP;**ARGV)    {            char input[max_input];           double angle;               printf ("give me an  angle  (In radians)  ==> ");           if (! Fgets (Input, max_input, stdin)) {                    perror ("an error occurred.\n");            }           angle =  strtod (input,  null);              printf ("Sin (%e)  =  %e\n ",  angle, sin (angle));               return 0;  }   Compilation Command:
Reference # Gcc-o TRIG-LM trig.c

GCC's "-lm" option, which tells GCC to view the system-supplied math library (LIBM). Because Linux and Unix system libraries are usually prefixed with "lib," we assume it exists. The true function library location varies from one system to another, but it is typically located in the directory/lib or/usr/lib, where there are hundreds of other required system function libraries.

4. Shared function library and static function library

Functional libraries on Linux systems are divided into two different types: shared and static

static function libraries: Each time an application is compiled with a statically connected function library, the code in any referenced library function is directly included in the final binary.

Shared function libraries: contains a single global version of each library function, which is shared across all applications. The mechanism involved behind this process is rather complex, but relies primarily on the virtual memory capabilities of modern computers, which allow the physical memory that contains library functions to be securely shared among multiple independent user programs.

Using a shared library not only reduces the size of the file and the areas in which the Linux application is covered in memory, but it also enhances the security of the system. A shared library that is called at the same time by many different programs is likely to reside in memory to be used immediately when it is needed, rather than in a swap partition on the disk. This helps to further reduce the load time for some large Linux applications.

Use the above message.c as an example of a shared library function:

Reference # Gcc-fpic-c MESSAGE.C
The "PIC" command-line tag tells GCC that the code produced does not contain a reference to the function and the specific memory location of the variable, because it is not yet known where the application that uses the message code will connect it to the memory address space. The file message.o that compiles the output can be used to build a shared function library, and we simply use GCC's "-shared" tag to:
Reference # Gcc-shared-o libmessage.so MESSAGE.O

Compile the MIAN.C using the shared library function ligmessage.so:
Refer to the # gcc-o goodbye-lmessage-l. message.o '-lmessage ' tag to tell GCC to reference the shared function library libmessage.so during the connection phase. "-L." The tag tells GCC that the function library may be in the current directory, otherwise the GNU Connector looks for the standard system function library directory, and in this case, the available function libraries are not found.

When you run the compiled goodbye, you will be prompted not to find the shared function library:
Referencing #./goodbye
./goodbye:error while loading shared libraries:libmessage.so:cannot open Shared object file:no such file or directory

You can use command LDD to discover a library of functions that a particular application needs to use. LDD searches the standard system function library path and displays the version of the function library that a particular program uses.
Reference #ldd Goodbye
Linux-gate.so.1 => (0x00493000)
Libmessage.so => not found
Libc.so.6 =>/lib/libc.so.6 (0x0097c000)
/lib/ld-linux.so.2 (0x0095a000)

The library file libmessage.so cannot be found in any of the standard search paths, and the system-supplied configuration file/etc/ld.so.conf does not contain an additional entry to specify the directory containing the library file.

You need to set up an environment variable LD_LIBRARY_PATH to create an additional shared function library search path,
Quote # export ld_library_path= ' pwd '
# LDD Goodbye
Linux-gate.so.1 => (0x002ce000)
libmessage.so =>/tmp/cpro/libmessage.so (0x00b0f000)
Libc.so.6 =>/lib/libc.so.6 (0x0097c000)
/lib/ld-linux.so.2 (0x0095a000) Run program
Reference #./goodbye
Goodbye, world!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.