0

My problem is very much related the post Defined macro not recognized

I wrote a CMakeLists file to be able to build my project for OS X (Eclipse mostly but sometimes used Xcode) and Windows (Visual Studio). The issue showed up today when creating my project for Windows + MinGW. I was using the defined _WIN32 to enable some functions when I was in windows, i.e.

bool Normal::HasNaNs() const
{
#ifdef _WIN32
    return _isnan(x) || _isnan(y) || _isnan(z);
#else
    return isnan(x) || isnan(y) || isnan(z);
#endif
}

However, with the combo Eclipse+MinGW the code is entering the ifdef part instead of (what I was hoping/thinking should be correct) entering the else part. I think Visual is the only one having the _isnan() function. So, what would be a more robust way to check for Windows+VS, Windows+MinGW, OSX ?

Community
  • 1
  • 1
BRabbit27
  • 6,333
  • 17
  • 90
  • 161
  • Your other question was about a non-standard extension (`M_PI` isn't part of C). `isnan` IS part of C++, but C++11. If you are using CMake, these preprocessor defines are the entirely wrong approach. Why aren't you doing this at the CMake level? – uh oh somebody needs a pupper May 09 '16 at 02:20
  • I read just enough to get my CMake file going to create projects. My project has changed and new needs have arisen. Could you provide a link where I can read and learn how to achieve this at CMake level? – BRabbit27 May 09 '16 at 02:24
  • Everything you wanted to know but was afraid to ask: [Pre-defined Compiler Macros](https://sourceforge.net/p/predef/wiki/Home/) – Ivan Aksamentov - Drop May 09 '16 at 03:31
  • Related question: http://stackoverflow.com/questions/10046114/in-cmake-how-can-i-test-if-the-compiler-is-clang – Antonio May 09 '16 at 14:15

1 Answers1

2

_MSC_VER is the best way to check if you're being compiled by Visual Studio:

https://msdn.microsoft.com/en-us/library/b0084kay.aspx

For checking for MinGW (please see comment for more info), you can use:

#ifdef __GNUC__
#ifdef __MINGW32__

as per this answer:

https://stackoverflow.com/a/17493838/493106

and then __APPLE__ for os x.

https://stackoverflow.com/a/2166491/493106

Community
  • 1
  • 1
xaxxon
  • 19,189
  • 5
  • 50
  • 80
  • Note that `__GNUC__` is [also defined for llvm](http://stackoverflow.com/a/1959615/2436175), so it's not always sufficient to understand unambiguously if the compiler is really gcc/g++ (although here we are speaking more specifically about MinGW) – Antonio May 09 '16 at 14:13
  • Good to know, I'll point to your comment in the answer just in case anyone wasn't looking.. – xaxxon May 09 '16 at 19:03