c语言extern与 重复定义以及common defination_extern 重复定义-程序员宅基地

技术标签: c/c++/objc  

http://stackoverflow.com/questions/1433204/how-do-i-share-a-variable-between-source-files-in-c-with-extern-but-how

Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I know that global variables in C sometimes have the extern keyword. What is an extern variable? What is the declaration like? What is its scope?

This is related to sharing variables across source files, but how does that work precisely? Where do I use extern?

share | improve this question
 
5  
Yes, probably, but it is also a legitimate question, and one for which the correct answer (hopefully mine as one of them) will help the asker to understand how to use C correctly and/or better.–  Jonathan Leffler Sep 16 '09 at 14:49
2  
See also stackoverflow.com/questions/683180/is-this-extern-harmless There may also be other relevant questions - there are a number of 'close but different enough not to count' questions that show up with a (SO) search on 'c extern' (typically, dealing with functions rather than variables).–  Jonathan Leffler Sep 16 '09 at 15:05
 
@Jonathan: Here's the one I linked to: stackoverflow.com/questions/1410563–  sbi Sep 17 '09 at 14:56
 
@sbi: that covers one of the topics (declaration vs definition) nicely; it doesn't cover the extern issues as clearly as this question warrants. Another related Q - but not a duplicate (and I don't think you were proposing it as a duplicate).–  Jonathan Leffler Sep 17 '09 at 18:11
add comment

12 Answers

Using extern is only of relevance when the program you're building consists of multiple source files linked together, where some of the variables defined, for example, in source file file1.c need to be referenced in other source files, such as file2.c.

It is important to understand the difference between defining a variable and declaring a variable:

  • A variable is defined when the compiler allocates the storage for the variable.
  • A variable is declared when the compiler is informed that a variable exists (and this is its type); it does not allocate the storage for the variable at that point.

You may declare a variable multiple times (though once is sufficient); you may only define it once within a given scope.

Best way to declare and define global variables

Although there are other ways of doing it, the clean, reliable way to declare and define global variables is to use a header file file3.h to contain an extern declaration of the variable. The header is included by the one source file that defines the variable and by all the source files that reference the variable. For each program, one source file (and only one source file) defines the variable. Similarly, one header file (and only one header file) should declare the variable.

file3.h

extern int global_variable;  /* Declaration of the variable */

file1.c

#include "file3.h"  /* Declaration made available here */

/* Variable defined here */
int global_variable = 37;    /* Definition checked against declaration */

int increment(void) { return global_variable++; }

file2.c

#include "file3.h"
#include <stdio.h>

void use_it(void)
{
    printf("Global variable: %d\n", global_variable++);
}

That's the best way to use them.

Guidelines

Rules to be broken by experts only, and only with good reason:

  • A header file only contains extern declarations of variables - never static or unqualified variable definitions.
  • For any given variable, only one header file declares it (SPOT - Single Point of Truth).
  • A source file never contains extern declarations of variables - source files always include the (sole) header that declares them.
  • For any given variable, exactly one source file defines the variable, preferably initializing it too. (Although there is no need to initialize explicitly to zero, it does no harm and can do some good, because there can be only one initialized definition of a particular global variable in a program).
  • The source file that defines the variable also includes the header to ensure that the definition and the declaration are consistent.
  • A function should never need to declare a variable using extern.
  • Avoid global variables whenever possible - use functions instead.

Not so good way to define global variables

With some (indeed, many) C compilers, you can get away with what's called a 'common' definition of a variable too. 'Common', here, refers to a technique used in Fortran for sharing variables between source files, using a (possibly named) COMMON block. What happens here is that each of a number of files provides a tentative definition of the variable. As long as no more than one file provides an initialized definition, then the various files end up sharing a common single definition of the variable:

file10.c

int i;   /* Do not do this in portable code */

void inc(void) { i++; }

file11.c

int i;   /* Do not do this in portable code */

void dec(void) { i--; }

file12.c

int i = 9;   /* Do not do this in portable code */

void put(void) { printf("i = %d\n", i); }

This technique does not conform to the letter of the C standard and the 'one definition rule', but the C standard lists it as a common variation on its one definition rule.Because this technique is not always supported, it is best to avoid using it, especially if your code needs to be portable. Using this technique, you can also end up with unintentional type punning. If one of the files declared i as a double instead of as an int, C's type-unsafe linkers probably would not spot the mismatch. If you're on a machine with 64-bit int and double, you'd not even get a warning; on a machine with 32-bit int and 64-bit double, you'd probably get a warning about the different sizes - the linker would use the largest size, exactly as a Fortran program would take the largest size of any common blocks.

This is mentioned in the C standard in informative Annex J as a common extension:

J.5.11 Multiple external definitions

There may be more than one external definition for the identifier of an object, with or without the explicit use of the keyword extern; if the definitions disagree, or more than one is initialized, the behavior is undefined (6.9.2).

Warning

As noted in comments here, and as stated in my answer to a similar question, using multiple definitions for a global variable leads to undefined behaviour, which is the standard's way of saying "anything could happen". One of the things that can happen is that the program behaves as you expect; and J.5.11 says, approximately, "you might be lucky more often than you deserve". But a program that relies on multiple definitions of an extern variable - with or without the explicit 'extern' keyword - is not a strictly conforming program and not guaranteed to work everywhere. Equivalently: it contains a bug which may or may not show itself.

Violating the guidelines

Note 1: if the header defines the variable without the extern keyword:

faulty_header.h

int some_var;    /* Do not do this in a header!!! */

Then each file that includes the header creates a tentative definition of the variable.

Note 2: if the header defines and initializes the variable, then only one source file in a given program can use the header:

broken_header.h

int some_var = 13;    /* Only one source file in a program can use this */

Note 3: if the header defines a static variable (with or without initialization), then each source file ends up with its own private version of the 'global' variable.

seldom_correct.h

static int hidden_global = 3;   /* Each source file gets its own copy  */

When the variable is actually a complex array, this can lead to extreme duplication of code. It can, very occasionally, be a sensible way to achieve some effect, but that is rather unusual.


Use the header technique I showed first. It works reliably and everywhere. Note, in particular, that the header declaring the global_variable is included in every file that uses it - including the one that defines it. This ensures that everything is self-consistent.

Similar concerns arise with declaring and defining functions - analogous rules apply. But the question was about variables specifically, so I've kept the answer to variables only.

End of Original Answer


Late Major Addition

Avoiding Code Duplication

One concern that is sometimes (and legitimately) raised about the'declarations in headers, definitions in source' mechanism describedhere is that there are two files to be kept synchronized - the headerand the source. This is usually followed up with an observation that amacro can be used so that the header serves double duty - normallydeclaring the variables, but when a specific macro is set before theheader is included, it defines the variables instead.

Another concern can be that the variables need to be defined in each ofa number of 'main programs'. This is normally a spurious concern; youcan simply introduce a C source file to define the variables and linkthe object file produced with each of the programs.

A typical scheme works like this, using the original global variableillustrated in file3.h:

file3a.h

#ifdef DEFINE_VARIABLES
#define EXTERN /* nothing */
#else
#define EXTERN extern
#endif /* DEFINE_VARIABLES */

EXTERN int global_variable;

file1a.c

#define DEFINE_VARIABLES
#include "file3a.h"  /* Variable defined - but not initialized */

int increment(void) { return global_variable++; }

file2a.c

#include "file3a.h"
#include <stdio.h>

void use_it(void)
{
    printf("Global variable: %d\n", global_variable++);
}

The problem with this scheme as shown is that it does not provide forinitialization of the global variable. With C99 and variable argumentlists for macros, you could define macros to support initialization too.You probably need two such macros, one for simple initializers with nocommas, and a second for complex initializers containing commas:

file3b.h

