Mail Archives: djgpp/1999/09/28/23:49:15
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
<alexmdc AT virtualis DOT com> 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/
- Raw text -