|
Re: Problem with macro defined in C file and referenced in header file [message #652089 is a reply to message #652054] |
Wed, 02 February 2011 16:29 |
John McCabe Messages: 228 Registered: July 2009 |
Senior Member |
|
|
On Wed, 02 Feb 2011 09:28:11 -0500, StevenW <qch1843@gmail.com>
wrote:
>I am using Eclipse Helios and I have a C project which defined some macro in C files and reference them in some other header files. But, it looks like the CDT doesn't think those macro are defined.
>such as,
>sample.c
>
>#define MY_MACRO
>..
>
>
>sample.h
>...
>#ifdef MY_MACRO
>.. (CDT thinks MY_MACRO is not defined and mark these codes as inactive)
>#endif
>
>Is there anything wrong in my C project?
I had a similar problem recently but I don't think there's anything
that can be done about it other than, if you go to the C/C++ Paths and
Symbols and defined the macro n there.
The problem is that there is nothing to directly tie a header file to
a source file in C (unlike Ada, where you define a package spec and a
package body and the compiler or editor can tie the two together
because of the package name). That means that the editor (or parser)
can't know when you're editing a .h that actually you're editing it as
though it were already included in a particular .c.
Let's say, based on your example, we had:
sample.h
====
#ifdef MY_MACRO
<blah>
#endif
<blah>
sample.c
====
#define MY_MACRO
#include "sample.h"
<blah>
sample2.c
====
#ifdef MY_MACRO
#undef MY_MACRO
#include "sample.h"
How can the editor determine whether you're editing sample.h as if it
were already included in sample.h (where MY_MACRO is defined) or
sample2.c (where MY_MACRO is explicitly undefined)?
As I said, I have the same issue. The best thing to do would be not to
write your code like that :-) If you've got part of a header that's
only applicable to certain source files, and other bits that are
common, then perhaps the specific stuff should be extracted to a
different header!
|
|
|
|
|
|
|
Re: Problem with macro defined in C file and referenced in header file [message #652861 is a reply to message #652667] |
Mon, 07 February 2011 12:13 |
John McCabe Messages: 228 Registered: July 2009 |
Senior Member |
|
|
On Fri, 04 Feb 2011 21:42:21 -0500, StevenW <qch1843@gmail.com> wrote:
>Actually, my situation is much simpler. As I mentined previously I have a macro defined and referenced as below,
>
>sample.c
>...
>#define MY_MACRO
>..
>
>sample.h
>...
>#ifdef MY_MACRO
>..
>#endif
>
>In Eclipse, those part within "#ifdef" is marked as inactive code. In Source Insight, those part are marked as active code.
In Source Insight what happens if you remove the #define from sample.c
and reload the project? Is the #ifdeffed code still marked as active?
I understand what you're saying but what I'm getting at is that, as
there is no definitive link between sample.c and sample.h (i.e.
sample.h could be called zzz.h but still be included in sample.c) the
possibilities are that:
1) Source Insight has parsed the whole project, found that sample.h is
ONLY included from sample.c, and therefore marked that section of code
as active
2) Source Insight just marks it as active when it can't resolve the
#define fully (and Eclipse marks it inactive in that case).
The reason I was asking about other files was to try to see what
Source Insight's behaviour was in the case where it can't know whether
you're looking at the include file as if it's included in file A
(where MY_MACRO is defined) or file B (where it isn't defined). If
Source Insight still marked the code as active, then clearly they've
chose to mark it that way if they can't resolve the macro whereas the
CDT developers seem to have chosen to go the other way.
|
|
|
|
|
|
Re: Problem with macro defined in C file and referenced in header file [message #806132 is a reply to message #778305] |
Fri, 24 February 2012 15:23 |
Eclipse User |
|
|
|
On 1/12/2012 4:51 AM, Gluca dibari wrote:
> Hello everybody,
> I have more or less the same problem as steven and I'm not able to fix it;
> It seems that CDT is not able to trace the macro definiton troughout *.c
> files and *.h files.
> I'm dealing with an already developped code and I would like start
> developping my modifications using Eclipse, but the indexer marks
> hundres of errors, here it is my example:
>
> sample.h
> ====
> #ifdef MY_MACRO
> unsigned char myVar = 1;
> #endif
>
> mySample.c
> ====
> #define MY_MACRO
> #include "sample.h"
> ..
> myvar++;
> ..
>
> yourSample.c
> ====
>
> #include "sample.h"
> unsigned char myVar = 0;
> ..
> myVar++;
> ..
>
> The indexer marks me myVar as undefined in mysample.c
> Can anyone help me to fix this?
IMHO, it's such an odd coding style that it should be fixed so future
maintainers won't have the same problems you do.
Typically, you define a variable in the .c code. You declare it in a
header. For a global, the header would say 'extern'.
You'll have a problem when mysample and yoursample are linked together.
Global variables also lead to multi-threading problems.
|
|
|
Powered by
FUDForum. Page generated in 0.03486 seconds