Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #58026 > unrolled thread
| Started by | DSF <notavalid@address.here> |
|---|---|
| First post | 2015-03-09 15:02 -0400 |
| Last post | 2015-03-28 09:05 -0700 |
| Articles | 20 on this page of 28 — 12 participants |
Back to article view | Back to comp.lang.c
Is there any way to make a function local to two TUs without including it as static in both? DSF <notavalid@address.here> - 2015-03-09 15:02 -0400
Re: Is there any way to make a function local to two TUs without including it as static in both? James Kuyper <jameskuyper@verizon.net> - 2015-03-09 15:22 -0400
Re: Is there any way to make a function local to two TUs without including it as static in both? Richard Heathfield <rjh@cpax.org.uk> - 2015-03-09 19:34 +0000
Re: Is there any way to make a function local to two TUs without including it as static in both? Keith Thompson <kst-u@mib.org> - 2015-03-09 12:47 -0700
Re: Is there any way to make a function local to two TUs without including it as static in both? Richard Heathfield <rjh@cpax.org.uk> - 2015-03-09 21:33 +0000
Re: Is there any way to make a function local to two TUs without including it as static in both? Keith Thompson <kst-u@mib.org> - 2015-03-10 07:28 -0700
Re: Is there any way to make a function local to two TUs without including it as static in both? Richard Heathfield <rjh@cpax.org.uk> - 2015-03-10 14:59 +0000
Re: Is there any way to make a function local to two TUs without including it as static in both? Keith Thompson <kst-u@mib.org> - 2015-03-10 10:37 -0700
Re: Is there any way to make a function local to two TUs without including it as static in both? Richard Heathfield <rjh@cpax.org.uk> - 2015-03-10 18:45 +0000
Re: Is there any way to make a function local to two TUs without including it as static in both? Lőrinczy Zsigmond <nospam@for.me> - 2015-03-09 21:04 +0100
Re: Is there any way to make a function local to two TUs without including it as static in both? August Karlstrom <fusionfile@gmail.com> - 2015-03-09 21:39 +0100
Re: Is there any way to make a function local to two TUs without including it as static in both? Kaz Kylheku <kaz@kylheku.com> - 2015-03-09 21:20 +0000
Re: Is there any way to make a function local to two TUs without including it as static in both? Robert Wessel <robertwessel2@yahoo.com> - 2015-03-09 19:23 -0500
Re: Is there any way to make a function local to two TUs without including it as static in both? wssimms@gmail.com - 2015-03-09 21:42 -0700
Re: Is there any way to make a function local to two TUs without including it as static in both? Robert Wessel <robertwessel2@yahoo.com> - 2015-03-10 04:52 -0500
Re: Is there any way to make a function local to two TUs without including it as static in both? Keith Thompson <kst-u@mib.org> - 2015-03-10 07:35 -0700
Re: Is there any way to make a function local to two TUs without including it as static in both? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-10 12:53 -0700
Re: Is there any way to make a function local to two TUs without including it as static in both? Les Cargill <lcargill99@comcast.com> - 2015-03-09 19:58 -0500
Re: Is there any way to make a function local to two TUs without including it as static in both? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-24 13:06 -0700
Re: Is there any way to make a function local to two TUs without including it as static in both? Les Cargill <lcargill99@comcast.com> - 2015-03-24 20:03 -0500
Re: Is there any way to make a function local to two TUs without including it as static in both? James Kuyper <jameskuyper@verizon.net> - 2015-03-25 10:26 -0400
Re: Is there any way to make a function local to two TUs without including it as static in both? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-25 10:36 -0700
Re: Is there any way to make a function local to two TUs without including it as static in both? Kaz Kylheku <kaz@kylheku.com> - 2015-03-25 17:47 +0000
Re: Is there any way to make a function local to two TUs without including it as static in both? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-25 16:03 -0700
Re: Is there any way to make a function local to two TUs without including it as static in both? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-25 10:28 -0700
Re: Is there any way to make a function local to two TUs without including it as static in both? Kaz Kylheku <kaz@kylheku.com> - 2015-03-25 17:46 +0000
[OT] Re: meaning of "signature" in other languages Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-25 16:01 -0700
Re: Is there any way to make a function local to two TUs without including it as static in both? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-28 09:05 -0700
Page 1 of 2 [1] 2 Next page →
| From | DSF <notavalid@address.here> |
|---|---|
| Date | 2015-03-09 15:02 -0400 |
| Subject | Is there any way to make a function local to two TUs without including it as static in both? |
| Message-ID | <5bnrfahtskm5osi399eb9954bleglspe2k@4ax.com> |
Hello!
Is there any way to make a function local to two TUs without including
it as static in both?
Note that this question is not "life or death." I can leave said
function in its own TU and declared public. I ask for reasons of
learning and "correctness." (The function need only be seen by the
two TUs, thus it would be logical for the symbol to be private to
them.)
I have a function in its own TU that is used exclusively by two
other TUs. I know I can make it private by declaring it static within
the TUs and placing it in them, but then there would be two copies.
On a related subject, if there were the same scenario as above, but
only one TU and I wanted to keep the static function in its own file
for clarity, would it be bad form to declare it in the first TU as
static and then include it into the main TU? Such as:
-------------------------------------------------------
/* first TU * (foo.c) /
static int bar(int a, char *c);
#include "bar.c"
void foo(void)
{
...
d = bar(number, string);
...
}
-----------------------------------------------------
/* "private" TU (bar.c) */
int bar(int a, char *c)
{
...
}
"bar.c" would be considered part of the TU "foo.c," right?
DSF
"'Later' is the beginning of what's not to be."
D.S. Fiscus
[toc] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-03-09 15:22 -0400 |
| Message-ID | <54FDF309.3080409@verizon.net> |
| In reply to | #58026 |
On 03/09/2015 03:02 PM, DSF wrote: > Hello! > > Is there any way to make a function local to two TUs without including > it as static in both? No. The closest you can get to doing that, is to combine the two TUs.
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-03-09 19:34 +0000 |
| Message-ID | <mdksj9$cbu$1@dont-email.me> |
| In reply to | #58027 |
On 09/03/15 19:22, James Kuyper wrote: > On 03/09/2015 03:02 PM, DSF wrote: >> Hello! >> >> Is there any way to make a function local to two TUs without including >> it as static in both? > > No. The closest you can get to doing that, is to combine the two TUs. > Which could be done via a #include. Whether that is considered acceptable is a matter of style. Note that, if the static function contains any static data and you want this static data to be shared by the two modules, you would actually have to #include one of the modules in the other (or, as James says, combine them into one module). If you factor out the static function into its own module and #include that, each including module would get its own copy of the function with its own static data. If the two modules really are so closely related that they both need this same static function (and nothing else does), then it is perhaps the case that they should in any case be merged. -- Richard Heathfield Email: rjh at cpax dot org dot uk "Usenet is a strange place" - dmr 29 July 1999 Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-03-09 12:47 -0700 |
| Message-ID | <87lhj62bt3.fsf@kst-u.example.com> |
| In reply to | #58026 |
DSF <notavalid@address.here> writes:
> Hello!
>
> Is there any way to make a function local to two TUs without including
> it as static in both?
[...]
You can declare it in a header, and #include that header only in those
two translation units. Would tnat satisfy your requirements?
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-03-09 21:33 +0000 |
| Message-ID | <mdl3hd$gtu$1@dont-email.me> |
| In reply to | #58029 |
On 09/03/15 19:47, Keith Thompson wrote:
> DSF <notavalid@address.here> writes:
>> Hello!
>>
>> Is there any way to make a function local to two TUs without including
>> it as static in both?
> [...]
>
> You can declare it in a header, and #include that header only in those
> two translation units. Would tnat satisfy your requirements?
It might, but it might not. Consider this function:
static unsigned char uccount(void)
{
static unsigned char count;
return count++;
}
If the function is to be truly local to both TUs, there must be only one
of it. Otherwise, in the above example, each TU's count will proceed
independently, which is presumably not what is intended.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-03-10 07:28 -0700 |
| Message-ID | <87h9tt2afx.fsf@kst-u.example.com> |
| In reply to | #58039 |
Richard Heathfield <rjh@cpax.org.uk> writes:
> On 09/03/15 19:47, Keith Thompson wrote:
>> DSF <notavalid@address.here> writes:
>>> Hello!
>>>
>>> Is there any way to make a function local to two TUs without including
>>> it as static in both?
>> [...]
>>
>> You can declare it in a header, and #include that header only in those
>> two translation units. Would tnat satisfy your requirements?
>
> It might, but it might not. Consider this function:
>
> static unsigned char uccount(void)
> {
> static unsigned char count;
> return count++;
> }
I didn't suggest defining it as static. Just define it normally,
declare it in a header, and #include that header only where it's needed.
[...]
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-03-10 14:59 +0000 |
| Message-ID | <mdn0rh$kdn$1@dont-email.me> |
| In reply to | #58053 |
On 10/03/15 14:28, Keith Thompson wrote:
> Richard Heathfield <rjh@cpax.org.uk> writes:
>> On 09/03/15 19:47, Keith Thompson wrote:
>>> DSF <notavalid@address.here> writes:
>>>> Hello!
>>>>
>>>> Is there any way to make a function local to two TUs without including
>>>> it as static in both?
>>> [...]
>>>
>>> You can declare it in a header, and #include that header only in those
>>> two translation units. Would tnat satisfy your requirements?
>>
>> It might, but it might not. Consider this function:
>>
>> static unsigned char uccount(void)
>> {
>> static unsigned char count;
>> return count++;
>> }
>
> I didn't suggest defining it as static. Just define it normally,
> declare it in a header, and #include that header only where it's needed.
I thought you did, but evidently I was wrong. Once the word 'static' is
in one's head, it tends to stay there (for etymological reasons, no
doubt). Sorry about that.
Whilst your approach is of course entirely workable in Real Life, it
doesn't prevent other TUs from accessing the function, so it doesn't
(quite) answer the OP's original question.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-03-10 10:37 -0700 |
| Message-ID | <874mps3gae.fsf@kst-u.example.com> |
| In reply to | #58056 |
Richard Heathfield <rjh@cpax.org.uk> writes:
> On 10/03/15 14:28, Keith Thompson wrote:
>> Richard Heathfield <rjh@cpax.org.uk> writes:
>>> On 09/03/15 19:47, Keith Thompson wrote:
>>>> DSF <notavalid@address.here> writes:
>>>>> Is there any way to make a function local to two TUs without including
>>>>> it as static in both?
>>>> [...]
>>>>
>>>> You can declare it in a header, and #include that header only in those
>>>> two translation units. Would tnat satisfy your requirements?
>>>
>>> It might, but it might not. Consider this function:
>>>
>>> static unsigned char uccount(void)
>>> {
>>> static unsigned char count;
>>> return count++;
>>> }
>>
>> I didn't suggest defining it as static. Just define it normally,
>> declare it in a header, and #include that header only where it's needed.
>
> I thought you did, but evidently I was wrong. Once the word 'static'
> is in one's head, it tends to stay there (for etymological reasons, no
> doubt). Sorry about that.
>
> Whilst your approach is of course entirely workable in Real Life, it
> doesn't prevent other TUs from accessing the function, so it doesn't
> (quite) answer the OP's original question.
*Nothing* can reliably prevent other translation units from accessing
a function. Even if it's defined as static, you can always remove
the static keyword and recompile, or you can arrange to make its
address available. If it's not declared in a header, you can add
your own declaration.
There are no secure mechanisms in C for protecting parts of a
program from other parts of the same program.
It can certainly make sense, in the interests of information
hiding, to make it *inconvenient* for other TUs to call a function.
The question is, how far do you go to accomplish this goal?
One possibility, I suppose, is to add a step to the build process
(Makefile?) that detects any attempt to #include the appropriate
header other than in the two permitted source files. Of course
it's still easy enough to edit the Makefile. Coding standards can
help -- if they're enforced and/or everyone actually follows them.
I think we're making assumptions about what DSF meant by "local"
(easy to do, since we all know what the word means, but we don't
all mean the same thing, and the standard doesn't define or use the
word in a way that's particularly helpful to the current discussion).
DSF, if you're still here: What exactly do you mean by "local"?
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-03-10 18:45 +0000 |
| Message-ID | <mdne3b$cpj$1@dont-email.me> |
| In reply to | #58058 |
On 10/03/15 17:37, Keith Thompson wrote: > Richard Heathfield <rjh@cpax.org.uk> writes: <snip> >> Whilst your approach is of course entirely workable in Real Life, it >> doesn't prevent other TUs from accessing the function, so it doesn't >> (quite) answer the OP's original question. > > *Nothing* can reliably prevent other translation units from accessing > a function. Kinda my point, yes. -- Richard Heathfield Email: rjh at cpax dot org dot uk "Usenet is a strange place" - dmr 29 July 1999 Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Lőrinczy Zsigmond <nospam@for.me> |
|---|---|
| Date | 2015-03-09 21:04 +0100 |
| Message-ID | <mdkucb$9v$1@speranza.aioe.org> |
| In reply to | #58026 |
On 2015.03.09. 20:02, DSF wrote: > Hello! > > Is there any way to make a function local to two TUs without including > it as static in both? Well, yes: create files foobarcommon.h and foobarcommon.c the usual way; but don't tell anyone about them, use them only in this context.
[toc] | [prev] | [next] | [standalone]
| From | August Karlstrom <fusionfile@gmail.com> |
|---|---|
| Date | 2015-03-09 21:39 +0100 |
| Message-ID | <mdl0d5$8nb$1@speranza.aioe.org> |
| In reply to | #58026 |
On 2015-03-09 20:02, DSF wrote: > Is there any way to make a function local to two TUs without including > it as static in both? [...] Just out of curiosity, what does the function do? Searching the internet for "programming" and "TU" turned up nothing relevant; I assume TU means "translation unit". -- August
[toc] | [prev] | [next] | [standalone]
| From | Kaz Kylheku <kaz@kylheku.com> |
|---|---|
| Date | 2015-03-09 21:20 +0000 |
| Message-ID | <20150309140254.576@kylheku.com> |
| In reply to | #58026 |
On 2015-03-09, DSF <notavalid@address.here> wrote: > Hello! > > Is there any way to make a function local to two TUs without including > it as static in both? There is no intermediate linkage level in standard C which allows this. Linkage is either external, or internal. On some platforms, library mechanisms are more expressive. E.g. ELF shared libraries on Linux can have cross-translation-unit symbolic references which are internal (not available for linkage to the clients which dynamically link the library). To do this in just a simple program made of .o's, you'd probably have to put this function into a custom section, and roll a custom linker script. The symbol is then private because it's in "mysection" or whatever. If you want to work in a C-like language, but with finer grained namespaces, you might want to use C++. (Of course, you can't use some C99 and newer language features.)
[toc] | [prev] | [next] | [standalone]
| From | Robert Wessel <robertwessel2@yahoo.com> |
|---|---|
| Date | 2015-03-09 19:23 -0500 |
| Message-ID | <k6esfa9lkmb8ennqvvr7u03oo9oplrgg2u@4ax.com> |
| In reply to | #58026 |
On Mon, 09 Mar 2015 15:02:44 -0400, DSF <notavalid@address.here>
wrote:
>Hello!
>
>Is there any way to make a function local to two TUs without including
>it as static in both?
>
> Note that this question is not "life or death." I can leave said
>function in its own TU and declared public. I ask for reasons of
>learning and "correctness." (The function need only be seen by the
>two TUs, thus it would be logical for the symbol to be private to
>them.)
>
> I have a function in its own TU that is used exclusively by two
>other TUs. I know I can make it private by declaring it static within
>the TUs and placing it in them, but then there would be two copies.
You could give the common implementation a name unlikely to ever
collide with anything, and then include a #define to translate that
into something usable in the two modules actually using it.
> On a related subject, if there were the same scenario as above, but
>only one TU and I wanted to keep the static function in its own file
>for clarity, would it be bad form to declare it in the first TU as
>static and then include it into the main TU? Such as:
>
>-------------------------------------------------------
>/* first TU * (foo.c) /
>
>static int bar(int a, char *c);
>
>#include "bar.c"
>
>void foo(void)
>{
>...
> d = bar(number, string);
>...
>}
>-----------------------------------------------------
>/* "private" TU (bar.c) */
>
>int bar(int a, char *c)
>{
>...
>}
>
> "bar.c" would be considered part of the TU "foo.c," right?
Yes, although including a .c is often considered bad form.
[toc] | [prev] | [next] | [standalone]
| From | wssimms@gmail.com |
|---|---|
| Date | 2015-03-09 21:42 -0700 |
| Message-ID | <dbe5cdd2-d7c3-435e-b52d-a297afaf5b38@googlegroups.com> |
| In reply to | #58043 |
Am Dienstag, 10. März 2015 09:21:22 UTC+9 schrieb robert...@yahoo.com:
> On Mon, 09 Mar 2015 15:02:44 -0400, DSF <notavalid@address.here>
> wrote:
>
> >Hello!
> >
> >Is there any way to make a function local to two TUs without including
> >it as static in both?
> >
> > Note that this question is not "life or death." I can leave said
> >function in its own TU and declared public. I ask for reasons of
> >learning and "correctness." (The function need only be seen by the
> >two TUs, thus it would be logical for the symbol to be private to
> >them.)
> >
> > I have a function in its own TU that is used exclusively by two
> >other TUs. I know I can make it private by declaring it static within
> >the TUs and placing it in them, but then there would be two copies.
>
>
>
> You could give the common implementation a name unlikely to ever
> collide with anything, and then include a #define to translate that
> into something usable in the two modules actually using it.
>
>
>
> > On a related subject, if there were the same scenario as above, but
> >only one TU and I wanted to keep the static function in its own file
> >for clarity, would it be bad form to declare it in the first TU as
> >static and then include it into the main TU? Such as:
> >
> >-------------------------------------------------------
> >/* first TU * (foo.c) /
> >
> >static int bar(int a, char *c);
> >
> >#include "bar.c"
> >
> >void foo(void)
> >{
> >...
> > d = bar(number, string);
> >...
> >}
> >-----------------------------------------------------
> >/* "private" TU (bar.c) */
> >
> >int bar(int a, char *c)
> >{
> >...
> >}
> >
> > "bar.c" would be considered part of the TU "foo.c," right?
>
>
> Yes, although including a .c is often considered bad form.
Why?
[toc] | [prev] | [next] | [standalone]
| From | Robert Wessel <robertwessel2@yahoo.com> |
|---|---|
| Date | 2015-03-10 04:52 -0500 |
| Message-ID | <oiftfatu2ftlpt63bo1iil8j5e08bkfftg@4ax.com> |
| In reply to | #58046 |
On Mon, 9 Mar 2015 21:42:45 -0700 (PDT), wssimms@gmail.com wrote:
>Am Dienstag, 10. März 2015 09:21:22 UTC+9 schrieb robert...@yahoo.com:
>> On Mon, 09 Mar 2015 15:02:44 -0400, DSF <notavalid@address.here>
>> wrote:
>>
>> >Hello!
>> >
>> >Is there any way to make a function local to two TUs without including
>> >it as static in both?
>> >
>> > Note that this question is not "life or death." I can leave said
>> >function in its own TU and declared public. I ask for reasons of
>> >learning and "correctness." (The function need only be seen by the
>> >two TUs, thus it would be logical for the symbol to be private to
>> >them.)
>> >
>> > I have a function in its own TU that is used exclusively by two
>> >other TUs. I know I can make it private by declaring it static within
>> >the TUs and placing it in them, but then there would be two copies.
>>
>>
>>
>> You could give the common implementation a name unlikely to ever
>> collide with anything, and then include a #define to translate that
>> into something usable in the two modules actually using it.
>>
>>
>>
>> > On a related subject, if there were the same scenario as above, but
>> >only one TU and I wanted to keep the static function in its own file
>> >for clarity, would it be bad form to declare it in the first TU as
>> >static and then include it into the main TU? Such as:
>> >
>> >-------------------------------------------------------
>> >/* first TU * (foo.c) /
>> >
>> >static int bar(int a, char *c);
>> >
>> >#include "bar.c"
>> >
>> >void foo(void)
>> >{
>> >...
>> > d = bar(number, string);
>> >...
>> >}
>> >-----------------------------------------------------
>> >/* "private" TU (bar.c) */
>> >
>> >int bar(int a, char *c)
>> >{
>> >...
>> >}
>> >
>> > "bar.c" would be considered part of the TU "foo.c," right?
>>
>>
>> Yes, although including a .c is often considered bad form.
>
>Why?
If nothing else it'll be a bit of a surprise. And in many build
systems you'll not get the results you want without doing some
tweaking (consider all the make files in the world that will compile
any .c file they see - not want you'd want in this case).
So why not just make the included thing a .h and avoid any issues?
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-03-10 07:35 -0700 |
| Message-ID | <87d24g3opa.fsf@kst-u.example.com> |
| In reply to | #58043 |
Robert Wessel <robertwessel2@yahoo.com> writes:
> On Mon, 09 Mar 2015 15:02:44 -0400, DSF <notavalid@address.here>
> wrote:
>>Is there any way to make a function local to two TUs without including
>>it as static in both?
>>
>> Note that this question is not "life or death." I can leave said
>>function in its own TU and declared public. I ask for reasons of
>>learning and "correctness." (The function need only be seen by the
>>two TUs, thus it would be logical for the symbol to be private to
>>them.)
>>
>> I have a function in its own TU that is used exclusively by two
>>other TUs. I know I can make it private by declaring it static within
>>the TUs and placing it in them, but then there would be two copies.
>
> You could give the common implementation a name unlikely to ever
> collide with anything, and then include a #define to translate that
> into something usable in the two modules actually using it.
What's the advantage of that over just declaring the function, with a
straightforward name, in a header that's #included only by the two
translation units that need it?
With the obscure name and the #define in a header, anything that
#includes the header can call the function. With a normal declaration
in a header, anything that #includes the header can call the function.
I think different people are making different assumptions about just
what hte OP means by "local". You can't make it entirely impossible for
something other than those two translation units to call the function.
The question is, just how difficult does it need to be?
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-03-10 12:53 -0700 |
| Message-ID | <e69abb6f-b250-487d-88d5-89f8daa812b0@googlegroups.com> |
| In reply to | #58054 |
On Tuesday, March 10, 2015 at 2:35:39 PM UTC, Keith Thompson wrote: > > > You could give the common implementation a name unlikely to ever > > collide with anything, and then include a #define to translate that > > into something usable in the two modules actually using it. > > What's the advantage of that over just declaring the function, with a > straightforward name, in a header that's #included only by the two > translation units that need it? > Let's say we've got a function called cubic_roots(). It's obvious what the function does, it's a good choice for a name. It's also a bit fiddly to get the accuracy as you want it - if you've got a version which works well with your particular application, you don't really want someone messing with the cubic root finder, in case it starts going wrong for near-degenerate / extreme inputs. However if you're writing a maths-heavy program, it's likely that lots of different modules, maybe written by different people, wil, want to take roots of cubic equations. So you'll have cubicroots, cubicRoots, solvepolynomial4() and, quite likely cubic_roots(). So a headache for the linker. A solution is to call it "KeithTs_cubic_roots". But that's a bit of a headache to read in code. so #define KeithTs_cubic_roots as just cubic_roots.
[toc] | [prev] | [next] | [standalone]
| From | Les Cargill <lcargill99@comcast.com> |
|---|---|
| Date | 2015-03-09 19:58 -0500 |
| Message-ID | <mdlf9v$359$1@dont-email.me> |
| In reply to | #58026 |
DSF wrote:
> Hello!
>
> Is there any way to make a function local to two TUs without including
> it as static in both?
>
The *identical* function? Or just "same name, same signature"?
You can have a pointer to a static function that is also static and
local to both.
I would also not consider having a slight difference in naming between
two global functions in two TUs to be too sinful.
> Note that this question is not "life or death." I can leave said
> function in its own TU and declared public. I ask for reasons of
> learning and "correctness." (The function need only be seen by the
> two TUs, thus it would be logical for the symbol to be private to
> them.)
>
> I have a function in its own TU that is used exclusively by two
> other TUs. I know I can make it private by declaring it static within
> the TUs and placing it in them, but then there would be two copies.
>
So:
static int thing(void *a, int b, void *x)
{
....
return 0;
}
int (*getRefToThing) (void *a, int b, void *x)
{
return &thing;
}
I always make mistakes on these so it may take a bit of massaging
to get the compiler to cease jabbering.
>
> On a related subject, if there were the same scenario as above, but
> only one TU and I wanted to keep the static function in its own file
> for clarity, would it be bad form to declare it in the first TU as
> static and then include it into the main TU? Such as:
>
> -------------------------------------------------------
> /* first TU * (foo.c) /
>
> static int bar(int a, char *c);
>
> #include "bar.c"
>
> void foo(void)
> {
> ...
> d = bar(number, string);
> ...
> }
> -----------------------------------------------------
> /* "private" TU (bar.c) */
>
> int bar(int a, char *c)
> {
> ...
> }
>
> "bar.c" would be considered part of the TU "foo.c," right?
>
> DSF
> "'Later' is the beginning of what's not to be."
> D.S. Fiscus
>
'C' files including 'C' files gives some people rash. I am not one
of those people. And there's the question of how to manage the
dependencies of the included 'C' file - nothing huge, just a bit
of work. Do the includers have to include all the includes or what?
"gcc -M" doesn't care if it's a .c file.
--
Les Cargill
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <txr@alumni.caltech.edu> |
|---|---|
| Date | 2015-03-24 13:06 -0700 |
| Message-ID | <kfn4mpaf9ed.fsf@x-alumni2.alumni.caltech.edu> |
| In reply to | #58047 |
Les Cargill <lcargill99@comcast.com> writes: > DSF wrote: >> Hello! >> >> Is there any way to make a function local to two TUs without including >> it as static in both? > > The *identical* function? Or just "same name, same signature"? In C functions don't have signatures, they have types.
[toc] | [prev] | [next] | [standalone]
| From | Les Cargill <lcargill99@comcast.com> |
|---|---|
| Date | 2015-03-24 20:03 -0500 |
| Message-ID | <met17o$lp3$1@dont-email.me> |
| In reply to | #58874 |
Tim Rentsch wrote: > Les Cargill <lcargill99@comcast.com> writes: > >> DSF wrote: >>> Hello! >>> >>> Is there any way to make a function local to two TUs without including >>> it as static in both? >> >> The *identical* function? Or just "same name, same signature"? > > In C functions don't have signatures, they have types. > True dat. Too bad it's not considered the same thing. -- Les Cargill
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.c
csiph-web