473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

How best to provide a module

If distributing a piece of code in C how is it best to provide it?
Options seem to be

1. Precompile the module.c file(s) to module.o and provide a header.
The using code then has

#include "module.h"

at the top and needs to be linked with the compiled module.

2. Include the source directly. The using code then has

#include "module.c"

at the lowest lexical level - i.e. not within a function. Are both of
these viable ways to include a separate piece of source code and and
are there any others?

--
TIA,
James
Aug 21 '08 #1
10 1358
James Harris said:
If distributing a piece of code in C how is it best to provide it?
It depends on your needs.

Options seem to be

1. Precompile the module.c file(s) to module.o and provide a header.
The using code then has

#include "module.h"

at the top and needs to be linked with the compiled module.
That's one way, and it's useful if you don't want to distribute the source.
2. Include the source directly. The using code then has

#include "module.c"
But that's *not* another way! :-) At least, it's a lousy, lousy idea. If
you're okay with the user having the source, just send them the source
(which you'd have to do anyway, for the above to "work"), and either rely
on them to, or tell them how to, compile it and link it into their
project. (You shouldn't need to tell them unless they're very new to C.)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 21 '08 #2
On 21 Aug, 14:15, Richard Heathfield <r...@see.sig.invalidwrote:
James Harris said:
....
2. Include the source directly. The using code then has
#include "module.c"

But that's *not* another way! :-) At least, it's a lousy, lousy idea. If
you're okay with the user having the source, just send them the source
(which you'd have to do anyway, for the above to "work"), and either rely
on them to, or tell them how to, compile it and link it into their
project. (You shouldn't need to tell them unless they're very new to C.)
Haha! I've not seen anyone do this. But why is it always lousy? And
are there no circumstances where it would be good?


Aug 21 '08 #3

"James Harris" <ja************@googlemail.comwrote in message
news:cf**********************************@l42g2000 hsc.googlegroups.com...
On 21 Aug, 14:15, Richard Heathfield <r...@see.sig.invalidwrote:
>James Harris said:
...
2. Include the source directly. The using code then has
#include "module.c"

But that's *not* another way! :-) At least, it's a lousy, lousy idea. If
you're okay with the user having the source, just send them the source
(which you'd have to do anyway, for the above to "work"), and either rely
on them to, or tell them how to, compile it and link it into their
project. (You shouldn't need to tell them unless they're very new to C.)

Haha! I've not seen anyone do this.
I've seen it once or twice.
But why is it always lousy?
It can cause problems with duplicate definitions.
And
are there no circumstances where it would be good?
Never say never. :-)

But I recommend not to do it without some compelling
reason.

-Mike
Aug 21 '08 #4
James Harris wrote:
Haha! I've not seen anyone do this. But why is it always lousy?
Because it means, that you can include that module into only one
source file. If you include it multiple times, that you got a
bunch of object files all with the same identifiers clashing at
link time.
And are there no circumstances where it would be good?
No. Either provide a separate header, or just provide the source
file and let the other people extract a header with a bunch of
PERL code.

Wolfgang Draxinger
--
E-Mail address works, Jabber: he******@jabber.org, ICQ: 134682867

Aug 21 '08 #5

"Wolfgang Draxinger" <wd********@darkstargames.dewrote in message
James Harris wrote:
>Haha! I've not seen anyone do this. But why is it always lousy?

Because it means, that you can include that module into only one
source file. If you include it multiple times, that you got a
bunch of object files all with the same identifiers clashing at
link time.
Simply make all the functions and globals static.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Aug 21 '08 #6
On 21 Aug, 15:25, "Mike Wahler" <mkwah...@mkwahler.netwrote:
....
But why is it always lousy?

It can cause problems with duplicate definitions.
ok
And
are there no circumstances where it would be good?

Never say never. :-)

But I recommend not to do it without some compelling
reason.
Agreed.
Aug 21 '08 #7
Malcolm McLean wrote:
>
"Wolfgang Draxinger" <wd********@darkstargames.dewrote in message
>James Harris wrote:
>>Haha! I've not seen anyone do this. But why is it always lousy?

Because it means, that you can include that module into only one
source file. If you include it multiple times, that you got a
bunch of object files all with the same identifiers clashing at
link time.
Simply make all the functions and globals static.
Reduces the problem a bit, but does not solve it. To be sure,
it can't be solved completely: Name collisions are ultimately
unavoidable when integrating C code from multiple suppliers.
However, separate compilation has the virtue of limiting the
"exposed" identifiers to those with external linkage, the module's
public interface, more or less. Wholesale source inclusion, on the
other hand, brings *all* the module's identifiers out into the open
where they can collide with the user's names.

/* module.c */
void doThis(void) { ... }
void doThat(void) { ... }
static void helper(void) { ... }

The normal approach requires the rest of the program to avoid using
`doThis' and `doThat' as externally-linked identifiers. Wholesale
inclusion requires the including module to avoid `doThis' and
`doThat' *and* `helper' for *any* file-scope identifiers.

Also, the mention of "globals" suggests that the consequences
have not been thought through. If `static int global;' appears in
the #include'd module, then each translation unit that #includes it
gets its very own private version of `global'. If Module M1 sets
`global = 42;', Module M2 cannot detect it -- which sort of goes
against the purpose of having a "global" in the first place.

--
Er*********@sun.com
Aug 21 '08 #8
James Harris <ja************@googlemail.comwrites:
If distributing a piece of code in C how is it best to provide it?
Options seem to be

1. Precompile the module.c file(s) to module.o and provide a header.
The using code then has

#include "module.h"

at the top and needs to be linked with the compiled module.
That can work, but only if the module.o file is compatible with the
user's system. You could provide multiple versions of module.o for
different systems. It's a pain, but it's pretty much necessary if you
don't want to distribute your source code.

You might also consider distributing shrouded C source. There are
utilities that will automatically obfuscate C source code, so it will
still compile and have the same semantics but is practically very
difficult to read or modify.

But if you don't mind distributing your sources (and your second idea
suggests you don't), then you probably shouldn't bother.
2. Include the source directly. The using code then has

#include "module.c"

at the lowest lexical level - i.e. not within a function. Are both of
these viable ways to include a separate piece of source code and and
are there any others?
Bad idea. Just distribute two files, module.h and module.c.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 22 '08 #9

"James Harris" <ja************@googlemail.comwrote in message
news:95**********************************@2g2000hs n.googlegroups.com...
If distributing a piece of code in C how is it best to provide it?
Options seem to be

1. Precompile the module.c file(s) to module.o and provide a header.
The using code then has

#include "module.h"

at the top and needs to be linked with the compiled module.

2. Include the source directly. The using code then has

#include "module.c"

at the lowest lexical level - i.e. not within a function. Are both of
these viable ways to include a separate piece of source code and and
are there any others?
3. Precompile into a dynamic library. Distribute the library plus a .h file
or docs sufficient to make use of the library.

The format will vary between OSs, but is not so dependent on compiler (or,
to some extent, language).

--
Bartc

Aug 22 '08 #10
James Harris wrote:
On 21 Aug, 14:15, Richard Heathfield <r...@see.sig.invalidwrote:
>James Harris said:
...
>>2. Include the source directly. The using code then has
#include "module.c"
But that's *not* another way! :-) At least, it's a lousy, lousy idea. If
you're okay with the user having the source, just send them the source
(which you'd have to do anyway, for the above to "work"), and either rely
on them to, or tell them how to, compile it and link it into their
project. (You shouldn't need to tell them unless they're very new to C.)

Haha! I've not seen anyone do this. But why is it always lousy? And
are there no circumstances where it would be good?
It may allow some compilers to perform better optimization (such as
inlining) if the entire program is presented as a single translation unit.

S
Aug 22 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Matthew Wilson | last post by:
Hi- I'm writing a bunch of classes, several of which need functions and variables defined in the math module. In some instances, I'm going to import my module like this: import myshapes ...
17
by: dananrg | last post by:
I'm a little confused about what's out there for database modules at: http://python.org/topics/database/modules.html What I'd like to do is use Python to access an Oracle 9.X database for...
9
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice =...
0
by: Thomas Ploch | last post by:
Hello fellows, I just wanted to know, if there is any best practice concerning following code: import re, shelve class TextMatcher: def __init__(self, patterns, email=False,...
13
by: André | last post by:
Hi, i'm developping asp.net applications and therefore i use VB.net. I have some questions about best practises. According what i read about class and module and if i understand it right, a...
2
by: Sells, Fred | last post by:
I need to talk to a vendor side via SOAP, Googling is overwhelming and many hits seem to point to older attempts. Can someone tell me which SOAP module is recommended. I'm using Python 2.4. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.