Differences between GCC and G ++ _ extern C in C ++ ""

Source: Internet
Author: User

**************************************** ******************************

**************************************** ******************************

Differences between GCC and G ++

**************************************** ******************************

Gcc/g ++ requires a total of four steps for compiling.

1. pre-process and generate the. I file [pre-processor CPP]

2. Convert the pre-processed file into an assembly language to generate the file. s [compiler egcs]

3. Generate. O files from assembly to target code (machine code) [assembler as]

4. Connect the target code to generate an executable program [linker ld]

When you invoke GCC, it normally does preprocessing, compilation, assembly and linking.

"Overall options" allow you to stop this process at an intermediate stage. For example,

-C option says not to run the linker. Then the output consists of object files output

The aggreger.

-E stop after the preprocessing stage; do not run the compiler proper. The output is in

The form of preprocessed source code, which is sent to the standard output.

-S stop after the stage of compilation proper; Do not assemble. The output is in the form

Of an attacker code file for each non-aggreger input file specified.

**************************************** ******************************

**************************************** ******************************

1.g ++ compiles both. C and. cpp as C ++ programs, while GCC compiles. cpp as a C ++ program.

In the compilation phase, G ++ calls GCC. For C ++ code, the two are equivalent, but because the GCC command cannot be automatically connected to the library used by the C ++ program, therefore, G ++ is usually used to complete the link. For the sake of unification, G ++ is used to compile/link all.

Example:

/*************************************** *******************************

* Compiler: GCC 4.5.1 20100924 (Red Hat 4.5.1-4)

* Last Update: Sun 27 May 2012 04:05:23 pm CST

* File name: Test. cpp

* Description:

**************************************** ********************************/

# Include <iostream>

Int main ()

{

STD: cout <"love" <STD: Endl;

Return 0;

}

[Test1 @ localhost tempcode] $ gcc-O test. cpp

/Tmp/cczbzjdu. O: In function 'main ':

Test. cpp :(. Text + 0x14): Undefined reference to 'std: cout'

Collect2: LD returned 1 exit status

[Test1 @ localhost tempcode] $ g ++-O test. cpp can be successful with G ++.

**************************************** ******************************

However, if you want to use GCC to link the C ++ standard library, you can do this.

[Test1 @ localhost tempcode] $ gcc-O test. cpp-lstdc ++

**************************************** ******************************

2. The _ cplusplus macro indicates that the compiler will explain the code in C or C ++ syntax.

For example, void Foo (int x, int y );

After the function is compiled by the C compiler, its name in the symbol table is Foo, while the C ++ compiler generates names such as _ z3fooii to support function overloading and secure type connection. C ++ programs cannot directly call C functions because the names in the symbol table are different after compilation. C ++ provides a C connection to exchange the specified symbol extern "C" to solve this problem.

**************************************** ******************************

**************************************** ******************************

**************************************** ******************************

Extern C "" in C ++ ""

/*************************************** *******************************

* Compiler: GCC 4.5.1 20100924 (Red Hat 4.5.1-4)

* Last Update: Sun 27 May 2012 03:21:50 pm CST

* File name: cexample. h

* Description:

**************************************** ********************************/

Extern int functest (int );

/*************************************** *******************************

* Compiler: GCC 4.5.1 20100924 (Red Hat 4.5.1-4)

* Last Update: Sun 27 May 2012 03:23:03 pm CST

* File name: cexample. c

* Description:

**************************************** ********************************/

# Include "cexample. H"

# Include <stdio. h>

Int functest (int x)

{

Printf ("% d \ n", X );

}

/*************************************** *******************************

* Compiler: GCC 4.5.1 20100924 (Red Hat 4.5.1-4)

* Last Update: Sun 27 May 2012 03:11:50 pm CST

* File name: 1.cpp

* Description:

**************************************** ********************************/

// # Include <stdio. h>

// # Ifdef _ cplusplus

// Extern "C"

//{

// # Endif

# Include "cexample. H"

// # Ifdef _ cplusplus

//}

// # Endif

Int main (INT argc, char * argv [])

{

Functest (1 );

Return 0;

}

The following uses the-s option of GCC to only activate preprocessing and compilation and compile the file into assembly code.

[Test1 @ localhost tempcode] $ gcc-s cexample. c

[Test1 @ localhost tempcode] $ gvim cexample.. s

. Globl functest

. Type
Functest, @ Function

Functest:

View the assembly code. You can see that the functest function in cexample. C is named functest in the symbol table.

You can also use the-C option to generate the target code and directly view the name of the target code in the symbol table.

The-C option says not to run the linker. Then the output consists of object files output by the specified er.

[Test1 @ localhost tempcode] $ gcc-C cexample. C // generate the target file

Use objdump-display information from object files.

-T print the symbol table entries of the file.

[Test1 @ localhost tempcode] $ objdump-T cexample. o

Cexample. O: File Format elf32-i386

Symbol table:

00000000 l DF * ABS * 00000000 cexample. c

00000000g f. Text 0000001c functest

00000000 * und * 00000000 printf

[Test1 @ localhost tempcode] $ g ++-O 1 1.cpp cexample. o

/Tmp/ccaodc5a. O: In function 'main ':

1. cpp :(. Text + 0x11): Undefined reference to 'functiontest (INT )'

Collect2: LD returned 1 exit status

Generate the target Code of 1. cpp separately for viewing.

[Test1 @ localhost tempcode] $ g ++-C 1.cpp

[Test1 @ localhost tempcode] $ objdump-T 1.o

1. O: File Format elf32-i386

Symbol table:

00000000 l DF * ABS * 00000000 1.cpp

00000000g f. Text 0000001c main

00000000 * und * 00000000 _ z8functesti

The name of functest in the 1. O symbol table is _ z8functesti, which is different from the name of functest in the cexample. O symbol table. Therefore, the link fails.

Below is 1. add extern "C" to CPP, so that the C ++ compiler will generate the target code for the items included in extern "C" {} according to the C compiler, in this way, the function name will not appear in. the name and. the name in the C target code is different. _ Cplusplus: the macro indicates that the compiler will explain the code in C or C ++ syntax.

/*************************************** *******************************

* Compiler: GCC 4.5.1 20100924 (Red Hat 4.5.1-4)

* Last Update: Sun 27 May 2012 03:11:50 pm CST

* File name: 1.cpp

* Description:

**************************************** ********************************/

# Include <stdio. h>

# Ifdef _ cplusplus

Extern "C"

{

# Endif

# Include "cexample. H"

# Ifdef _ cplusplus

}

# Endif

Int main (INT argc, char * argv [])

{

Functest (1 );

Return 0;

}

[Test1 @ localhost tempcode] $ g ++-C 1.cpp

[Test1 @ localhost tempcode] $ objdump-T 1.o

1. O: File Format elf32-i386

Symbol table:

00000000g f. text0000001c main

00000000 * und * 00000000 functest

After compilation, you can pass.

[Test1 @ localhost tempcode] $ g ++-O 1 1.cpp cexample. o

[Test1 @ localhost tempcode] $./1

1

**************************************** ******************************

If you directly use GCC to compile the link between 1.cpp and cexample. C, an error occurs. As mentioned above, GCC treats 1. cpp as a C ++ program and treats. C as a C program. Therefore, if the function name in the generated symbol table is different, an error occurs when linking.

[Test1 @ localhost tempcode] $ gcc-O 1 1.cpp cexample. c

/Tmp/ccswh6xc. O: In function 'main ':

1. cpp :(. Text + 0x11): Undefined reference to 'functiontest (INT )'

Collect2: LD returned 1 exit status

However, if you use g ++ to compile the link between 1.cpp and cexample. C, no error occurs, because both. C,. cpp, and g ++ are treated as C ++ programs.

[Test1 @ localhost tempcode] $ g ++-O 1 1.cpp cexample. c

**************************************** ******************************

**************************************** ******************************

Why does the C ++ program not need to add extern C to call the C standard library ""

Because the C ++ program calls the C standard library, taking this into account, in the C header file (. h header file) has been specially processed, that is, when compiling the C ++ program, the C calling mechanism (extern "C") is used for the functions declared in these C header files "), instead of using the default C ++ call mechanism, this prevents the C ++ function call mechanism from failing to find the corresponding function in the C library.

**************************************** ******************************

Reference: G ++

Based on the GCC compiler, first prepare some knowledge about C and C ++:

Differences between GCC and G ++

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.