[ACCEPTED]-Why does Eclipse CDT say: 'syntax error', but compilation no problem-eclipse-cdt

Accepted answer
Score: 28

Eclipse CDT contains its own preprocessor/parser 27 for analyzing your code and building an 26 index. However, when you invoke a build 25 CDT calls out to your system compiler, like 24 gcc for example. There may be minor differences 23 between the syntax accepted by the CDT parser 22 and the syntax accepted by your compiler. When 21 this happens the CDT parser can get confused.

On 20 my system the offsetof macro expands into an expression 19 that uses the __offsetof__ keyword. This keyword isn't 18 recognized by CDT so that's why there's 17 a syntax error. To deal with this problem 16 the CDT parser has a macro built in to deal 15 with __offsetof__ which looks like this:

#define __offsetof__(x) (x)

This doesn't 14 appear to be correct, at least on my system 13 the result is the removal of the __offsetof__ keyword 12 from the source which still leads to a syntax 11 error.

I was able to get rid of the syntax 10 error by going to the Paths and Symbols 9 property page and adding a macro for __offsetof__ which 8 maps to 'foo'. This tricks the parser into 7 thinking its just a call to a function it 6 hasn't seen before, but not a syntax error.

Alternatively 5 you can turn off syntax error reporting 4 in the editor by going to Window > Preferences 3 > General > Editors > Text Editors > Annotations 2 and unchecking all the checkboxes for C/C++ Indexer 1 Markers.

Score: 10

I've fixed problem in eclipse CDT with Preferences->C/C++->Language 2 Mappings : Add Content Type : C-header Language 1 : C++

Score: 8

Sometimes, although the code compiles with 20 no error, eclipse CDT's real-time code analyzer 19 shows some errors in C/C++ files (eg. 'Function 18 xxx could not be resolved). This is because 17 eclipse CDT uses its own preprocessor/parser 16 for analyzing the code and building the 15 indexes instead of the MinGW's one (or any 14 other GNU compiler). In order to fix this 13 globally for all eclipse projects in the 12 workspace, follow these steps: (In order 11 to fix this only for a specific project, follow 10 steps 1, 2 and 4 in menu 'Project->Preferences')

1-In menu 'Window->Preferences->C/C++->Language Mappings', add 9 the correct mappings as shown in below: (eg. for 8 content types: C++ Source/Header File, use 7 GNU C++ language and so on) Global Language Mappings Settings

2-In menu 'Window->Preferences->C/C++->Indexer', set 6 full indexing by checking all checkboxes 5 (but not 'Skip' ones) as shown in below: Global Indexer Settings

3-In 4 each project's specific properties, menu 3 'Project->Properties->C/C++ general->Indexer', Uncheck 'Enable project specific settings' as 2 shown in below: Project Indexer Settings

4-Rebuild the indexing, menu 1 'Project->C/C++ Index->Rebuild'.

Score: 5

It seems the CDT parser doesn't like the 5 portion offsetof(struct ...). If you declare 4 collect_conn using a typedef the error goes 3 away. At least for me, the following code 2 works:

typedef struct  {
   struct runicast_conn runicast_conn;
   struct announcement announcement;
   const struct collect_callbacks *cb;
   struct ctimer t;
   uint16_t rtmetric;
   uint8_t forwarding;
   uint8_t seqno;
} collect_conn;
struct collect_conn *tc = (struct collect_conn *)
     ((char *)c - offsetof(collect_conn, runicast_conn));

If you can't change the original declaration 1 do something like this:

typedef struct collect_conn collect_conn_t;
Score: 2

It might be confused, check if you have 8 a definition of offsetof in-scope, for instance. Otherwise 7 you might try simplifying the expression, breaking 6 it up using e.g. a #define with the offset of, or something.

I'm 5 thinking the compiler might provide a built-in 4 version of offsetof, while Eclipses's compiler/code-parser 3 might not. If so, you would need to make 2 sure you have the definition, for Eclipse 1 to be able to properly parse your code.

Score: 2

try switching the indexer to "Full c/C++ indexer 1 (complete parse)" in Preferences->c/C++ -> indexer

Score: 1

Iv got the same problem. There is 2 definition 5 of offsetof (one for C and one for C++). IMO 4 the problem come from that

For example if 3 i type

#ifndef __cplusplus

Eclipse will grey it. It mean __cplusplus 2 is defined, but my project is a C

Unfortunatly 1 i dont find a fix.

Score: 1

I fixed similar problem after checking the 3 tab Error Parsers in Makefile Project in 2 New CDT Project Wizard, removing CDT Visual 1 C Error Parser (I am using gcc)

Score: 1

I ended up solving the problem like this. First 8 I opened the project properties, then the 7 C/C++ general->Paths and Symbols category. Under 6 the Symbols tab I added this entry:

Symbol: offsetof(TYPE,MEMBER)
Value: ((ssize_t) &((TYPE *)0)->MEMBER)

These 5 symbols are used by the indexer but not 4 passed to the compiler (at least in Makefile 3 projects, I haven't tried it in the other 2 kind of C project), so it doesn't override 1 GCC's built-in offsetof

Score: 0

I've seen Eclipse do this some times, and 5 I use it for Java. Usually closing and opening 4 the file again fixes it for me (resets whatever 3 is wrong). It usually seems to be an error 2 that WAS there but has been fixed and the 1 "error cache" isn't updated correctly.

More Related questions