1
votes

I am developing a C application, and using Eclipse CDT IDE, which I find great. The project uses Glib,Gtk,and GStreamer , so whenever I use some of their features in a file, I need to include:

#include <glib.h>
#include <gtk/gtk.h>
#include <gst/gst.h>

The code compiles without any error, since the PATH variable to search those headers is set correctly in a CMakeLists.txt. However, while working on the project, I found annoying errors highlighting in my code, regarding type definitions like gchar or GValue or GTKApplication; the error outlined is "symbol **** could not be resolved". These definitions are inside a header file that my Eclipse IDE cannot find (included by glib.h), if not at compile time (indeed the program compiles correctly). Instead, the type GError , defined in gst.h , is not highlighted as an error by the pre-compiler.

I would like then that my Eclipse IDE could search on nested headers (#include inside an #inlcude inside...) to find those type definition, in order so to not have those annoying errors highlighting. How can I do so? I would not like to have to include directly all files where the type definitions are done.

EDIT: As Jonah Graham outlined, the problem is not beacuse Eclispe does a "single-step research" on the headers, since it inspects includes inside other includes like any other IDE. It is a CMake bug with c and Eclipse

Thanks in advance.

1
I may be able to write a longer answer, but I am not exactly sure which problem you are having. AFAICT it looks like CDT is failing to find the details of where glib.h is, or perhaps some similar stuff related to built-in compiler macros. The manual 1 2 has info on setting up scanner discovery. - Jonah Graham
Or you may want to consider one of the cmake plug-ins, however I don't use them, so YMMV. - Jonah Graham
Eclipse finds glib.h surely, the problem is that types defined in headers included by glib.h are not considere by Eclipse CDT because it does a "one-step research". Indeed, for instance, if I directly include in a source file the header where GValue is defined, the error highlighting about GValue goes away. But I want Eclipse to do this automatically, by expanding the headers included in glib.h, and not to have to include all the necessary headers (would be too many) - Carlo Benussi
No, CDT does not do "one-step research". CDT of course inspects includes of includes otherwise none of even the standard header files would work. If you pop your project on github I can have a look to see if I can reproduce your problem. - Jonah Graham
Ok, It is already on Github: project. - Carlo Benussi

1 Answers

3
votes

The problem you are facing is a CMake bug*. CMake adds __cplusplus into the defined symbols unconditionally, which means that glib headers are not parsed properly when in C mode. You can see this clearly by opening gmacros.h around the definition for G_BEGIN_DECLS:

enter image description here

Because CMake told CDT __cplusplus is defined, it thinks G_BEGIN_DECLS is also defined, which makes code like this from gtypes.h parse incorrectly:

G_BEGIN_DECLS

/* Provide type definitions for commonly used types.
 *  These are useful because a "gint8" can be adjusted
 *  to be 1 byte (8 bits) on all platforms. Similarly and
 *  more importantly, "gint32" can be adjusted to be
 *  4 bytes (32 bits) on all platforms.
 */

typedef char   gchar;
...

Of course with no gchar defined, everything else is going to go badly.

Luckily there is a quick workaround until the problem is resolved in CMake, remove __cplusplus from the info in CDT.

  1. Open Project Properties
  2. C/C++ Include Paths and Symbols
  3. Remove __cplusplus from the list and press OK

enter image description here

  1. (sometimes necessary) Right-click on project -> Index -> Rebuild

* There may be some other workarounds if you know CMake better. The bug says also it will be fixed for the next release of CMake.