Richard Heathfield wrote:
> Hal Vaughan said:
>
>> I know I saw the answer to this in a FAQ somewhere in the past week or
>> two, but now tat I need to know it, I can't find which FAQ it was in.
>>
>> I have a program that's grown to several files and I was compiling it
by
>> just using #include where necessary
>
> Hal, you need to find yourself a large hammer, preferably one with
> "ANAESTHETIC" written along the side... :-)
>
> Never use #include for source (admittedly for certain values of "never",
> but this is something that expert programmers are very loathe to do
except
> on very special occasions. You may deduce that newbies should not be
doing
> it *at all* - and it's certainly not necessary for them to do so).
>
>> How can I make sure several files can access the functions in a
separate
>> file without getting repeated definitions?
>
> In the source, you simply declare (but not define) the functions in a
> header, and #include the header into the sources that need to call those
> functions (and the source that defines them, as a safety check).
>
> To make the files all work together, you have to "link" them.
>
> In gcc, it works like this:
>
> gcc ...lots of flags and stuff... -c -o foo.o foo.c
> gcc ...lots of flags and stuff... -c -o bar.o bar.c
> gcc ...lots of flags and stuff... -c -o baz.o baz.c
>
> -c means "just compile, don't try to link", and translates source files
to
> object files, but you don't get a program yet...
>
> gcc ...lots of flags and stuff... -o myprog foo.o bar.o baz.o
Now that I've done this, there's one point I'm still having problems with.
I have a file with a function like this:
void setour****t(string);
I'm calling it from another file, so I get this error:
hdlinuxio.h:5: error: variable or field ?setour****t? declared void
hdlinuxio.h:5: error: ?string? has not been declared
I notice the error is for the header file, not the source file. I've
experimented and it seems whenever I'm using a function with a string as a
parameter and I'm calling it from one file when it's in another, I get
this
as a problem. I tried adding "#include <string>" to the start of the
header file and it makes no difference.
Why won't it let me use a function with a string as a parameter in another
file?
Thanks!
Hal


|