Why does Eclipse CDT say: 'syntax error', but compilation no problem

前端 未结 10 1385
庸人自扰
庸人自扰 2021-02-04 08:04

I am working in existing C code which has a couple of lines with statements similar to this one:

struct collect_conn *tc = (struct collect_conn *) 
     ((char *         


        
相关标签:
10条回答
  • 2021-02-04 08:37

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

    For example if i type

    #ifndef __cplusplus
    #endif
    

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

    Unfortunatly i dont find a fix.

    0 讨论(0)
  • 2021-02-04 08:38

    It seems the CDT parser doesn't like the portion offsetof(struct ...). If you declare collect_conn using a typedef the error goes away. At least for me, the following code 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 do something like this:

    typedef struct collect_conn collect_conn_t;
    
    0 讨论(0)
  • 2021-02-04 08:40

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

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

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

    0 讨论(0)
  • 2021-02-04 08:43

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

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

    #define __offsetof__(x) (x)
    

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

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

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

    0 讨论(0)
  • 2021-02-04 08:45

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

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

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

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

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

    0 讨论(0)
  • 2021-02-04 08:45

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

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

    0 讨论(0)
提交回复
热议问题