In the C language, the modifier extern is used before the declaration of a variable or function to illustrate that "this variable/function is defined elsewhere." To be referenced here. (extern can be placed before a variable or function to indicate the definition of a variable or function in another file, prompting the compiler to find its definition in other modules when encountering this variable and func
1 Invocation Scenario DescriptionIn the previous article on the extern "C" principle and usage, it explains in detail why extern "C" is required and how it can be used to solve the problems encountered in mixed C and C + + programming. Next, use the example validation method to verify that the C and C + + functions are named in various cases when the function is compiled and linked with the
# Include can contain declarations of variables and functions in other header files. Why do I need the extern keyword? If I want to reference a global variable or function, I only need to include # include
Header file
First, let's talk about the header file. In fact, the header file has no effect on the computer. She just expanded it at # include during pre-compilation, which makes no difference, in fact, the header file is mainly for others.
I did a
assumes that it isc++ 's function was compiled successfully, but when you connect you will find a lovely error. The solution is to specify that it is a C function: extern "C" function description NBSP; Specify a group of functions: extern "C" {} If you want C and C + + mixed words: #ifdef _cplusplus #endif n function description NBSP; #ifdef _cplusplus}NBSP; #endif e
1. Basic explanation
Extern can be placed before a variable or function to indicate that the definition of a variable or function is in another file, prompting the compiler to find its definition in other modules when it encounters this variable or function.
In addition, extern can be used to specify links.
2 problem: extern variable
An array is define
The role of extern "c" and the role of extern
Function: Mixed Programming of C and C ++.
Principle: After the C and C ++ compilers compile, the function name will be compiled into different names. The target cannot be found in the Link stage name search, which will be explained in the following examples.
Usage: ①. Function defined in file c ,. when the cpp file is to be called. use
When reading svm. h, we found the following code:
#ifndef _LIBSVM_H#define _LIBSVM_H#define LIBSVM_VERSION 317#ifdef __cplusplusextern "C" {#endifApparently, the first three lines of code are intended to prevent repeated references to header files. What do 5-6 lines mean?
An enterprise once gave the following interview question: why do standard header files have a structure similar to the following?
#ifndef __INCvxWorksh#define __INCvxWorksh #ifdef __cplusplusextern "C" {#endif /*...*/ #ifdef
, its survival time is changed. After changing a global variable to a static variable, it changes its scope and limits its scope of use.
What is the difference between a static function and a common function?
Only functions used in the current source file should be declared as internal functions (static). Internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, it should be described in a header file that the
1. Basic explanationExtern can be placed before a variable or function to indicate that the definition of a variable or function is in another file, prompting the compiler to find its definition in other modules when it encounters this variable or function.In addition, extern can be used to specify links. 2 problem: extern variableAn array is defined in a source file:
Example:Two files:C file: C. c
**************************************** *******
Int external = 5; // global variable. The default value is extern.Int func () // global function. The default value is extern.{Return external;}
**************************************** *******
Cpp file: CPP. cpp
**************************************** *******
# Include "iostream"Using namespace std;
# Ifdef _ cplusplusExtern "C"{# EndifExtern int external; // tell
Static is a commonly used modifier in C + +, which is used to control the storage and visibility of variables. extern, "C" is a means to enable C + + to invoke C writing library files, if you want to prompt the compiler to use C to handle functions, then use extern "C" to explain.
The static keyword in a. C language
In the C language, static can be used to modify local variables, global variables, and funct
Recently learning Lua, about LUA compiled place, because Lua is written in C, C language in C + + functions, need to use the extern "C" compile before, seemingly did not touch this knowledge, write a blog to commemorate, by the way the role of the extern keyword.I. The role of extern "C"This is the first time I've seen it, because I've never had C + + to callC's
The use of extern and static in C ++ is confusing. The following is a summary: 1. the global variable definition is used to modify the global variable definition. The global variable definition is used to expand the scope of the global variable, other files can also use this global variable static to modify the definition of the global variable to limit that this global variable can only be used in this file. after the CPP file is compiled, it becomes
Extern "C" contains a double meaning, which can be obtained literally: first, the target is "extern", and second, the target is "C.First, let's take a look.:A. in a file, if an external variable is not defined at the beginning of the file, its valid range is limited to the end of the definition. If you need to reference this variable before the definition, you need to use the keyword "
connect to them, you will find a very cute error.The solution is to specify it as a C function:Extern "C" Function DescriptionIf you specify a group of functions:Extern "C "{N function descriptions}If you want to mix C and C ++:# Ifdef _ cplusplusExtern "C "{# EndifN function descriptions# Ifdef _ cplusplus}# Endif
Extern "C" indicates that the internal Symbol names generated by Compilation Use the c Convention.
C ++ supports function overloading, bu
and type information, C + + is this mechanism to implement function overloading. Here's an example of how to use C's function in C + +,Or, you use C + + functions in C.Examples of C + + reference functionsTEST.c#include void MyTest (){printf ("MyTest in. c file ok\n");}Main.cppextern "C"{void MyTest ();}int main (){MyTest ();return 0;}To reference C + + functions in CIn C, when referencing functions and variables in the C + + language, a C + + function or variable is declared in the
The C language often appears in the file duplicate contains and causes the global variable to duplicate the definition, below an example illustrates
-----Makefile
CC = gcc
objs = main.o errhandle.o strcopy.o
cflags =-wall-std=c99
main: ${objs} # or $ (OBJS), ${OBJS} i s shell style
${cc} ${cflags}-o $@ ${objs}
main.o:main.c errhandle.h strcopy.h
${cc} ${cflags}-C main.c
errhandle.o:errhandle.c errhandle.h
${CC} ${cflags}-C errhandle.c strcopy.o:strcopy.c strcopy.h
Errhandle.h
${CC} ${
The intent of extern "C" in C + + is to implement mixed programming between C + + and C and other languages.C + + supports function overloading, whereas programming language C is not supported. Functions are compiled in C + + with different names in the symbol library than in the C language.For example, suppose a function is prototyped as: void int int y) The function is compiled by the C compiler and is named _foo in the symbol library.The C+ + compi
In the C language, the modifier extern is used before the declaration of a variable or function to illustrate that "this variable/function is defined elsewhere and is referenced here." 1. Declarations of extern-modified variables. For example, if the file a.c needs to refer to the variable int v in B.C, you can declare the extern int v in A.C, and then you can r
STACK.O, which contains the. Data segment of variables A and Stack,main also incorporates the. Data segment of the MAIN.O and STACK.O, which contains the variables B and top,main. Text segment merges MAIN.O and S TACK.O. Text segment that contains the definitions of each function. As shown in the following:If you put STACK.O in front of MAIN.O at compile time, that is: gcc stack.o main.o-o Main, the executable file from the MAIN.O variable or function in each paragraph of main is discharged to
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.