Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is the best external interface for a library file?

Status
Not open for further replies.

JasonWB007

Programmer
Dec 21, 2005
14
0
0
US
Hi all,

I am in the process of reworking some of my code so that some of the more resuable functions are collected into libraries that will be linked in with multiple applications. One of these functions is a serial port driver and the way it is set up, it requires access to several flags and also a timer that is incremented by an ISR outside of the serial port driver library.

My question is this - what is the best and most proper way to define an interface to this .lib file that is linked in with the final code. Specifically:

1. Should I strive to eliminate variables accessed within the libray that need to be defined externally?
2. If I have large (static) buffers that need to be defined (e.g. int bigbuf[BUF_SIZE]), should this best be done internally to the library or externally and have pointers passed to the library routines.
3. Up until now, I have had three flags that control execution (e.g. OutputBusyFlag, etc.) How should I handle these?

Thanks,

Jason
 
Hi Jason,

I think it's not really that important (from the code point of view) whether your variables or static buffers are defined inside the lib or not. Since you want to build a static lib (no *.dll, or *.so) the resulting executable won't care in which object file the variables were defined.
But for the sake of readability and modularity I would prefer to let the library code handle it's memory usage. Generally accessing external variables is an ugly thing. Better to provide functions, which set the values the library depends on, and stores them in statically linked library variables.
Put all the function prototypes and flag definitions (enums or #defines) in a header file <name_of_library.h>. This header file makes up the interface to your lib.

Best regards,
Tobi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top