Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ Electronics
➜ Microprocessors
➜ Declaration vs Definition in C
Declaration vs Definition in C
|
Postings by administrators only.
Refresh page
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Sun 22 Feb 2015 03:21 AM (UTC) |
Message
| A short discussion about the difference in C (and C++) between declarations and definitions.
This example declares foo:
extern int foo;
extern int foo;
void setup ()
{
foo = 42;
} // end of setup
void loop () { }
Interestingly, you can declare foo multiple times (provided you declare it the same way each time).
The code above compiles but does not link, because the linker cannot find a definition of foo. That is, we promise that foo will eventually exist in some compilation unit, but it does not.
sketch_feb22a.cpp.o: In function `loop':
/home/nick/Development/arduino-1.0.6/sketch_feb22a.ino:9: undefined reference to `foo'
/home/nick/Development/arduino-1.0.6/sketch_feb22a.ino:9: undefined reference to `foo'
You can tell it is a linker error because the file name is "sketch_feb22a.cpp.o" - which ends with .o for "object" file.
If we remove the declaration the compiler complains that it isn't declared (not that it isn't defined):
void setup ()
{
foo = 42;
} // end of setup
void loop () { }
Error:
sketch_feb22a.ino: In function ‘void setup()’:
sketch_feb22a:4: error: ‘foo’ was not declared in this scope
Note this is a compiler error, not a linker error, because the file name ends in ".ino".
If we add a definition of foo, it compiles without errors:
int foo;
void setup ()
{
foo = 42;
} // end of setup
void loop () { }
This definition also provides a declaration, thus we don't get any messages about foo not being declared.
You can also declare and also define foo:
extern int foo; // declare
int foo; // define
void setup ()
{
foo = 42;
} // end of setup
void loop () { }
You can also do a similar thing with functions:
void bar ();
void setup ()
{
bar ();
} // end of setup
void loop () { }
In this case bar is declared but not defined.
sketch_feb22a.cpp.o: In function `setup':
/home/nick/Development/arduino-1.0.6/sketch_feb22a.ino:5: undefined reference to `bar()
The linker (not the compiler) complains that it cannot find a definition of bar().
void bar ();
void setup ()
{
bar ();
} // end of setup
void bar ()
{
Serial.println ("Hello, world");
}
void loop () { }
Now bar is both declared (a forward declaration) and defined.
The definition is the part that instantiates the function or variable. And this can only be done once.
You cannot have multiple definitions of one thing:
int foo;
int foo;
void setup ()
{
foo = 42;
} // end of setup
void loop () { }
Error:
sketch_feb22a:2: error: redefinition of ‘int foo’
sketch_feb22a:1: error: ‘int foo’ previously declared here
Interestingly, the compiler reports where it was previously declared, not where it was previously defined, although the error clearly is a duplicate definition.
If you move foo into a different compilation unit (eg. foo.cpp) then you get a different message (a linker error). This is because each compilation unit compiled OK, but the linker found multiple definitions of foo.
sketch_feb22a.cpp.o: In function `loop':
/home/nick/Development/arduino-1.0.6/sketch_feb22a.ino:9: multiple definition of `foo'
foo.cpp.o:(.bss.foo+0x0): first defined here
Now the error message refers to where it was previously defined (not previously declared).
A declaration provides enough information for the compiler to "work" with that variable or function, without necessarily knowing where it resides or what it does. For example:
The compiler knows to generate code to load/store/work with an int, even though at this stage it doesn't know where foo resides.
int bar (byte a, char * b);
In this case it knows bar is a function, it returns an int, and it takes two arguments, one is a byte, and one is a char. This is enough to generate code to call bar, and handle whatever bar returns.
However at this stage it does not know exactly what bar does. That is eventually provided when bar is defined (possibly in a different compilation unit). |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
7,577 views.
Postings by administrators only.
Refresh page
top