GCC compiled c C + + and C + +

Source: Internet
Author: User
Tags naming convention

compiling C From Ubuntu Chinese

Directory Hidden

1 C programming related file suffix 2 single source file build executable program 3 source file generate object file 4 multiple source files generate executable program 5 compile preprocessing 6 generate assembly code 7 create a static library 8 create a shared library 9 beyond naming convention 10 other references

[edit] related file suffix in C programming

. A Static Library (archive)
. C C Source code (requires compilation preprocessing)
. h C Source on behalf of the dock file
. I C Source code (without compilation preprocessing)
. o Object files
. s Assembly language Code
. So Dynamic Library

[Edit] A single source file generates an executable program

The following is a simple "Hello, ubuntu" program source code:

* HELLOUBUNTU.C * *
#include <stdio.h>
int main (int argc,char *argv[])
{
printf ("Hello, ubuntu\n");

return 0;
}

The simplest way to compile the code as an executable is to save the code as a file helloubuntu.c and execute the following command: $ gcc-wall helloubuntu.c

The compiler recognizes a C source code file by checking the suffix name of the file specified in the command line. GCC Default action: Compile source code file to generate object file, link object file get executable program, delete object file. The compiler takes the default a.out because the file name of an executable program is not specified on the command line. Enter the program name on the command line to execute it and display the result: $./a.out
Hello, Ubuntu

Option-O is used to specify the file name of the generated executable program. The following command generates an executable program named Helloubuntu: $ gcc-wall helloubuntu.c-o Helloubuntu

Enter the program name on the command line to execute it as follows: $./helloubuntu
Hello, Ubuntu

Edit source file build object file

Option-C instructs GCC to compile the source code file, but keep the object file on disk and skip the link object file to generate the executable file. In this case, the file name of the default output file is the same as the code file name, except that the suffix is replaced by. O. For example, the following command generates an object file named HELLOUBUNTU.O: $ gcc-c-wall helloubuntu.c

Option-O can be used to specify the file name of the generated object file. The following command produces an object file named KUBUNTU.O: $ gcc-c-wall helloubuntu.c-o KUBUNTU.O

When building object libraries or generating a series of object files for later linking, a command can generate the corresponding object files from multiple source files. The following command generates object files UBUNTU.O, KUBUNTU.O and XUBUNTU.O: $ gcc-c-wall ubuntu.c kubuntu.c

[Edit] multiple source files to generate an executable program

Even if multiple source files are compiled, the GCC compiler will also automatically link operations. For example, the following code is saved in a file named HELLOMAIN.C and calls a function named SayHello ():

* HELLOMAIN.C * *
void SayHello (void);
int main (int argc,char *argv[])
{
SayHello ();
return 0;
}

The following code is saved in a file named SAYHELLO.C and defines the SayHello () function:

