Reply by Jan Vanek October 8, 20092009-10-08
Hi, if you speak about inline functions and you say: "prototype in the .h
and the actual function in the .c", then I think you still didn't understand
what inline means, even though 2 others already told you. Put simply, there
is no .c file for inline functions, and in 99% of cases, there is no
prototype. There is just and only a full definition in the .h file:

static inline mod365(unsigned x)
{
return x % 365;
}

With regards,
Jan
----- Original Message -----
From: "drproton2003"
To:
Sent: Wednesday, October 07, 2009 7:14 PM
Subject: [lpc2000] Re: inline function problems
That still doesn't seem to work. I found if I make both the prototype in
the .h and the actual function in the .c static then it works but only if
the function is called from the same .c file. I have made the appropriate
#includes to the other c files that may call the function.

--- In l..., "Paul Curtis" wrote:
>
> Hi,
>
> > I am trying to port some code for the LPC2366 (crossworks V1.7) to the
> > LPC1768 (Crossworks 2.03) and am having some problems. The first problem
> > I
> > encountered has to do with some functions I was inlining. With these
> > functions declared as inline in the relevent .h and .c files I get
> > "undefined reference" errors when the linker runs. If I remove the
> > inline
> > attributes it all builds as normal. What might be the problem?
>
> You need "static inline" in your header file and all uses to #include the
> header:
>
> static inline mod365(unsigned x)
> {
> return x % 365;
> }
>
> --
> Paul Curtis, Rowley Associates Ltd http://www.rowley.co.uk
> CrossWorks V2 is out for LPC1700, LPC3100, LPC3200, SAM9, and more!
>

An Engineer's Guide to the LPC2100 Series

Reply by Paul Curtis October 7, 20092009-10-07
On Wed, 07 Oct 2009 19:37:20 +0100, drproton2003
wrote:

> I am aware of the scope issues with static functions. I was not
> surprised when other files could not access it.
>
> Putting the functions into the .h file does indeed work. The question
> is why did I not have these problems in crossworks 1.7? Has the GCC
> version changed from 1.7 to 2.0?

Yes, gcc has changed between 1.7 and 2.0. The fact is that 1.7 dropped a
non-inlined version in some cases which resolved your "unseen" non-static
inlines.

"inline foo(...)" will inline foo() in the unit it's declared in, and may
*optionally* drop a non-inline version into your code for non-inline
cases. Note the *optionally* here.

-- Paul.

Reply by Paul Curtis October 7, 20092009-10-07
On Wed, 07 Oct 2009 18:14:02 +0100, drproton2003
wrote:

> That still doesn't seem to work.

Yes, it does. You didn't follow instructions.

> I found if I make both the prototype in the .h and the actual function
> in the .c static then it works but only if the function is called from
> the same .c file. I have made the appropriate #includes to the other c
> files that may call the function.

How on earth do you expect the C compiler to inline a function if it
doesn't know what the body of code to inline is because it's in a
separately-compiled file.

I said,
>> You need "static inline" in your header file and all uses to #include
>> the header:
>>
>> static inline mod365(unsigned x)
>> {
>> return x % 365;
>> }

That is, your header file *requires* that the body is in the header too
and there is exactly one definition of the function. No extern linkage,
static linkage #included into each and every file.

-- Paul.

Reply by drproton2003 October 7, 20092009-10-07
I am aware of the scope issues with static functions. I was not surprised when other files could not access it.

Putting the functions into the .h file does indeed work. The question is why did I not have these problems in crossworks 1.7? Has the GCC version changed from 1.7 to 2.0?

--- In l..., 42Bastian wrote:
>
> drproton2003 schrieb:
> > That still doesn't seem to work. I found if I make both the
> > prototype in the .h and the actual function in the .c static then it
> > works but only if the function is called from the same .c file. I
> > have made the appropriate #includes to the other c files that may
> > call the function.
>
> 1st: Get a good book on C/C++ or google the C-FAQ.
> 2nd: A static object (function or variable) is only visible within its
> scope, i.e. a static variable in a function only inside this function, a
> static variable/function within a module (aka C file) only within this
> module.
> 3rd: You need to move the declaration into the .h file, not the prototype.
>
> --
> 42Bastian
> ------------------
> Parts of this email are written with invisible ink.
>
> Note: SPAM-only account, direct mail to bs42@
>

Reply by 42Bastian October 7, 20092009-10-07
drproton2003 schrieb:
> That still doesn't seem to work. I found if I make both the
> prototype in the .h and the actual function in the .c static then it
> works but only if the function is called from the same .c file. I
> have made the appropriate #includes to the other c files that may
> call the function.

1st: Get a good book on C/C++ or google the C-FAQ.
2nd: A static object (function or variable) is only visible within its
scope, i.e. a static variable in a function only inside this function, a
static variable/function within a module (aka C file) only within this
module.
3rd: You need to move the declaration into the .h file, not the prototype.

--
42Bastian
------------------
Parts of this email are written with invisible ink.

Note: SPAM-only account, direct mail to bs42@...
Reply by drproton2003 October 7, 20092009-10-07
That still doesn't seem to work. I found if I make both the prototype in the .h and the actual function in the .c static then it works but only if the function is called from the same .c file. I have made the appropriate #includes to the other c files that may call the function.

--- In l..., "Paul Curtis" wrote:
>
> Hi,
>
> > I am trying to port some code for the LPC2366 (crossworks V1.7) to the
> > LPC1768 (Crossworks 2.03) and am having some problems. The first problem I
> > encountered has to do with some functions I was inlining. With these
> > functions declared as inline in the relevent .h and .c files I get
> > "undefined reference" errors when the linker runs. If I remove the inline
> > attributes it all builds as normal. What might be the problem?
>
> You need "static inline" in your header file and all uses to #include the
> header:
>
> static inline mod365(unsigned x)
> {
> return x % 365;
> }
>
> --
> Paul Curtis, Rowley Associates Ltd http://www.rowley.co.uk
> CrossWorks V2 is out for LPC1700, LPC3100, LPC3200, SAM9, and more!
>

Reply by Paul Curtis October 7, 20092009-10-07
Hi,

> I am trying to port some code for the LPC2366 (crossworks V1.7) to the
> LPC1768 (Crossworks 2.03) and am having some problems. The first problem I
> encountered has to do with some functions I was inlining. With these
> functions declared as inline in the relevent .h and .c files I get
> "undefined reference" errors when the linker runs. If I remove the inline
> attributes it all builds as normal. What might be the problem?

You need "static inline" in your header file and all uses to #include the
header:

static inline mod365(unsigned x)
{
return x % 365;
}

--
Paul Curtis, Rowley Associates Ltd http://www.rowley.co.uk
CrossWorks V2 is out for LPC1700, LPC3100, LPC3200, SAM9, and more!

Reply by drproton2003 October 7, 20092009-10-07
I am trying to port some code for the LPC2366 (crossworks V1.7) to the LPC1768 (Crossworks 2.03) and am having some problems. The first problem I encountered has to do with some functions I was inlining. With these functions declared as inline in the relevent .h and .c files I get "undefined reference" errors when the linker runs. If I remove the inline attributes it all builds as normal. What might be the problem?