#ifdef DEFINE_VARIABLES
#define EXTERN                  extern
#define INITIALIZER(...)        /* nothing */
#else
#define EXTERN                  /* nothing */
#define INITIALIZER(...)        = __VA_ARGS__
#endif /* DEFINE_VARIABLES */

EXTERN int global_variable INITIALIZER(37);
EXTERN struct { int a; int b; } oddball_struct INITIALIZER({ 41, 43 });

file1b.c

#define DEFINE_VARIABLES
#include "file3b.h"  /* Variables now defined and initialized */

int increment(void) { return global_variable++; }
int oddball_value(void) { return oddball_struct.a + oddball_struct.b; }

file2b.c

#include "fileba.h"
#include <stdio.h>

void use_them(void)
{
    printf("Global variable: %d\n", global_variable++);
    oddball_struct.a += global_variable;
    oddball_struct.b -= global_variable / 2;
}

Clearly, the code for the oddball structure is not what you'd normallywrite, but it illustrates the point. The first argument to the second invocation ofINITIALIZER is { 41 and the remaining argument (singular in thisexample) is 43 }. Without C99 or similar support for variableargument lists for macros, initializers that need to contain commas arevery problematic.

Header Guards

Any header should be protected against reinclusion, so that typedefinitions (enum, struct or union types, or typedefs generally) do notcause problems. The standard technique is to wrap the body of theheader in a header guard:

#ifndef FILE3B_H_INCLUDED
#define FILE3B_H_INCLUDED

...contents of header...

#endif /* FILE3B_H_INCLUDED */

The header might be included twice indirectly. For example, iffile4b.h includes file3b.h for a type definition that isn't shown,and file1b.c needs to use both header file4b.h and file3b.h, thenyou have some more tricky issues to resolve. Clearly, you might revisethe header list to include just file4b.h. However, you might not beaware of the internal dependencies - and the code should, ideally,continue to work.

Further, it starts to get tricky because you might include file4b.hbefore including file3b.h to generate the definitions, but the normalheader guards on file3b.h would prevent the header being reincluded.

So, you need to include the body of file3b.h at most once fordeclarations, and at most once for definitions, but you might need bothin a single translation unit (TU - a combination of a source file andthe headers it uses).

Multiple inclusion with variable definitions

However, it can be done subject to a not too unreasonable constraint. Let'sintroduce a new set of file names:

  • external.h for the EXTERN macro definitions, etc.
  • file1c.h to define types (notably, struct oddball, the type of oddball_struct).
  • file2c.h to define or declare the global variables.
  • file3c.c which defines the global variables.
  • file4c.c which simply uses the global variables.
  • file5c.c which shows that you can declare and then define the global variables.
  • file6c.c which shows that you can define and then (attempt to) declare the global variables.

In these examples, file5c.c and file6c.c directly include the headerfile2c.h several times, but that is the simplest way to show that themechanism works. It means that if the header was indirectly included twice, it would also be safe.

The restrictions for this to work are:

  1. The header defining or declaring the global variables may not itself define any types.
  2. Immediately before you include a header that should define variables, you define the macro DEFINE_VARIABLES.
  3. The header defining or declaring the variables has stylized contents.

external.h

/*
** This header must not contain header guards (like <assert.h> must not).
** Each time it is invoked, it redefines the macros EXTERN and INITIALIZER
** based on whether macro DEFINE_VARIABLES is currently defined.
*/
#undef EXTERN
#undef INITIALIZER

#ifdef DEFINE_VARIABLES
#define EXTERN                  extern
#define INITIALIZER(...)        /* nothing */
#else
#define EXTERN                  /* nothing */
#define INITIALIZER(...)        = __VA_ARGS__
#endif /* DEFINE_VARIABLES */

file1c.h

#ifndef FILE1C_H_INCLUDED
#define FILE1C_H_INCLUDED

struct oddball
{
    int a;
    int b;
};

extern void use_them(void);
extern int increment(void);
extern int oddball_value(void);

