Peter L. Peres [plp at ACTCOM.CO.IL] says
The keys to larger C projects are header files and build scripts.The bug that causes you trouble is a very sane and useful ANSI C feature: Symbols declared in a module are not visible to other modules unless they are declared in the module that uses them as external or as function prototypes.
For example a function int foo(void); shall appear as int foo(void); in the main file, and a global variable int bar shall appear as extern int bar; in same. Of course you don't write them there, you include a library header file instead.
In other words, when making a library, you put the functions in the library, and prototypes for all the public functions in it, and 'external' definitions for all the public data in it, in a corresponding header file (usually called like the libarary but with suffix .h). The IMPORTANT part is that there should be NO actual functions NOR data declarations in the header. Anything not declared in the header will remain private to the libary. However some compilers do not like name clashes between same-named symbols located in different libraries (even if they are not public).
Then in your main program, you #include "path/to/header" and the functions are visible. The library is usually compiled separately or automatically (it's a DLL of sorts at the object file level). You must understand the semantics of the double ticks (as opposed to angled brackets) here, and apply some serious path rewriting for DOS under certain circumstances.
Now you need to tell the compiler about all the files it needs to build the executable. Sometimes you tell this the linker. In other words, pass all the required libraries (already compiled into relocatable object form) to the linker, or pass all the source file names to the compiler. I don't know what compiler you are using but sane (i.e. Unixish) compilers allow you to string everything onto the command line. DOS compilers have a limit on the command line length so they often use a command file which contains the actual list of files to use.
A tool that does almost all of this (the invocation of the toolchain parts) for you is called a make tool and YOU WANT ONE BADLY. There are free ones and there are non free ones. The 'standard' make tool is called... make. See Linux... Another make tool is a batch script of whatever shell you are using (yes, you can do it in DOS if you HAVE to).
See:
file: /Techref/language/ccpp/largeproj.htm, 3KB, , updated: 2006/4/26 21:57, local time: 2024/12/7 22:11,
18.97.14.80:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://www.piclist.com/Techref/language/ccpp/largeproj.htm"> The C and C++ Programming Languages </A> |
Did you find what you needed? |
PICList 2024 contributors:
o List host: MIT, Site host massmind.org, Top posters @none found - Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr. |
Welcome to www.piclist.com! |
.