Re: To include or not to include.... C language!
Robby wrote:
Hello,
As you all know [..]
Well, I didn't know that, but it probably doesn't matter.
The error is:
1>C:\_DTS_PROGRAMMING\C_PROGRAMMING\c\C_TESTS\C_FILES\CFILES\Debug\CFILES.exe : fatal error LNK1169: one or more multiply defined symbols found
The thing is that even if I double click on the error, VC++ won't take me to
the line with the problem ????
Where would it take you? It's a multiple definition of a symbol.
Here are the 6 files:
====================================KERNEL.h
enum enumKM{KM_QUIT = 0, KM_CREATE = 1, KM_RECUR};
// Enumerate switches 1 to 8 according to their hardware binary input
enum e_keys18 {e_tsFOUR=127, e_tsTHREE=191, e_tsTWO=223, e_tsONE=239,
e_btFOUR=247, e_btTHREE=251, e_btTWO=253, e_btONE=254 };
// Enumerate switches 9 to 12 according to their hardware binary input
enum e_keys912 {e_btNINE=254, e_btTEN=253, e_btELEVEN=251, e_btTWELVE=247};
long MQ[]={1};
Here you're defining an array whose name has external linkage. As soon
as you include this header in more than one .c file, you get multiply
defined symbol. Don't do that. Definitions belong to the .c files, not
to headers.
long KERNEL_get_msg();
void KERNEL_update_all_inputs();
=========================================KERNEL.c
#include <stdio.h>
#include "KERNEL.h"
> [..]
======================================MAIN.h
====================================API.c
#include "API.h"
#include "KERNEL.h"
[..]
There you go!...
===================================INPUT.h
[..]
QUESTIONS:
1) This including stuff is a mess *to me that is* I still don't see the
advantage of including every .h files in .c files.
There is probably no advantage. Include only the headers you actually
need in that module.
> Why can't we just put
everything that we need to declare in KERNEL.h and make sections denoted by
comments to show the relativity of the declartations?
Uh... I don't know. You would have to decide for yourself.
> But then we would need
to include KERNEL.h in very .c file right? and this would give duplication
errors at compile time right ??? confused, confused, confused!
Not at compile time, but at link time (and that's what you get). But
that is easy to fix: just don't put *definitions* in headers, only
*declarations*.
2) Keeping the program the way it is, what modification can I do to get rid
of the error?
Well, it bears repeating I guess: put only *declarations* in headers.
3) Is the following function okay?
===================================
void API_InsertMessage(enum enumKM m)
{
long MQ_LOC;
MQ[MQ_LOC] = m;
}
==================================
No. Not OK by a mile. First off, you don't initialize 'MQ_LOC' and
immediately attempt to use it as the index. That's a disaster waiting
to happen. Second, what's 'MQ'? What's 'enumKM'? Did you define
those? Are they known to the compiler at this point?
The reason I am asking is that MPLAB generates the following error:
API.c:3: error: parameter `m' has incomplete type
Perhaps you forgot to include the header that defines 'enumKM' type...
Why would MPLAB complain but VC++ doesn't.... Just though I would mention
that and I know that this question does not pertain to this forum!
But this all used to work without errors in compiler xxx. This makes me
wonder how many things will I have to fix !
From the looks of it, enough to keep you busy for a while. But isn't
it actually good? I mean, guaranteed employment an so on...
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask