Declare vs define in C and C ++

Source: Internet
Author: User

In C and C ++, there is a subtle but important distinction between the meaning of the words declare and define. if you don't understand the difference, you'll run into weird linker errors like "undefined symbol foo" or "undefined reference to 'foo'" or even "undefined reference to vtable For foo" (in C ++ ).

What it means to declare something in C and C ++

When you declare a variable, a function, or even a class all you are doing is saying: There is something with this name, and it has this type. the compiler can then handle most (but not all) uses of that name without needing the full definition of that name. declaring a value -- without defining it -- allows you to write code that the compiler can understand without having to put all of the details. this is a special useful if you are working with multiple source files, and you need to use a function in multiple files. you don't need want to put the body of the function in multiple files, but you do need to provide a declaration for it.


So what does a declaration look like? For example, if you write:

 
Int func ();

This is a function declaration; it does not provide the body of the function, but it does tell the compiler that it can use this function and perform CT that it will be defined somewhere.

What it means to define something in C and C ++

Defining something means providing all of the necessary information to create that thing in its entirity. defining a function means providing a function body; defining a class means giving all of the methods of the class and the fields. once something is defined, that also counts as declaring it; so you can often both declare and define a funtion, class or variable at the same time. but you don't have.

For example, having a declaration is often good enough for the compiler. You can write code like this:

 
Int func (); int main () {int x = func ();} int func () {return 2 ;}

Since the compiler knows the return value of func, and the number of arguments it takes, it can compile the call to func even though it doesn't yet have the definition. in fact, the definition of the method func cocould go into another file!

You can also declare a class without defining it

 
Class myclass;

Code that needs to know the details of what is in myclass can't work -- you can't do this:

 
Class myclass; myclass an_object; Class myclass {int _ a_field ;};

Because the compiler needs to know the size of the variable an_object, and it can't do that from the declaration of myclass; it needs the definition that shows up below.

Declaring and defining variables with extern

Most of the time, when you declare a variable, you are also providing the definition. What does it mean to define a variable, exactly? It means you are telling the compiler where to create the storage for that variable. For example, if you write:

 
Int X; int main () {x = 3 ;}

The line int X; both declares and defines the variable; it implements tively says, "Create a variable named X, of Type Int. also, the storage for the variable is that it is a global variable defined in the object file associated with this source file. "That's kind of weird, isn' t it? What is going on is that someone else cocould actually write a second source file that has this Code:

 
Extern int X; int func () {x = 3 ;}

Now the useExternIs creating a declaration of a variable but not defining it; it is saying that the storage for the variable is somewhere else. Technically, you cocould even write code like this:

 
Extern int X; int func () {x = 3;} int X;

And now you have a declaration of X at the top of the program and a definition at the bottom. but usually extern is used when you want to access a global variable declared in another source file, as I showed abve, and then link the two resulting object files together after compilation. using extern to declare a global variable is pretty much the same thing as using a function declaration to declare a fucntion in a header file. (In fact, you 'd generally put extern in a header file rather than putting it in a source file .)

In fact, if you put a variable into a header file and do not use extern, you will run into the inverse problem of an undefined symbol; you will have a symbol with multiple definitions, with an error like "redefinition of 'foo '". this will happen when the linker goes to link together multiple object files.

Declaration vs definition: In summary

A declaration provides basic attributes of a symbol: its type and its name. A definition provides all of the details of that symbol -- if it's a function, what it does; if it's a class, what fields and methods it has; if it's a variable, where that variable is stored. often, the compiler only needs to have a declaration for something in order to compile a file into an object file, expecting that the linker can find the definition from another file. if no source file ever defines a symbol, but it is declared, you will get errors at link time complaining about undefined symbols.

Common Cases

If you want to use a function to upload SS multiple source files, you shoshould declare the function in one header file (. h) and then put the function definition in one source file (. C or. CPP ). all code that uses the function shoshould include just. h file, and you shoshould link the resulting object files with the object file from compiling the source file.

If you want to use a class in multiple files, you should put the class definition in a header file and define the class methods in a corresponding source file. (You An also use inline functions for the methods .)

If you want to use a variable in multiple files, you should put the declaration of the variable using the extern keyword in one header file, and then include that header file in all source files that need that variable. then you shocould put the definition of that variable in one source file that is linked with all the object files that use that variable.

Related Article

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.