what does it mean to compile a language?

Declare vs Define in C and C++


By Alex Allain

In C and C++, in that location is a subtle just important stardom between the meaning of the words declare and define. If you don't understand the deviation, 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 lot declare a variable, a role, or even a form all y'all are doing is saying: there is something with this name, and it has this type. The compiler tin and then handle most (merely not all) uses of that name without needing the total definition of that name. Declaring a value--without defining it--allows you lot to write code that the compiler can understand without having to put all of the details. This is specially useful if you are working with multiple source files, and you lot need to use a part in multiple files. You don't want to put the body of the function in multiple files, but you exercise need to provide a declaration for it.

Then what does a announcement look similar? For instance, 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 expect that it volition 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 entirety. Defining a function means providing a function body; defining a class means giving all of the methods of the class and the fields. In one case something is defined, that also counts equally declaring it; and then you can often both declare and define a function, class or variable at the same time. But you don't have to.

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

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

Since the compiler knows the render value of func, and the number of arguments it takes, it can compile the phone call to func fifty-fifty though it doesn't nonetheless have the definition. In fact, the definition of the method func could go into another file!

You lot can also declare a class without defining information technology

form MyClass;        

Code that needs to know the details of what is in MyClass can't work--you lot can't exercise this:

class MyClass;  MyClass an_object;  class MyClass  {     int _a_field; };        

Considering the compiler needs to know the size of the variable an_object, and it can't practise that from the announcement of MyClass; it needs the definition that shows upwards below.

Declaring and Defining Variables with Extern

Most of the fourth dimension, when you lot declare a variable, you are as well providing the definition. What does information technology mean to define a variable, exactly? It means you lot are telling the compiler where to create the storage for that variable. For case, if you write:

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

The line int ten; both declares and defines the variable; information technology effectively says, "create a variable named x, of type int. Likewise, 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 2d source file that has this code:

extern int ten; int func() {     x = three; }        

Now the use of extern is creating a annunciation of a variable but Non defining information technology; it is saying that the storage for the variable is somewhere else. Technically, you could even write lawmaking like this:

extern int x; int func() {     x = 3; }  int ten;        

And at present you have a declaration of ten at the superlative of the plan and a definition at the bottom. Just usually extern is used when you want to admission a global variable declared in some other source file, as I showed to a higher place, and and then link the ii resulting object files together after compilation. Using extern to declare a global variable is pretty much the same matter equally using a function declaration to declare a function in a header file. (In fact, you'd generally put extern in a header file rather than putting information technology in a source file.)

In fact, if you put a variable into a header file and practise non use extern, you volition encounter the inverse problem of an undefined symbol; you lot volition 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.

Announcement 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 part, what it does; if information technology's a course, what fields and methods information technology has; if it's a variable, where that variable is stored. Frequently, the compiler only needs to take a announcement for something in order to compile a file into an object file, expecting that the linker tin find the definition from another file. If no source file ever defines a symbol, merely information technology is declared, you will become errors at link time complaining most undefined symbols.

Common Cases

If you want to utilize a part across multiple source files, you should declare the office in one header file (.h) and so put the part definition in one source file (.c or .cpp). All code that uses the role should include just the .h file, and you should link the resulting object files with the object file from compiling the source file.

If you lot want to use a course in multiple files, you should put the class definition in a header file and ascertain the class methods in a corresponding source file. (Y'all an besides utilise inline functions for the methods.)

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

Read more than similar articles

Compiling and Linking A brief clarification of the compiling and linking process

The C Preprocessor tin aid you avoid the problem of having multiple declarations for the same proper noun

Learn more about dealing with compiler warnings Compiler warnings are meaningful--acquire how and why to deal with them

yoppoverearrever.blogspot.com

Source: https://www.cprogramming.com/declare_vs_define.html

0 Response to "what does it mean to compile a language?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel