

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'". (In fact, you'd generally put extern in a header file rather than putting it in a source file.) Using extern to declare a global variable is pretty much the same thing as using a function declaration to declare a function in a header file. But usually extern is used when you want to access a global variable declared in another source file, as I showed above, and then link the two resulting object files together after compilation.
#Definition of declaration code#
Technically, you could even write code like this:Īnd now you have a declaration of x at the top of the program and a definition at the bottom. Now the use of extern is creating a declaration of a variable but NOT defining it it is saying that the storage for the variable is somewhere else. 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 could actually write a second source file that has this code: The line int x both declares and defines the variable it effectively says, "create a variable named x, of type int. What does it mean to define a variable, exactly? It means you are telling the compiler where to create the storage for that variable.


Most of the time, when you declare a variable, you are also providing the definition. Declaring and Defining Variables with Extern You can also declare a class without defining itĬode that needs to know the details of what is in M圜lass can't work-you can't do this:īecause the compiler needs to know the size of the variable an_object, and it can't do that from the declaration of M圜lass it needs the definition that shows up below. In fact, the definition of the method func could go into another file! 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. But you don't have to.įor example, having a declaration is often good enough for the compiler. Once something is defined, that also counts as declaring it so you can often both declare and define a function, class or variable at the same time. Defining a function means providing a function body defining a class means giving all of the methods of the class and the fields.

What it Means to Define Something in C and C++ĭefining something means providing all of the necessary information to create that thing in its entirety. 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 expect that it will be defined somewhere. So what does a declaration look like? For example, if you write: You don't want to put the body of the function in multiple files, but you do need to provide a declaration for it. This is particularly useful if you are working with multiple source files, and you need to use a function in multiple files. Declaring a value-without defining it-allows you to write code that the compiler can understand without having to put all of the details.
#Definition of declaration full#
The compiler can then handle most (but not all) uses of that name without needing the full definition of that name. 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. What it Means to Declare Something in C and C++ 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++). In C and C++, there is a subtle but important distinction between the meaning of the words declare and define.
