extern What is the role?

Category: C/C++ -> C++ Author: s1169931417 Date: 2009-06-15 10:52:32
 
s1169931417
2009-06-15 10:52:32
A basic explanation

extern variable or function may be placed prior to mark a variable or function is defined in another file, prompting the compiler encounters this variable and function in other modules to find its definition.

addition, extern can also be used to link specified.

2 questions: extern variable

in a source file defines an array:

char a [6];

in another file a statement with the following statement:

extern char * a;

ask, so you can do ?

answers and analysis:

1), can not, the program runs will tell you when unauthorized access. The reason is that the pointer points to the type T is not equivalent to an array of type T. extern char * a statement is a pointer variable instead of an array of characters, so different from the actual definition, resulting runtime unauthorized access. Statement should be changed to extern char a [].

2), examples are as follows, if a [] = "abcd", then the external variables a = 0x61626364 (abcd in ASCII code value), * a clearly does not make sense, as shown below:

obviously a point of space (0x61626364) does not make sense, prone to illegal memory access.

3), which reminds us that, when using the extern declaration should strictly correspond to the format of the actual programming, such errors are common.

4), extern variable declaration is often used in such an action, you are in *. c file declares a global variable, the global If the variable is referenced, it is placed in *. h and treated with extern to declare.

3 questions: extern function 1

often common in the function extern The front part of a function declaration, then, C language keyword extern declaration in the function what role?

answers and analysis:

If the function declaration with extern keyword , just a hint of this function may be defined in another source file, no other effect. The following two function declarations that no significant difference:

extern int f (); and int f ();

course, such use is still there, is replaced in the program include "*. h" to declare a function in some complex projects, I more accustomed to all the extern modifier added before the function declaration.

cfy_meiyuan
2009-06-15 11:01:17
often among the cpp code to see this code:

# ifdef __ cplusplus
extern "C" {
# endif

/ / some code

# ifdef __ cplusplus
}
# endif
such a code in the end what does that mean? First, __cplusplus is cpp The custom macros, this macro is defined as saying that this is a cpp code, that is, the meaning of the above code is: If this is a cpp code, then adding extern " C "{and} handling the code.

To understand why to use extern "C", but also from the function in cpp overloaded treatment to begin with. In c + +, in order to support overloading mechanism assembly code generated by the compiler, the name of the function you want to do some processing, such as adding the function's return type, etc., while in C, but simply a function name only, not add other information that is: C + + and C function name on the resulting treatment is not the same.

a simple example, the following function, we look to join and do not Add extern "C" what are generated assembly code changes:

int f (void)
{
return 1;
}
adding extern "C" when the assembly code generated is:

. file "test.cxx"
. text
. align 2
. globl _f
. def ; _f;. scl 2;. type 32;. endef
_f:
pushl% ebp
movl ;% esp,% ebp
movl $ 1,% eax
popl% ebp
ret
but not to join the extern "C" after

. file "test.cxx"
. text
. align ; 2
. globl __ Z1fv
. def __ Z1fv;. scl 2;. type 32;. endef
__Z1fv:
pushl% ebp
movl% esp,% ebp
movl $ 1,% eax
popl% ebp
ret
same two assembly code using gcc-S commands are generated, all places are the same, only exception is generated function name, one is _f, one is __Z1fv.
join or not to join to understand extern "C" after the impact on the function name, we continue our discussion: Why do I need to use extern "C" it? C + +, the father in the design of C + +, taking into account time already exist a lot of C code, in order to support the original C code and have written C library, you need to C + +, as much as possible to support C, while the extern "C" is one strategy.

Imagine this situation: a library written in C and has been running very well, this time we need to use the library, but we need to use C + + to write the new code. If the code uses the C + + way links This C library file, then there will be a link error, we look at a code: First, we use the C's approach to write a function, that function was assumed that this written in C:

/ / f1.c
extern "C"
{
void f1 ()
{
return;
}
}
compile command is: gcc-c f1.c-o f1.o produce a library file called f1.o. F1 to write a piece of code to call this function:

/ / test.cxx
/ / This function extern indicates f1 defined in other places, so you can By
/ / compile, but the link still need time
/ / link the original library file.
extern void f1 ( );

int main ()
{
f1 ();

return 0;
}
through gcc-c test.cxx-o test.o test.o generate a file called. We then use gcc test.o f1.o to link the two files, but wrong, wrong tips are:

test.o (. Text + 0x1f): test.cxx: undefine reference to 'f1 ()'
That is, at compile time test.cxx compiler using C + + way to deal with f1 ( ) function, but in fact link the library file is way to deal with C functions, so there will be links to make life difficult for the error: because the linker can not find the function.

Therefore, in order to call C + + code written in C library file, you need to use extern "C" to tell the compiler: This is a library written in C Please use the C way to link them.

