Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » Problem with macro defined in C file and referenced in header file
Problem with macro defined in C file and referenced in header file [message #652054] Wed, 02 February 2011 14:28 Go to next message
Steven is currently offline StevenFriend
Messages: 3
Registered: February 2011
Junior Member
Hi,

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?
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 Go to previous messageGo to next message
John McCabe is currently offline John McCabeFriend
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 #652147 is a reply to message #652089] Wed, 02 February 2011 18:53 Go to previous messageGo to next message
Andrew Gvozdev is currently offline Andrew GvozdevFriend
Messages: 257
Registered: July 2009
Senior Member
AFAIK the indexer parses a header only once so you won't be able to interpret it differently.

Andrew
Re: Problem with macro defined in C file and referenced in header file [message #652204 is a reply to message #652054] Thu, 03 February 2011 05:11 Go to previous messageGo to next message
Steven is currently offline StevenFriend
Messages: 3
Registered: February 2011
Junior Member
John & Andrew,

Thanks a lot for the reponse! I know I can manually define those macro in Path and Symbols and that would resolve the problem. I am just curious why Source Insight could handle this type of macro defines correctly.
Re: Problem with macro defined in C file and referenced in header file [message #652249 is a reply to message #652204] Thu, 03 February 2011 10:32 Go to previous messageGo to next message
John McCabe is currently offline John McCabeFriend
Messages: 228
Registered: July 2009
Senior Member
On Thu, 03 Feb 2011 00:12:24 -0500, StevenW <qch1843@gmail.com>
wrote:

>John & Andrew,
>
>Thanks a lot for the reponse! I know I can manually define those macro in Path and Symbols and that would resolve the problem. I am just curious why Source Insight could handle this type of macro defines correctly.

That's interesting about Source Insight. Are you saying that when
you're in a C++ file in Source Insight and you traverse to the
declaration the code is marked as not-#ifdeffed-out? If you do that
from two separate files that both include that .h and have the macro
set differently? What now happens if you just open the file as opposed
to navigating to it from a declaration or whatever? What about if you
just navigate to the window that the .h is open in? How does that deal
with such #defines?

It's a little while since I used Source Insight as I hadn't the time
to set up the options to make it not garish, and then I discovered
Eclipse :-)
Re: Problem with macro defined in C file and referenced in header file [message #652667 is a reply to message #652054] Sat, 05 February 2011 02:42 Go to previous messageGo to next message
Steven is currently offline StevenFriend
Messages: 3
Registered: February 2011
Junior Member
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.
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 Go to previous messageGo to next message
John McCabe is currently offline John McCabeFriend
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 #653433 is a reply to message #652054] Wed, 09 February 2011 18:12 Go to previous messageGo to next message
Nobody Mising name is currently offline Nobody Mising nameFriend
Messages: 75
Registered: July 2010
Member
Rather interesting thread about CDT approach to C++ code parsing. I think that In general there should not be active/inactive code, i.e. indexing should parse all available configurations.

For example Xrefactory extension for Emacs parses all variants and provides navigation/references in all preprocessor branches. And they use EDG C++ front end for parsing, so currently it is a lot more reliable for indexing C++ sources than CDT is. But it is rather slow in comparison with CDT.
Re: Problem with macro defined in C file and referenced in header file [message #653584 is a reply to message #652054] Thu, 10 February 2011 10:46 Go to previous messageGo to next message
elvin11  is currently offline elvin11 Friend
Messages: 5
Registered: February 2011
Junior Member
It is not a much bigger problem as you think. You can do nothing to directly tie a header file to a source file. In my opinion, now what you can do is define the macro in C/C++ Paths and Symbols. Recheck the header coding also. Some slight changes are needed there also.
Re: Problem with macro defined in C file and referenced in header file [message #778305 is a reply to message #652054] Thu, 12 January 2012 09:51 Go to previous messageGo to next message
Gluca dibari is currently offline Gluca dibariFriend
Messages: 1
Registered: January 2012
Junior Member
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?

Thanks a lot.


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 Go to previous message
Eclipse UserFriend
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.
Previous Topic:limit breakpoint scope to only current project or debug configuration
Next Topic:How to see all unresolved names for the project?
Goto Forum:
  


Current Time: Fri Apr 19 11:25:51 GMT 2024

Powered by FUDForum. Page generated in 0.03486 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top