EmbeddedRelated.com
Forums
Memfault Beyond the Launch

How does can you use all functions lpc230x.c

Started by jefspalace April 27, 2008
Hi all,

I am digging through the UART example that comes with the
Olimex_LPC_2378_STK.hzq package in Crossstudio. I noticed that the
file lpc230x.c has no header file and that you can use the functions
in that file in every other file although there is no header file
used.

How can this be?

Regards, Jef

An Engineer's Guide to the LPC2100 Series

Standard C behavior.

In C, functions, unlike variables, are always implicitly defined as extern.
So unless you declare them as static, they have universal scope.

But a compiler worth its salt SHOULD generate a warning....

~ Paul Claessen

----- Original Message -----
From: jefspalace
To: l...
Sent: Sunday, April 27, 2008 3:22 PM
Subject: [lpc2000] How does can you use all functions lpc230x.c
Hi all,

I am digging through the UART example that comes with the
Olimex_LPC_2378_STK.hzq package in Crossstudio. I noticed that the
file lpc230x.c has no header file and that you can use the functions
in that file in every other file although there is no header file
used.

How can this be?

Regards, Jef


> In C, functions, unlike variables, are always implicitly defined as
> extern.
> So unless you declare them as static, they have universal scope.

That would be "extern" storage class. Scope is something completely
different.

> But a compiler worth its salt SHOULD generate a warning....

-Wall in Additional Compiler Options would be your friend here.

-- Paul.

While storage class and scope are different things, they're not totally unrelated.
By giving a function (or variable) a specific storage class, you affect its scope! .. which is what I referred to.
(If you have K&R 2nd edition, see par 4.4 'Scope rules', and A4.1, A8.1, A10 and A11)
Granted, 'universal scope' isn't the accurate term here, but if I had used the more correct name 'external linkage' then I fear it would have been less clear what I meant.

~ Paul Claessen

----- Original Message -----
From: Paul Curtis
To: l...
Sent: Sunday, April 27, 2008 4:22 PM
Subject: RE: [lpc2000] How does can you use all functions lpc230x.c
> In C, functions, unlike variables, are always implicitly defined as
> extern.
> So unless you declare them as static, they have universal scope.

That would be "extern" storage class. Scope is something completely
different.

> But a compiler worth its salt SHOULD generate a warning....

-Wall in Additional Compiler Options would be your friend here.

-- Paul.


Hi,

> While storage class and scope are different things, they're not totally
> unrelated. By giving a function (or variable) a specific storage class,
you affect
> its scope!

No, you do NOT affect a variable's scope by giving it a storage class. No
way.

void foo(void)
{
Extern int x;
x = 1;
}

'x' has extern storage class but has local scope. Storage class and scope
are not related.

--
Paul Curtis, Rowley Associates Ltd http://www.rowley.co.uk
CrossWorks for ARM, MSP430, AVR, MAXQ, and now Cortex-M3 processors

On a closer read of things ...
You are right.
I stand corrected.

~ Paul Claessen

----- Original Message -----
From: Paul Curtis
To: l...
Sent: Sunday, April 27, 2008 5:56 PM
Subject: RE: [lpc2000] How does can you use all functions lpc230x.c
Hi,

> While storage class and scope are different things, they're not totally
> unrelated. By giving a function (or variable) a specific storage class,
you affect
> its scope!

No, you do NOT affect a variable's scope by giving it a storage class. No
way.

void foo(void)
{
Extern int x;
x = 1;
}

'x' has extern storage class but has local scope. Storage class and scope
are not related.

--
Paul Curtis, Rowley Associates Ltd http://www.rowley.co.uk
CrossWorks for ARM, MSP430, AVR, MAXQ, and now Cortex-M3 processors


I don't think that is correct. By declaring it as "extern" you are
explicitly telling the compiler that storage is allocated elsewhere.
What you wrote does not declare a local variable.

If x is not declared elsewhere, the linker will generate an error.

