C/C + + extern "C."

Source: Internet
Author: User

C/C + + extern "C."

Common styles

extern "C" {...} extern "C" Return-type func-name (type, type) {}extern "C" return-type func-name (type, type); Meaning: in C + +, the source file is compiled in C language.
Example//t_extern_yes.cpp
    1. extern "C"
    2. {
    3. int f(void)
    4. {
    5. return 0;
    6. }
    7. }
T_extern_non . cpp
    1. int f(void)
    2. {
    3. return 0;
    4. }
Assembly
    1. GCC T_extern_non. cpp T_extern_yes. cpp
    2. Or
    3. GCC T_extern_non. cpp t_extern_yes. cpp
Get
    1. .file "T_extern_non.cpp"
    2. . Text
    3. . Align 2
    4. . Globl _Z1FV
    5. . Type _ Z1FV, @function
    6. _Z1FV:
    7. . LFB2:
    8. pushl%ebp
    9. . LCFI0:
    10. movl%esp,%ebp
    11. . LCFI1:
    12. movl $,%eax
    13. popl%ebp
    14. ret
    15. . LFE2:
    16. . Globl __gxx_personality_v0
    17. . Ident "GCC: (GNU) 4.2.1 (SUSE Linux)"
    18. . Section. Note . Gnu-stack, "", @progbits
    1. .file "T_extern_yes.cpp"
    2. . Text
    3. . Align 2
    4. . globl F
    5. . Type F, @ function
    6. f:
    7. . LFB2:
    8. pushl%ebp
    9. . LCFI0:
    10. movl%esp,%ebp
    11. . LCFI1:
    12. movl $,%eax
    13. popl%ebp
    14. ret
    15. . LFE2:
    16. . Globl __gxx_personality_v0
    17. . Ident "GCC: (GNU) 4.2.1 (SUSE Linux)"
    18. . Section. Note. Gnu-stack, "", @progbits
When using Imdiff to see only the above red flag is different, this is the function name compiled id,c++ enriched the content. If you use extern "C", tell the C + + compiler to compile with C, so you can connect the original C file compiled with C. Now simulates a C + + call to the library file.

C + + Call Library

//T_ADD.C
    1. int add(int a, int b){
    2. Return (a+b);
    3. }
Get the static library file for C T_ADD.O
    1. Gcc-c T_ADD.C
Original C-header file
    1. #ifndef t_add_c__
    2. #define T_ADD_C__
    3. int add(int a, int b);
    4. #endif
Modified to be a header file that can be used by C + +//t_add.h
    1. #ifndef t_add_c__
    2. #define T_ADD_C__
    3. extern "C" int add(int a, int b);
    4. #endif
Can be referenced by C + + files, such as//main.cpp
  1. #include <stdio. h>
  2. #include "T_add.h"
  3. int main(void)
  4. {
  5. printf("1 + 2 =%d\n", add(1, 2));
  6. return 0;
  7. }
Can compile the
    1. G++ t_add. o main. C
output
  1. 1 + 2 = 3
 
How to use
Of course, the extern "C" way various,
  1. Before the function, you can
  2. File start end {}hold Live,
  3. can also be convenient in #inldue语句加, such as
    1. extern "C" {
    2. #include "T_add.h"
    3. }
In addition, C + + Transformers will have __cplusplus macro definitions, are generally used in conjunction with

C/C + + extern "C."

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.