[CMake] Eclipse generator - basic macros

Hendrik Sattler post at hendrik-sattler.de
Wed Jul 15 13:43:28 EDT 2009


Am Mittwoch 15 Juli 2009 17:21:38 schrieb Mike Jackson:
> On Wed, Jul 15, 2009 at 2:18 AM, Hendrik Sattler<post at hendrik-sattler.de> 
wrote:
> > Zitat von Benjamin Schindler <bschindler at inf.ethz.ch>:
> >> I'm working on a project which builds both on linux and windows. I
> >> generated an eclipse project out of it which works basically fine but
> >> it's not able to recognize i.e. the __GNUC__ macro (and probably any
> >> other macro defined per default on gcc) are not recognized by eclipse.
> >> That means that by using a header like:
> >>
> >> #if defined(_MSC_VER) && (_MSC_VER >= 1300)
> >> #ifdef FLOW_DLL_EXPORT
> >> #define FLOW_DLL _declspec(dllexport)
> >> #else
> >> #define FLOW_DLL _declspec(dllimport)
> >> #endif
> >> #else
> >> #ifdef __GNUC__
> >> #define FLOW_DLL
> >> #endif
> >> #endif
> >
> > I know that it's unrelated to your question but this code is slightly
> > wrong. For the windows version of gcc, it also understands the
> > _declspec() thing. You don't make it compiler-specific but
> > system-specific.
> > Additionally, on non-windows systems, you can use the visibility
> > attribute to achieve something similar.
> > That's why the gcc people suggest the following at
> > http://gcc.gnu.org/wiki/Visibility (I simplified it):
> > #if defined _WIN32 || defined __CYGWIN__
> >  #ifdef FLOW_DLL_EXPORT
> >    #define FLOW_DLL __declspec(dllexport)
> >  #else
> >    #define FLOW_DLL __declspec(dllimport)
> >  #endif
> > #elif __GNUC__ >= 4
> >  #define FLOW_DLL __attribute__ ((visibility("default")))
> >  #define DLL_LOCAL  __attribute__ ((visibility("hidden")))
> > #endif
> > #ifndef FLOW_DLL
> > #define FLOW_DLL
> > #endif
> > #ifndef DLL_LOCAL
> > #define DLL_LOCAL
> > #endif
> >
> > You only need to add -fvisibility=hidden to gcc command line on
> > non-windows systems.
> > The above still has the flaw you use _declspec(dllimport) when compiling
> > the library as static library. That's ok if you don't intend to do so
> > else easy to fix.
> >
> > Additionally, eclipse may not select the right choice but the macros will
> > not error out as they are _always_ defined in some way.
> >
> > Have fun
> >
> > HS
>
> I think I need some explanation on this logic.
>
> This line:
>
> #if defined _WIN32 || defined __CYGWIN__
>
> will always be true on msvc, mingw and cygwin because _WIN32 is
> defined for the MS compilers and when MinGW is being used so the
> "#elif" will _never_ get executed? Where am I going wrong on this?

You don't. It is always right on Windows, at least with all MSVC and gcc 
compilers. Using any MSVC older that 7 (.NET 2003) is insane and that works 
there.
The elif case if for systems like Linux with GCC >= 4.

HS



More information about the CMake mailing list