delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/03/14:42:59

Date: Sat, 3 Jan 1998 11:39:48 -0800 (PST)
Message-Id: <199801031939.LAA26824@adit.ap.net>
Mime-Version: 1.0
To: rburrows AT hotmail DOT com, djgpp AT delorie DOT com
From: Nate Eldredge <eldredge AT ap DOT net>
Subject: Re: Making Library/object

At 06:18  12/31/1997 +0000, Robin Burrows wrote:
>I'm trying to make a playback library for my utility/programme, and am
>completely stuck.
>Ideally I'd like a file you can link with gcc to another programme
>without recompiling. Example:
>
>int myvar1, myvar2, myvar3;
>
>void function1 (void)
>{
>	things;
>}
>
>void function2 (void)
>{
>	things;
>}
>
>How would I make this into a linkable module (are they .o files?). The
>user should be able to use the functions and access the variables from a
>.h file that they include in their C code without recompiling the
>functions listed above. Could people reply to rburrows AT hotmail DOT com
First of all, this is not a DJGPP-specific question and might be better
posted to comp.lang.c. Now that that's out of the way... :)

Typically, you *declare* your functions and variables in a header file which
the user includes. Then you *define* them in separate source file(s), which
is linked with the finished product.

For your example:
--file mylib.h--
extern int myvar1, myvar2, myvar3; /* extern is cosmetic */
extern void function1(void);
extern void function2(void);
--end--

--file mylib.c--
#include "mylib.h" /* so the compiler can check that it matches */
int myvar1, myvar2, myvar3;
void function1(void) { stuff }
void function2(void) { stuff }
--end--

mylib.c can be precompiled into mylib.o. The user #includes "mylib.h", uses
the functions and variables to their heart's content, and links with mylib.o.

Efficiency note: You might want to split the code up so that there is one
function per source file, then `ar' them all into a library. This lets the
linker include only the functions that are used (the smallest piece it
includes is one .o file).

Hope this helps.

Nate Eldredge
eldredge AT ap DOT net



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019