Mark

--- In l..., "Paul Curtis" wrote:
>
> Hi,
>
> > While storage class and scope are different things, they're not
totally
> > unrelated. By giving a function (or variable) a specific storage
class,
> you affect
> > its scope!
>
> No, you do NOT affect a variable's scope by giving it a storage
class. No
> way.
>
> void foo(void)
> {
> Extern int x;
> x = 1;
> }
>
> 'x' has extern storage class but has local scope. Storage class
and scope
> are not related.
>
> --
> Paul Curtis, Rowley Associates Ltd http://www.rowley.co.uk
> CrossWorks for ARM, MSP430, AVR, MAXQ, and now Cortex-M3 processors
>

"What you wrote does not declare a local variable."

That's not what he said: it has local SCOPE!

;-)

~ Paul Claessen

From: l... [mailto:l...] On Behalf Of
mehiegl
Sent: Monday, April 28, 2008 11:16 AM
To: l...
Subject: [lpc2000] Re: How does can you use all functions lpc230x.c

I don't think that is correct. By declaring it as "extern" you are
explicitly telling the compiler that storage is allocated elsewhere.
What you wrote does not declare a local variable.

If x is not declared elsewhere, the linker will generate an error.

Mark

--- In l... , "Paul
Curtis" wrote:
>
> Hi,
>
> > While storage class and scope are different things, they're not
totally
> > unrelated. By giving a function (or variable) a specific storage
class,
> you affect
> > its scope!
>
> No, you do NOT affect a variable's scope by giving it a storage
class. No
> way.
>
> void foo(void)
> {
> Extern int x;
> x = 1;
> }
>
> 'x' has extern storage class but has local scope. Storage class
and scope
> are not related.
>
> --
> Paul Curtis, Rowley Associates Ltd http://www.rowley.co.uk
> CrossWorks for ARM, MSP430, AVR, MAXQ, and now Cortex-M3 processors
>


Mark,

> I don't think that is correct.

Oh, by jove, is is correct.

> By declaring it as "extern" you are
> explicitly telling the compiler that storage is allocated elsewhere.

Actually, no. You're telling it that it has external linkage.

> What you wrote does not declare a local variable.

Oh, it's not an "auto" variable for sure. However, the declaration of x has
LOCAL SCOPE. You cannot assign to "x" outside the function unless you have
another declaration or a definition.

> If x is not declared elsewhere, the linker will generate an error.

Correct. That's linkage and storage class. It has ZERO, NOTHING, NADA to
do with the scope of a declaration.

--
Paul Curtis, Rowley Associates Ltd http://www.rowley.co.uk
CrossWorks for ARM, MSP430, AVR, MAXQ, and now Cortex-M3 processors

Isn't the extern more specifically telling the compiler that the variable
will be resolved at link time?

Michael Scott, SELDS, Inc. http://www.selds.com

Small systems with Big Impact.

_____

From: Paul Curtis [mailto:p...@rowley.co.uk]
Sent: Monday, April 28, 2008 11:38 AM
To: l...
Subject: RE: [lpc2000] Re: How does can you use all functions lpc230x.c

Mark,

> I don't think that is correct.

Oh, by jove, is is correct.

> By declaring it as "extern" you are
> explicitly telling the compiler that storage is allocated elsewhere.

Actually, no. You're telling it that it has external linkage.

> What you wrote does not declare a local variable.

Oh, it's not an "auto" variable for sure. However, the declaration of x has
LOCAL SCOPE. You cannot assign to "x" outside the function unless you have
another declaration or a definition.

> If x is not declared elsewhere, the linker will generate an error.

Correct. That's linkage and storage class. It has ZERO, NOTHING, NADA to
do with the scope of a declaration.

--
Paul Curtis, Rowley Associates Ltd http://www.rowley.
co.uk
CrossWorks for ARM, MSP430, AVR, MAXQ, and now Cortex-M3 processors



Memfault Beyond the Launch