#endif /* FILE1C_H_INCLUDED */

file2c.h

/* Standard prologue */
#if defined(DEFINE_VARIABLES) && !defined(FILE2C_H_DEFINITIONS)
#undef FILE2C_H_INCLUDED
#endif

#ifndef FILE2C_H_INCLUDED
#define FILE2C_H_INCLUDED

#include "external.h"   /* Support macros EXTERN and INITIALIZER */
#include "file1c.h"     /* Type definition for struct oddball */

/* Global variable declarations / definitions */
EXTERN int global_variable INITIALIZER(37);
EXTERN struct oddball oddball_struct INITIALIZER({ 41, 43 });

/* Standard epilogue */
#ifdef DEFINE_VARIABLES
#define FILE2C_H_DEFINITIONS
#undef DEFINE_VARIABLES     /* Safety first */
#endif /* DEFINE_VARIABLES */

#endif /* FILE2C_H_INCLUDED */

file3c.c

#define DEFINE_VARIABLES
#include "file2c.h"  /* Variables now defined and initialized */

int increment(void) { return global_variable++; }
int oddball_value(void) { return oddball_struct.a + oddball_struct.b; }

file4c.c

#include "file2c.h"
#include <stdio.h>

void use_them(void)
{
    printf("Global variable: %d\n", global_variable++);
    oddball_struct.a += global_variable;
    oddball_struct.b -= global_variable / 2;
}

file5c.c

#include "file2c.h"     /* Declare variables */

#define DEFINE_VARIABLES
#include "file2c.h"  /* Variables now defined and initialized */

int increment(void) { return global_variable++; }
int oddball_value(void) { return oddball_struct.a + oddball_struct.b; }

file6c.c

#define DEFINE_VARIABLES
#include "file2c.h"     /* Variables now defined and initialized */

#include "file2c.h"     /* Declare variables */

int increment(void) { return global_variable++; }
int oddball_value(void) { return oddball_struct.a + oddball_struct.b; }

This scheme avoids most problems. You only run into a problem if aheader that defines variables (such as file2c.h) is included byanother header (say file7c.h) that defines variables. There isn't aneasy way around that other than "don't do it".

You can partially work around the problem by revising file2c.h to:

file2c.h - revised

/* Standard prologue */
#if defined(DEFINE_VARIABLES) && !defined(FILE2C_H_DEFINITIONS)
#undef FILE2C_H_INCLUDED
#endif

#ifndef FILE2C_H_INCLUDED
#define FILE2C_H_INCLUDED

#include "external.h"   /* Support macros EXTERN and INITIALIZER */
#include "file1c.h"     /* Type definition for struct oddball */

#if !defined(DEFINE_VARIABLES) || !defined(FILE2C_H_DEFINITIONS)

/* Global variable declarations / definitions */
EXTERN int global_variable           INITIALIZER(37);
EXTERN struct oddball oddball_struct INITIALIZER({ 41, 43 });

#endif /* !DEFINE_VARIABLES || !FILE2C_H_DEFINITIONS */

/* Standard epilogue */
#ifdef DEFINE_VARIABLES
#define FILE2C_H_DEFINITIONS
#undef DEFINE_VARIABLES         /* See discussion below */
#endif /* DEFINE_VARIABLES */

#endif /* FILE2C_H_INCLUDED */

The issue becomes 'should the header include #undef DEFINE_VARIABLES?'If you omit that from the header and wrap any defining invocation with#define and #undef:

#define DEFINE_VARIABLES
#include "file2c.h"
#undef DEFINE_VARIABLES

in the source code (so the headers never alter the value ofDEFINE_VARIABLES, then you should be clean. It is just a nuisance tohave to remember to write the the extra line. An alternative might be:

#define HEADER_DEFINING_VARIABLES "file2c.h"
#include "externdef.h"

where externdef.h contains:

#if defined(HEADER_DEFINING_VARIABLES)
#define DEFINE_VARIABLES
#include HEADER_DEFINING_VARIABLES
#undef DEFINE_VARIABLES
#undef HEADER_DEFINING_VARIABLES
#endif /* HEADER_DEFINING_VARIABLES */

This is getting a tad convoluted, but seems to be secure (using therevised version of file2c.h, and with no #undef DEFINE_VARIABLES inthe file2c.h).

file7c.c

/* Declare variables */
#include "file2c.h"

/* Define variables */
#define HEADER_DEFINING_VARIABLES "file2c.h"
#include "externdef.h"

/* Declare variables - again */
#include "file2c.h"

/* Define variables - again */
#define HEADER_DEFINING_VARIABLES "file2c.h"
#include "externdef.h"

int increment(void) { return global_variable++; }
int oddball_value(void) { return oddball_struct.a + oddball_struct.b; }

file8c.h

/* Standard prologue */
#if defined(DEFINE_VARIABLES) && !defined(FILE8C_H_DEFINITIONS)
#undef FILE8C_H_INCLUDED
#endif

#ifndef FILE8C_H_INCLUDED
#define FILE8C_H_INCLUDED

#include "external.h"   /* Support macros EXTERN and INITIALIZER */
#include "file2c.h"     /* struct oddball */

#if !defined(DEFINE_VARIABLES) || !defined(FILE8C_H_DEFINITIONS)

/* Global variable declarations / definitions */
EXTERN struct oddball another INITIALIZER({ 14, 34 });

#endif /* !DEFINE_VARIABLES || !FILE8C_H_DEFINITIONS */

/* Standard epilogue */
#ifdef DEFINE_VARIABLES
#define FILE8C_H_DEFINITIONS
#endif /* DEFINE_VARIABLES */

#endif /* FILE8C_H_INCLUDED */

file8c.c

/* Define variables */
#define HEADER_DEFINING_VARIABLES "file2c.h"
#include "externdef.h"

/* Define variables */
#define HEADER_DEFINING_VARIABLES "file8c.h"
#include "externdef.h"

int increment(void) { return global_variable++; }
int oddball_value(void) { return oddball_struct.a + oddball_struct.b; }

However, the problems are relatively unlikely to occur in practice,especially if you take the standard advice to

Avoid global variables


Does this exposition miss anything?

Confession: The 'avoiding duplicated code' scheme outlined here wasdeveloped because the issue affects some code I work on (but don't own),and is a niggling concern with the scheme outlined in the first part ofthe answer. However, the original scheme leaves you with just twoplaces to modify to keep variable definitions and declarationssynchronized, which is a big step forward over having exernal variabledeclarations scattered throughout the code base (which really matterswhen there are thousands of files in total). However, the code in thefiles with the names fileNc.[ch] (plus external.h and externdef.h)shows that it can be made to work. Clearly, it would not be hard tocreate a header generator script to give you the standardized templatefor a variable defining and declaring header file.

share | improve this answer
 
2  
@litb: see Annex J.5.11 for the common definition - it is a common extension.–  Jonathan Leffler Sep 16 '09 at 15:19
2  
@litb: and I agree it should be avoided - that's why it is in the section on 'Not so good way to define global variables'.–  Jonathan Leffler Sep 16 '09 at 15:20
2  
Indeed it's a common extension, but it's undefined behavior for a program to rely on it. I just wasn't clear whether you were saying that this is allowed by C's own rules. Now i see you are saying it's just a common extension and to avoid it if you need your code to be portable. So i can upvote you without doubts. Really great answer IMHO :)–  Johannes Schaub - litb Sep 16 '09 at 15:30
20  
I think I need a printable version of this.–  jokoon Sep 20 '12 at 18:48
1  
@Zak: No. The conditional code in file3a.h is #ifdef DEFINE_VARIABLES / #define EXTERN / #else / #define EXTERN extern / #endif, removing comments and using slashes to mark the ends of lines. If DEFINE_VARIABLES is specified, then the variables should not have the extern prefix which would mark them as declarations instead of definitions. That, in turn, means that the compiler will allocate space for the variables, rather than simply recording their existence.–  Jonathan Leffler Jan 16 '13 at 2:14
show 3 more comments

An extern variable is a declaration (thanks to sbi for the correction) of a variable which is defined in another translation unit. So that means that the variable is defined in another file...

Say you have two .c-files test1.c and test2.c. If you define a global variable int test1_var; in test1.c and you'd like to access this variable in test2.c you have to use extern int test1_var; in test2.c.

Complete sample:

$ cat test1.c 
int test1_var = 5;
$ cat test2.c
#include <stdio.h>

extern int test1_var;

int main(void) {
    printf("test1_var = %d\n", test1_var);
    return 0;
}
$ gcc test1.c test2.c -o test
$ ./test
test1_var = 5
share | improve this answer
 
3  
There's no "pseudo-definitions". It's a declaration.–  sbi Sep 16 '09 at 14:18
add comment

Extern is the keyword you use to declare that the variable itself resides in another translation unit.

So you can decide to use a variable in a translation unit and then access it from another one, then in the second one you declare it as extern and the symbol will be resolved by the linker.

If you don't declare it as extern you'll get 2 variables named the same but not related at all, and an error of multiple definitions of the variable.

share | improve this answer
 
3  
In other words the translation unit where extern is used knows about this variable, its type etc. and hence allows the source code in the underlying logic to use it, but it does not allocate the variable, another translation unit will do that. If both translation units were to declare the variable normally, there would be effectily two physical locations for the variable, with the associated "wrong" references within the compiled code, and with the resulting ambiguity for the linker.–  mjv Sep 16 '09 at 14:19
add comment

extern tells the compiler to trust you that the memory for this variable is declared elsewhere, so it doesnt try to allocate/check memory.

Therefore, you can compile a file that has reference to an extern, but you can not link if that memory is not declared somewhere.

Useful for global variables and libraries, but dangerous because the linker does not type check.

share | improve this answer
 
 
The memory isn't declared. See the answers to this question: stackoverflow.com/questions/1410563 for more details.–  sbi Sep 16 '09 at 14:37
add comment

I like to think of an extern variable as a promise that you make to the compiler.

When encountering an extern, the compiler can only find out its type, not where it "lives", so it can't resolve the reference.

You are telling it, "Trust me. At link time this reference will be resolvable."

share | improve this answer
 
 
More generally, a declaration is a promise that the name will be resolvable to a exactly one definition at link time. An extern declares a variable without defining.–  Lie Ryan Nov 30 '10 at 2:16
add comment

Adding an extern turns a variable definition into a variable declaration. See this thread as to what's the difference between a declaration and a definition.

share | improve this answer
 
 
I suppose it's out of question that the down-voter would tell me what he thinks is wrong with this answer?–  sbi Sep 18 '09 at 8:57
 
What difference between int foo and extern int foo (file scope)? Both are declaration, isn't it?–  Corvus Nov 8 '12 at 19:07
 
@user14284: They are both declaration only in the sense that every definition is a declaration, too. But I linked to an explanation of this. ("See this thread as to what's the difference between a declaration and a definition.") Why don't you simple follow the link and read?–  sbi Nov 9 '12 at 9:12
add comment

The correct interpretation of extern is that you tell something to the compiler. You tell the compiler that, despite not being present right now, the variable declared will somehow be found by the linker (typically in another object (file)). The linker will then be the lucky guy to find everything and put it together, whether you had some extern declarations or not.

share | improve this answer
 
 
add comment

extern keyword is used with the variable for its identification as a global variable.

It also represents that you can use the variable declared using extern keyword in any file though it is declared/defined in other file.

share | improve this answer
 
 
add comment

In C a variable inside a file say example.c is given local scope. The compiler expects that the variable would have its definition inside the same file example.c and when it does not find the same , it would throw an error.A function on the other hand has by default global scope . Thus you do not have to explicitly mention to the compiler "look dude...you might find the definition of this function here". For a function including the file which contains its declaration is enough.(The file which you actually call a header file). For example consider the following 2 files :
example.c

#include<stdio.h>
extern int a;
main(){
       printf("The value of a is <%d>\n",a);
}

example1.c

int a = 5;

Now when you compile the two files together, using the following commands :

step 1)cc -o ex example.c example1.cstep 2)./ex

You get the following output : The value of a is <5>

share | improve this answer
 
 
add comment

externAllows one module of your program to access a global variable or function declared in another module of your program.You usually have extern variables declared in header files.If you don't want a program to access your variables or functions, you use static which tells the compiler that this variable or function cannot be used outside of this module.

share | improve this answer
 
 
add comment

first of extern keyword is not used for defineing variable rather it is used for declaring variable. I can say extern is storage class not datatype.

extern is used to let other c file / externalComponent know know this variable is already defined somewhere, Example if you building a library no need define global variable mandatarily some where in library itself. library will be compiled directly but while linking the file it checks for the definition.

share | improve this answer
 
 
add comment

A global variable means that once you define it in any source file, you can access it in another source file without having to define it all over again. To do so, you define and initialize it BEFORE main() in your original function instead of after main, as is normally done.eg:

char codename=gets();

main()
{...
...
your code...
...
...
}

Now, whenever you want to use the global variable codename in another program, you simply type it in as you would normally have defined it, like your other variables, EXCEPT THAT YOU PUT AN extern BEFORE THE VARIABLE NAME.

eg: if I wanted to use extern in another program, I'd type something like this:

main()
{
extern char codename;

printf("%s",codename);

..
...
..

}
share | improve this answer
 
 
In C, you can't use a function call such as gets() to initialize a global variable such as codename (especially since gets() takes an argument and returns a char * but codename is a plain char). In C++, you can use functions to initialize globals, but using gets() still wouldn't work as shown and main() must have an explicit return type in C++ (and in C99 or later). You do not have to define the global variables in the file with main() — they can be defined in other files (think of stderr). You should not write extern char codename; anywhere except one header file.–  Jonathan Leffler Jan 25 at 19:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/waterwindsxu/article/details/21440597

智能推荐

hdu 1229 还是A+B(水)-程序员宅基地

文章浏览阅读122次。还是A+BTime Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24568Accepted Submission(s): 11729Problem Description读入两个小于10000的正整数A和B,计算A+B。...

http客户端Feign——日志配置_feign 日志设置-程序员宅基地

文章浏览阅读419次。HEADERS:在BASIC的基础上,额外记录了请求和响应的头信息。FULL:记录所有请求和响应的明细,包括头信息、请求体、元数据。BASIC:仅记录请求的方法,URL以及响应状态码和执行时间。NONE:不记录任何日志信息,这是默认值。配置Feign日志有两种方式;方式二:java代码实现。注解中声明则代表某服务。方式一:配置文件方式。_feign 日志设置

[转载]将容器管理的持久性 Bean 用于面向服务的体系结构-程序员宅基地

文章浏览阅读155次。将容器管理的持久性 Bean 用于面向服务的体系结构本文将介绍如何使用 IBM WebSphere Process Server 对容器管理的持久性 (CMP) Bean的连接和持久性逻辑加以控制,使其可以存储在非关系数据库..._javax.ejb.objectnotfoundexception: no such entity!

基础java练习题(递归)_java 递归例题-程序员宅基地

文章浏览阅读1.5k次。基础java练习题一、递归实现跳台阶从第一级跳到第n级,有多少种跳法一次可跳一级,也可跳两级。还能跳三级import java.math.BigDecimal;import java.util.Scanner;public class Main{ public static void main(String[]args){ Scanner reader=new Scanner(System.in); while(reader.hasNext()){ _java 递归例题

面向对象程序设计(荣誉)实验一 String_对存储在string数组内的所有以字符‘a’开始并以字符‘e’结尾的单词做加密处理。-程序员宅基地

文章浏览阅读1.5k次,点赞6次,收藏6次。目录1.串应用- 计算一个串的最长的真前后缀题目描述输入输出样例输入样例输出题解2.字符串替换(string)题目描述输入输出样例输入样例输出题解3.可重叠子串 (Ver. I)题目描述输入输出样例输入样例输出题解4.字符串操作(string)题目描述输入输出样例输入样例输出题解1.串应用- 计算一个串的最长的真前后缀题目描述给定一个串,如ABCDAB,则ABCDAB的真前缀有:{ A, AB,ABC, ABCD, ABCDA }ABCDAB的真后缀有:{ B, AB,DAB, CDAB, BCDAB_对存储在string数组内的所有以字符‘a’开始并以字符‘e’结尾的单词做加密处理。

算法设计与问题求解/西安交通大学本科课程MOOC/C_算法设计与问题求解西安交通大学-程序员宅基地

文章浏览阅读68次。西安交通大学/算法设计与问题求解/树与二叉树/MOOC_算法设计与问题求解西安交通大学

随便推点

[Vue warn]: Computed property “totalPrice“ was assigned to but it has no setter._computed property "totalprice" was assigned to but-程序员宅基地

文章浏览阅读1.6k次。问题:在Vue项目中出现如下错误提示:[Vue warn]: Computed property "totalPrice" was assigned to but it has no setter. (found in <Anonymous>)代码:<input v-model="totalPrice"/>原因:v-model命令,因Vue 的双向数据绑定原理 , 会自动操作 totalPrice, 对其进行set 操作而 totalPrice 作为计..._computed property "totalprice" was assigned to but it has no setter.

basic1003-我要通过!13行搞定:也许是全网最奇葩解法_basic 1003 case 1-程序员宅基地

文章浏览阅读60次。十分暴力而简洁的解决方式:读取P和T的位置并自动生成唯一正确答案,将题给测点与之对比,不一样就给我爬!_basic 1003 case 1

服务器浏览war文件,详解将Web项目War包部署到Tomcat服务器基本步骤-程序员宅基地

文章浏览阅读422次。原标题:详解将Web项目War包部署到Tomcat服务器基本步骤详解将Web项目War包部署到Tomcat服务器基本步骤1 War包War包一般是在进行Web开发时,通常是一个网站Project下的所有源码的集合,里面包含前台HTML/CSS/JS的代码,也包含Java的代码。当开发人员在自己的开发机器上调试所有代码并通过后,为了交给测试人员测试和未来进行产品发布,都需要将开发人员的源码打包成Wa..._/opt/bosssoft/war/medical-web.war/web-inf/web.xml of module medical-web.war.

python组成三位无重复数字_python组合无重复三位数的实例-程序员宅基地

文章浏览阅读3k次,点赞3次,收藏13次。# -*- coding: utf-8 -*-# 简述:这里有四个数字,分别是:1、2、3、4#提问:能组成多少个互不相同且无重复数字的三位数?各是多少?def f(n):list=[]count=0for i in range(1,n+1):for j in range(1, n+1):for k in range(1, n+1):if i!=j and j!=k and i!=k:list.a..._python求从0到9任意组合成三位数数字不能重复并输出

ElementUl中的el-table怎样吧0和1改变为男和女_elementui table 性别-程序员宅基地

文章浏览阅读1k次,点赞3次,收藏2次。<el-table-column prop="studentSex" label="性别" :formatter="sex"></el-table-column>然后就在vue的methods中写方法就OK了methods: { sex(row,index){ if(row.studentSex == 1){ return '男'; }else{ return '女'; }..._elementui table 性别

java文件操作之移动文件到指定的目录_java中怎么将pro.txt移动到design_mode_code根目录下-程序员宅基地

文章浏览阅读1.1k次。java文件操作之移动文件到指定的目录_java中怎么将pro.txt移动到design_mode_code根目录下

推荐文章

热门文章

相关标签