directory
- A. __cplusplus profile
- 2. __cplusplus is used
- Guess you like it
C/C++ learning Directory >> C language basics
A. __cplusplus profile
__cplusplus and extern “C” are usually used in pairs, and we often see the __cplusplus keyword when reading programs, as in this code:
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language __cplusplus //@Time:2021/07/14 07:30 //@Motto: No accumulation of small steps without a thousand miles, no accumulation of small streams without rivers and oceans, the wonderful life of the program needs to be unremitting accumulation! /******************************************************************************************/ #ifndef __CODERSRC_H__ #define __CODERSRC_H__ #ifdef __cplusplus #define __dersrc_h__ #ifdef __cplusplus Instead of C + + extern "C" {# endif / *... * do something here *................................. */ #ifdef __cplusplus } #endif #endif /*end of __CODERSRC_H__*/Copy the code
The “# ifNdef __CODERSRC_H**, #define __CODERSRC_H**, #endif” macros are used to prevent the header from being referenced repeatedly.
It also tells the compiler that if __cplusplus is defined (CPP files define this macro by default), it should be compiled in C (that is, calling C in C ++).
Important note: this is mandatory if C++ is calling a third-party library written in CExtern “C”Tell the compiler to compile in C mode, otherwise there will be compilation error problem;
2. __cplusplus is used
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language __cplusplus //@Time:2021/07/14 07:30 //@Motto: No accumulation of small steps without a thousand miles, no accumulation of small streams without rivers and oceans, the wonderful life of the program needs to be unremitting accumulation! /******************************************************************************************/ #ifdef __cplusplus extern "C" { #endif void *memset(void* ,int , size_t); #ifdef __cplusplus } #endifCopy the code
Since C and C++ are different, it would be too much trouble to define two sets of header files in order to implement a program that is compatible with both C and C++, so __cplusplus is unique to C++. __cplusplus is actually C++. This is where the first code above is used. If this code appears in a C++ file, it will be compiled to read:
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language __cplusplus //@Time:2021/07/14 07:30 //@Motto: No accumulation of small steps without a thousand miles, no accumulation of small streams without rivers and oceans, the wonderful life of the program needs to be unremitting accumulation! / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / * * * * * * * * * * conditional compilation in C + + files after the * * * * * * * * * * * * * * * / extern "C" {void * memset (void *, int, size_t); } in the C file, after a conditional compilation, this code becomes: / * * * * * * * * * * C file conditional compilation results after * * * * * * * * * * * * * / void * memset (void *, int, size_t);Copy the code
Guess you like it
- C array subscript out of bounds and memory overflow difference
- C language pointer declaration and definition
- P ++ / p –
- The C languagep++/§ + + / _ (p++) / _p + +
- C language uses Pointers to iterate over groups of numbers
- C language pointer and array difference
- C language pointer array and array pointer difference
- C NULL pointer
- C void Pointers
- C language field pointer
- C function value passing and address passing
- Default parameter of C language function
- C language function variable parameter
- C function pointer
- C language pointer function
- C language callback function callback
- C typedef
- C defines constants
- C define prevents repeated inclusion of header files
- Pragma once
- C language #include <> is different from #include
- C const decorates a variable
- C const decorates Pointers
- C const modifier functions
- C const decorates function arguments
- C const is different from define
- C # operator
- C language ## operator
- Extern “C”
- C language __cplusplus
C language __cplusplus
This article is published by the blog – Ape Say Programming Ape Say programming!