From: DavMac AT iname DOT com (Davin McCall) Newsgroups: comp.os.msdos.djgpp Subject: Re: Newbie Question Date: Wed, 29 Sep 1999 02:02:30 GMT Organization: Monash Uni Lines: 103 Distribution: world Message-ID: <37f170d7.1796300@newsserver.cc.monash.edu.au> References: <37F065E4 DOT 2C3B57D5 AT virtualis DOT com> <37F0EF05 DOT B46FB474 AT crosswinds DOT net> <37F16793 DOT 4D98B7CE AT virtualis DOT com> NNTP-Posting-Host: damcc5.halls.monash.edu.au X-Trace: towncrier.cc.monash.edu.au 938570525 26238 130.194.198.138 (29 Sep 1999 02:02:05 GMT) X-Complaints-To: abuse AT monash DOT edu DOT au NNTP-Posting-Date: 29 Sep 1999 02:02:05 GMT X-Newsreader: Forte Free Agent 1.1/32.230 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com You need to link both your "prog.c" and "subs.c" together. So you could use a line like this to compile: gcc prog.c subs.c -o prog.exe This of course can be a right pain if there are lots of little "subroutine" files (you're not coming from a BASIC background are you?). It may be a good idea to create a library of common routines. You do this by compiling all of them, but not linking: (after copying all files to current directory, which is a newly created directory:) gcc -c *.c And then: ar qcs libmylib.a *.o "libmylib.a" is the name of the library. You should generally always give it the "lib" prefix and the ".a" suffix. Once this is done, you can copy the newly created library into the same directory as your programs, and to link with it: gcc prog.c -lmylib -o prog.exe (You may also need to specify "-L." to indicate that the current directory should be searched for libraries, I'm not sure). Note that you specify the library name *without* the "lib" prefix and ".a" suffix. The good thing about this is that only the *needed* files from the library are linked into your program. Davin. On Wed, 29 Sep 1999 11:12:51 +1000, Alex Mendes da Costa wrote: >Okay, so now I have a header file with my routines declared and globals >and stuff called "subs.h" and I have a c program containing all these >subs called "subs.c" and I have a program called "prog.c" which contains >the line '#include "subs.h"'. I try to compile this using GCC (ie gcc >prog.c -o prog.exe) and I get error messages for all my subs which say: >c:/djgpp/tmp\cccqmssf(.text+0x123):prog.c:undefined referance to 'mysub' >or something similar. > >I know I'm probably doing something really dumb but I can't figure it >out! > >Thanks, >Alex > >Ishpeck wrote: >> >> It's very simple. A header file is just a source file with the .h >> extension. When you import header, you'll usually go like so: >> >> #include "header.h"//Notice, it's in quotes, not <> thingies >> >> If your header is in a different directory as your source file, go like >> this. >> >> #include "other/dir/header.h" >> >> Voila! It's done! >> >> Inside your header, you might want it all within a special if statement, >> just so you don't over-declare anything. Like so >> >> #ifndef __MY_HEADER_NAME__ //The underscores are an arbitrary detail >> #define __MY_HEADER_NAME__ >> >> //Header goes in here: >> //Function prototypes >> //Global variables >> //Constants, etc. >> >> #endif >> >> After that, you just treat it like a normal source file. >> >> Alex Mendes da Costa wrote: >> > >> > Hi All. >> > I know a bit about C programming (i.e. the basics) and wrote a list of C >> > routines that I want to be able to save seperately and then use them in >> > other programs I write. I don't know how to write a header file or >> > invoke the routines with it. Please Help!! >> > >> > Thanks, >> > Alex >> >> -- >> Those who seek enlightenment >> may turn to Ishpeck the wise. >> http://come.to/ishpeck/ __________________________________________________________ *** davmac - sharkin'!! davmac AT iname DOT com *** my programming page: http://yoyo.cc.monash.edu.au/~davmac/