For example, we now have a C library file, its header file fh, generate the lib file is f.lib, so if we want to use this in C + + library files We need to write:

extern "C"
{
# include "f.h"
}
back to the question above, if you want to correct link errors, we need to be rewritten like this test.cxx:

extern "C"
{
extern void f1 ();
}

int main ()
{
f1 ();

return 0;
}
recompile and link can be passed.

summary

C and C + + for function The approach is different. extern "C" is to be able to call C C + + library file writing as a means, if you want to use the C compiler hints ways to handle functions, then we should use extern " C "to illustrate.

shh01
2009-06-15 11:18:43
mark a variable or function is defined in another file, prompting the compiler encounters this variable and function in other module to find its definition

such a basic question, reading good
rogersu2000
2009-06-15 11:25:27
tells the compiler to allow the connector to me
biantaibingmao
2009-06-15 11:33:30
statement, no allocation of space.
need to be defined.
zhen_99
2009-06-15 11:39:19
landlord or buy the "c + + primer", and then to the book indexing table extern find it. Almost entirely in that the.
meibao131719
2009-06-15 11:50:32
google lot about
lanhulan
2009-06-15 11:52:38
tells the compiler that the guy is defined in other files.
lray2
2009-06-15 11:59:26
extern tells the compiler that this variable or this function is there, you can not find the short-lived fad is normal, and later, when you will find links to the
zhaome
2009-06-15 12:07:39

more detailed
lcpyh
2009-06-15 12:22:06


1 Basic explanation
extern variable or function may be placed prior to mark a variable or function is defined in another file , suggesting that the compiler encounters this variable and function in other modules to find its definition.
addition, extern can also be used to link specified.

2 questions: extern variable
in a source file defines an array: char a [6];
in another file with the following statements were declaration: extern char * a;
ask, so you can do?
answers and analysis:
1), can not, will tell you when the program is running unauthorized access. The reason is that the pointer points to the type T is not equivalent to an array of type T. extern char * a statement is a pointer variable instead of an array of characters, so different from the actual definition, resulting runtime unauthorized access. Statement should be changed to extern char a [].
2), examples are as follows, if a [] = "abcd", then the external variables a = 0x61626364 (abcd ASCII value), * a clearly pointless
apparently a pointed space (0x61626364) does not make sense, prone to illegal memory access.
3), which reminds us that, when using the extern declaration should strictly correspond to the format of the actual programming, such errors are common.
4), extern variable declaration is often used in such an action, you are in *. c file declares a global variable, the global variable if you want to be quoted, is placed *. h and treated with extern to declare.

4 questions: extern function 2
When the function provided unilaterally modify the function prototype, if you continue to use the original consumer knowledge of an extern declaration, so the compiler will not compile error. However, during operation, because the less or more input parameters, often according to a system error, this situation should be how to solve? Analysis
Answer:
the industry for the processing of this case is not a perfect solution, the usual practice is to provide a side xxx_pub.h in their external interface is provided in the statement, then the caller include the header file, thereby eliminating the need extern this step. To avoid this error.
a double-edged sword, for extern applications, on different occasions should choose a different approach.

5 questions: extern "C"
in C + + environment using C function, often appear obj module compiler can not find the C function definitions, resulting link failure situation, it should be how to resolve this situation?

answers and analysis:
C + + language at compile time polymorphic function in order to solve the problem, will unite the function name and parameter name of the function to generate an intermediate, while the C language does not so will cause the corresponding function can not find the link case, then you need to use a C function extern "C" to link specified, which tells the compiler, please keep my name, do not give me the link for the middle generation function name.
Below is a standard way:
/ / in the. h file head
# ifdef __ cplusplus
# if __ cplusplus
extern "C" {
# endif
# endif / * __ cplusplus * /
...
...
/ /. h file ends where
# ifdef __ cplusplus
# if __ cplusplus
}
# endif
# endif / * __ cplusplus * /

3 Problem: extern extern function 1
often common in the function of the front part of a function declaration, then, C language keyword extern declaration in the function what role?
answers and analysis:
If the function declaration with the keyword extern, just a hint of this function may be defined in another source file, no other effect. The following two function declarations that no significant difference:
extern int f (); and int f ();
course, such use is still there, is in the process replace include "*. h" to declare a function, in some complex projects, I am more accustomed to all added before the function declaration extern modifier.
wanghx627
2009-06-15 12:40:18

4), extern variable declaration is often used in such an action, you are in *. c file declares a global variable, the global variable if you want to is used, you put *. h and treated with extern to declare. Can you explain

Kazakhstan?
urgently Kazakhstan
hh403632609
2009-06-15 12:49:20
link indicator extern C
if a programmer wants to invoke other programming languages, especially C function is called to write a function that must
tell compiler uses different requirements such as when this function is called when the function name or parameter order may be arranged either
different C + + function call it still functions written in other languages ​​programmers call it
link Indicator linkage directive tells the compiler that the function is the use of other programming languages ​​
link indicator prepared in two forms either in the form of a single statement single statement can also be combined complex
statements compound statement form
/ / statement in the form of a single link indicator
extern "C" void exit (int);
/ / compound statement in the form of link indicator
extern "C" {
int printf (const char * ...);
int scanf ( ; const char * ...);
}
/ / compound statement in the form of link indicator
extern "C" {
# include <cmath>
}
link indicators from the first form of extern keyword followed by a string constant and a normal function
statements constitute While function is written in another language, but it still needs to call the compiler type checking such
checks passed to the function exit () argument whether the type is int or can be implicitly converted to int type
Multiple function declarations can use curly braces indicator compound statement included in the link is the link indicator in this second form
style tricks number is used as separator indicates that the link indicator on the application in which the statement of other The sense of being ignored
braces braces slightly so declared in the function name is visible if External functions are declared outside the compound statement as
example, in the previous example compound statement extern "C" indicates the function printf () and scanf () is written in the C language
function so the significance of this statement as printf () and scanf () is extern "C" compound statement Like
declared outside
link indicator when the compound statement contains # include parentheses in the header file when the function declarations are assumed to be linked with
programming language indicator In the previous example written in the header file <cmath> functions are declared in C
function
link indicator does not appear in the body of the function in the following code segment will cause a compilation error
int main ()
{
/ / error: Link indicator does not appear within a function
extern "C" double sqrt (double ;);
305 Chapter VII function
double getValue (); / / ok
double result = sqrt (getValue ()) ;
/ / ...
return 0;
}
If the link indicator will move to function in vitro program compiles without errors
extern "C" ; double sqrt (double);
int main ()
{
double getValue (); / / ok
double result = sqrt (getValue ());
/ / ...
return 0;
}
but the link indicator on the header file There are more appropriate function declaration describes the function interface belongs
If we want C + + functions can be used as a C program how should we do it we can also use extern "C"
link indicator to make C + + functions are available for the C program example
/ / function calc () can be called a C program
extern "C" double calc (double dparm) {/ * ... * /}
If a function is declared in the same file more than once, the link indicator can appear in each statement it
function can only appear in the first statement in this case the second and subsequent statements will accept the first link in an acoustic indicator
clear link specified rules such as
/ / ---- myMath.h ----
extern "C" double calc (double);
/ / - --- myMath.C ----
/ / in Math.h in calc () statement
# include "myMath.h"
/ / define the extern "C" calc () function
/ / calc () can be called from a C program
double calc (double dparm) {/ / ...
In this section, we only see a link for the C language provides instructions extern "C" extern "C" is the only guarantee is
consists of all C + + implementations are supported by each compiler can achieve their common language environment to provide additional links
indicates, for example extern "Ada" can be used Ada language is used to declare functions written in extern "FORTRAN" statement is used to
functions written in FORTRAN, etc. because other links with specific implementation instructions differ
It is recommended that readers see the compiler's user guide for further information on other link indicator
cfy_chemi
2009-06-15 12:52:57
4.extern "C" idiom

(1) in references in C + + C language functions and variables in C language header file contains the (assumed to cExample.h), the need to carry out the following process:

extern "C"
{
# include "cExample.h"
}

while in C language header file, you can specify only its external function as extern type, C language does not Support extern "C" statement in the. c file contains the extern "C" syntax error occurs when the compiler.

I write a C + + C function examples cited included three works document the source code is as follows:

/ * c language header file: cExample.h * /
# ifndef C_EXAMPLE_H
# define C_EXAMPLE_H
extern int add (int x, int y);
# endif
/ * c language file: cExample.c * /
# include "cExample.h"
int add (int x, int y)
{
return x + y;
}
/ / c + + implementation file, invoke add: cppFile.cpp
extern "C"
{
# include "cExample.h"
}
int main (int argc, char * argv [])
{
add (2,3);
return 0;
}

if C + + calls a C language. DLL, when included. DLL's head document or declaration interface functions should be added extern "C" {}.

(2) in C, C + + language reference functions and variables, C + + header files need to add extern "C", but in the C language can not directly reference declared extern " ; C "in the header file, you should file will only C defined in C + + extern" C "functions declared as extern type.
I write C + + function C reference example project contains three files of source code is as follows:

/ / C + + header files cppExample.h
# ifndef CPP_EXAMPLE_H
# define CPP_EXAMPLE_H
extern "C" int add (int x, int y);
# endif
/ / C + + implementation file cppExample.cpp
# include "cppExample.h"
int add (int x, int y)
{
return x + y;
}
/ * C implementation file cFile.c
/ * This will compile errors: # include "cExample . h "* /
extern int add (int x, int y);
int main (int argc, char * ; argv [])
{
add (2, 3);
return 0;
}