extern

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

extern

Ian Chapman
HI,
     I'm using -  msp430-gcc (GCC) 4.6.3 20120301 (mspgcc LTS 20120406
unpatched)- and it fails on

extern unsigned short int ContactThreshold;

giving this error

msp430-gcc -Os -Wall -g -mmcu=msp430f5438a main.cpp
/tmp/cciyW9nS.o: In function `SetSensitivity(unsigned short)':
/home/stan/msp430/Ovi_Stuff/main.cpp:258: undefined reference to
`ContactThreshold'

If I junk extern it compiles and once I get there I expect a linker
error with ContactThreshold defined in two places.

Any suggestions appreciated.  Ian.  Oh and it did compile with windows.  
Maybe I should change to straight c in place of cpp.

         Regards Ian.

------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Reply | Threaded
Open this post in threaded view
|

Re: extern

Daniel Beer-3
On Thu, Jul 25, 2013 at 10:39:09PM -0400, Ian Chapman wrote:

>      I'm using -  msp430-gcc (GCC) 4.6.3 20120301 (mspgcc LTS 20120406
> unpatched)- and it fails on
>
> extern unsigned short int ContactThreshold;
>
> giving this error
>
> msp430-gcc -Os -Wall -g -mmcu=msp430f5438a main.cpp
> /tmp/cciyW9nS.o: In function `SetSensitivity(unsigned short)':
> /home/stan/msp430/Ovi_Stuff/main.cpp:258: undefined reference to
> `ContactThreshold'
>
> If I junk extern it compiles and once I get there I expect a linker
> error with ContactThreshold defined in two places.
>
> Any suggestions appreciated.  Ian.  Oh and it did compile with windows.  
> Maybe I should change to straight c in place of cpp.

Hi Ian,

The "extern" keyword means that ContactThreshold is defined in another
compilation unit. That is:

    unsigned short int ContactTheshold;

...defines a variable called ContactThreshold. If you prefix it with
"extern", you aren't defining it -- you're telling the compiler that it
exists and is defined somewhere else.

Since you appear to be compiling only a single file, "extern" is
probably not what you want.

- Daniel

--
Daniel Beer <[hidden email]>    www.dlbeer.co.nz
IRC: inittab (Freenode)    PGP key: 2048D/160A553B

------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users
Reply | Threaded
Open this post in threaded view
|

Re: extern

DJ Delorie
In reply to this post by Ian Chapman

Typically, you have "extern ..." in all but one source file, and just
"..." in one source file where that symbol is defined.

Saying "extern int foo;" means "I expect some other module to have foo."

Saying "int foo;" means "I have foo."

So exactly one module needs to have foo, to avoid linker errors.

There's an exception in C...

If you have an uninitialized variable, it tells the linker to allocate
space for it (it's a "common symbol" instead of a regular symbol).
IMHO this is bad practice, because it lets two modules think they
"own" a variable, perhaps with two different purposes.

------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
_______________________________________________
Mspgcc-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users