* SAYHELLO.C * *
#include <stdio.h>
void SayHello ()
{
printf ("Hello, ubuntu\n");/* Here's a small error, the quotation marks in the Chinese input method to make GCC error.
}

The following command compiles two files into object files and links them to executables Hello and deletes object files: $ gcc-wall hellomain.c sayhello.c-o Hello

[edit] compilation preprocessing

Option-E instructs the compiler to compile preprocessing only. The following command helloubuntu.c the source file and lists the results in standard output: $ gcc-e helloubuntu.c

Option-O is used to direct preprocessed code to a file. As given in the suffix list given in the beginning of this article, the C source file, which is not preprocessed, is saved as a. i file, which can be obtained by: $ gcc-e helloubuntu.c-o helloubuntu.i

[edit] Generate assembly code

Option-S instructs the compiler to generate assembly language code and then ends. The following command will be generated by the C source file helloubuntu.c assembly language file Helloubuntu.s: $ gcc-s helloubuntu.c

The form of assembly language relies on the target platform of the compiler. If multiple source files are compiled, each file will produce a corresponding assembly code module, respectively.

[edit] Create a static library

A static library is a collection of generic. o files generated by the compiler. When you link to a program, the object file in the library or the object file in the directory is the same. Another name for the static library is the archive file (archive), and the tool for managing this archive is called AR.

To build a library, you first compile the object modules that are required in the library. For example, the following two source files are hellofirst.c and HELLOSECOND.C:

* HELLOFIRST.C * *
#include <stdio.h>
void Hellofirst ()
{
printf ("The hello\n");
}

* HELLOSECOND.C * *
#include <stdio.h>
void Hellosecond ()
{
printf ("The second hello\n");
}

These two source files can be compiled into object files with the following command: $ gcc-c-wall hellofirst.c HELLOSECOND.C

Program AR fit parameter-R You can create a new library and insert the object file. If the library does not exist, parameter-R creates a new one and adds the object module (if necessary, by replacing) to the archive. The following command creates a static library named LIBHELLO.A that contains two object modules in this example: $ ar-r libhello.a HELLOFIRST.O HELLOSECOND.O

The library is now built and ready to use. The following program TWOHELLOS.C calls the two functions in the library:

* TWOHELLOS.C * *
void Hellofirst (void);
void Hellosecond (void);
int main (int argc,char *argv[])
{
Hellofirst ();
Hellosecond ();
return 0;
}

Program Twohellos can be compiled and linked by a command on the command line by specifying a library, as follows: $ gcc-wall twohellos.c libhello.a-o Twohellos

The naming convention for static libraries is to start with a three-letter Lib and end with a suffix. A. All system libraries Use this naming convention, and it allows you to abbreviate the library name in the command line with the-l (ELL) option. The following command differs from the previous command only because GCC expects to find the library in a different location: $ gcc-wall twohellos.c-lhello-o Twohellos

Specifying a full pathname enables the compiler to look for a library in a given directory. The library name can be specified as an absolute path (such as/USR/WORKLIBS/LIBHELLO.A) or as a relative to the current directory (for example,./lib/libhello.a). Option-L cannot have the ability to specify a path, but it requires the compiler to locate the library in the System library directory.

[edit] Create a shared library

A shared library is a collection of object files that the compiler generates in a special way. All addresses (variable references or function calls) in the object file module are relative rather than absolute, which allows the shared module to be dynamically invoked and executed during the program's operation.

To build a shared library, you first compile the object modules that are required in the library. For example: The following are two source files named Shellofirst.c and SHELLOSECOND.C:

* SHELLOFIRST.C * *
#include <stdio.h>
void Shellofirst ()
{
printf ("The" the "the" the "a shared library\n");
}
* SHELLOSECOND.C * *
#include <stdio.h>
void Shellosecond ()
{
printf ("The second Hello from a shared library\n");
}

To compile the above two source files into object files, you can use the following command: $ gcc-c-wall-fpic shellofirst.c SHELLOSECOND.C

Option-C tells the compiler to generate only. O object files. Option-fpic the generated object module with a floating (relocatable) address. The micrographics pic represents "position-independent code" (position independent).

The following GCC command constructs the object file into a shared library named hello.so: $ gcc-wall-shared shellofirst.o shellosecond.o-o hello.so

Option-O is used to name the output file, and the filename suffix. So tells the compiler to link the object file into a shared library. Typically, the linker locates and uses the main () function as the entry point for the program, but this example does not have this entry in the output module, which is necessary to suppress the error option-shared.

The compiler can recognize the suffix. c file as a C language source code file and know how to compile it into an object file. Based on this, the previous two commands can be merged into one; The following command compiles the module directly and stores it as a shared library: $ gcc-wall-fpic-shared shellofirst.c shellosecond.c-o hello.so

The following program, stored in file STWOHELLOS.C, is the main program that invokes two functions in the shared library:

* STWOHELLOS.C * *
void Shellofirst (void);
void Shellosecond (void);
int main (int argc,char *argv[])
{
Shellofirst ();
Shellosecond ();
==========================================

return 0;
}

The program can compile and link shared libraries with the following command: $ gcc-wall stwohellos.c hello.so-o Stwohellos

The program Stwohello has completed, but to run it must be able to navigate to the shared library hello.so because functions in the library are loaded when the program is run. It should be noted that the current working directory may not be in the lookup path of the shared library, so you need to set the environment variable using the following command line Ld_library_path: $ export ld_library_path= $LD _library_path:./

[edit] beyond naming conventions

If your environment requires you to name your C source file with a suffix other than. C, you can specify its corresponding language by using the-X option to ignore our naming conventions. For example, the following command compiles the C language source code from a file Helloworrld.jxj and generates an executable file Helloubuntu: $ gcc-xc helloubuntu.jxj-o Helloubuntu

Typically, in the absence of the-X option, any source file name with an unknown suffix is considered an option that the connector can recognize and is passed to the linker without making any changes. Option-X works on all of the files that are later in the unknown suffix. For example, the following command enables GCC to treat both align.zzz and types.xxx as C source files: $ gcc-c-xc align.zzz types.xxx

compiling Cpp From Ubuntu Chinese

Directory Hidden

1 C + + programming related file suffix 2 single source file build executable program 3 multiple source file build executable program 4 source file generate object file 5 compile preprocessing 6 generate assembly code 7 create static Library 8 other references

[edit] related file suffixes in C + + programming

. A Static Library (archive)
. C
. C
. cc
. CP
. cpp
. cxx
. C + +
C + + Source code (requires compilation preprocessing)
. h C or the source on behalf of the dock file
. II C + + Source code (no compilation preprocessing required)
. o Object files
. s Assembly language Code
. So Dynamic Library
<none> Standard C + + system header file

[Edit] A single source file generates an executable program

Here is a simple C + + program code saved in file Helloworld.cpp:

* Helloworld.cpp * *
#include <iostream>
int main (int argc,char *argv[])
{
Std::cout << "Hello, World" << Std::endl;
return (0);
}

The program uses the cout defined in the header file iostream to write a simple string to the standard output. The code can be compiled into an executable file with the following command:

$ g++ Helloworld.cpp

Compiler g++ can be recognized as a C + + source code file by checking the suffix name of the file specified on the command line. Compiler default action: Compile source code file to generate object file, link object file and function in Libstdc++ Library to get executable program. The object file is then deleted. The compiler takes the default a.out because the file name of an executable program is not specified on the command line. Programs can run like this:

$/a.out Hello, world

It is more common to specify the file name of an executable program by using the-o option. The following command produces an executable file named HelloWorld:

$ g++ Helloworld.cpp-o HelloWorld

Enter the program name on the command line to run:

$/helloworld Hello, world

Program g++ is to set the GCC default language to a special version of C + +, which automatically uses the C + + standard library instead of the C standard library. By following the naming conventions of the source code and specifying the name of the corresponding library, it is possible to compile the linked C + + program with GCC, as shown in the following example:

$ gcc helloworld.cpp-lstdc++-o HelloWorld

Option-L (ELL) by adding prefix lib and suffix. A transforms the name following it into the name of the library LIBSTDC++.A. It then looks for the library in the standard library path. The compilation process and output files of GCC are identical to the g++.

In most systems, GCC installs a program that is C + +. If it is installed, it is equivalent to g++, as shown in the following example, and the usage is consistent:

$ c + + helloworld.cpp-o HelloWorld [edit] multiple source files to generate executable programs

If more than one source file is specified in the g++ command, they are compiled and linked into a single executable file. The following is a header file named Speak.h; it contains the definition of a class that contains only one function:

* Speak.h * *
#include <iostream>
Class Speak
{
Public
void SayHello (const char *);
};

The following is a list of the contents of the file Speak.cpp: A function body containing the SayHello () function:

* Speak.cpp * *
#include "Speak.h"
void Speak::sayhello (const char *STR)
{
Std::cout << "Hello" << str << "\ n";
}

Within the file Hellospeak.cpp is a program that uses the Speak class:

* Hellospeak.cpp * *
#include "Speak.h"
int main (int argc,char *argv[])
{
Speak Speak;
Speak.sayhello ("World");
return (0);
}

The following command compiles and links the two source files into a single executable program:

$ g++ hellospeak.cpp Speak.cpp-o Hellospeak

PS: Here's why the "speak.h" file is not mentioned in the command (the reason: "Speak.cpp" contains "#include" Speak.h "", which means that before searching the system header file directory, the file will be searched in the current directory). Speak.h ". and "Speak.h" is in the directory, no longer specified in the command.

Edit source file build object file

Option-C is used to tell the compiler to compile the source code but not to execute the link, and the output is the object file. The file default name is the same as the source file name, but the suffix is changed to. O. For example, the following command compiles the source file Hellospeak.cpp and generates the object file HELLOSPEAK.O:

$ g++-C hellospeak.cpp

The command g++ also recognizes the. o file and passes it as an input file to the linker. The following command compiles the source file as an object file and links it to a single executable program:

$ g++-C Hellospeak.cpp $ g++-C speak.cpp $ g++ hellospeak.o speak.o-o Hellospeak

Option-O is not only used to name executable files. It is also used to name other files that the compiler outputs. For example: In addition to the intermediate object file with a different name, the following command generates the exact same executable file:

$ g++-C hellospeak.cpp-o hspk1.o $ g++-C speak.cpp-o hspk2.o $ g++ hspk1.o hspk2.o-o hellospeak [edit] compilation preprocessing

Option-E enables g++ to use the source code with the compiler preprocessor to no longer perform other actions. The following commands preprocess the source file Helloworld.cpp and displays the results in standard output:

$ g++-E Helloworld.cpp

The source code for the helloworld.cpp listed earlier in this article has only six lines, and the program does nothing except display a single line of text, but the preprocessed version will exceed 1200 lines. This is mainly because the header file iostream is included and it contains other header files, and there are several definitions of classes that process input and output.

The GCC suffix for preprocessed files is. II, which can be generated by the-o option, for example:

$ gcc-e helloworld.cpp-o helloworld.ii [edit] Generate assembly code

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.