FWGS / xash3d

DEPRECATED in favor of https://github.com/FWGS/xash3d-fwgs. Only bugfixes are accepted.
https://xash.su
GNU General Public License v3.0
551 stars 107 forks source link

_DEBUG preprocessor #define is not present on non-Windows platforms #371

Closed noodlecollie closed 6 years ago

noodlecollie commented 6 years ago

#ifdef _DEBUG code doesn't seem to be included for me when I build for Mac in debug mode. I haven't had a chance to check it with Linux yet, but I suspect the issue is the same.

I've read up on how Cmake handles the debug configuration for makefiles, and apparently passing -DCMAKE_BUILD_TYPE=Debug means that NDEBUG is then not defined. My current workaround, therefore, is to add the following to somewhere like common.h:

#if !defined(_WIN32) && !defined(NDEBUG)
#define _DEBUG
#endif

Is this correct, or is there a recommended way?

a1batross commented 6 years ago

_DEBUG macro is a one of many leftovers from original Xash3D code. Yes, for compability it can be defined in such way. I don't know though about MinGW.

IMHO, such macro shouldn't be used in code(except assertions, maybe). Otherwise, it may lead to difference between Release and Debug builds.

a1batross commented 6 years ago

NDEBUG though, isn't guaranteed to be defined. I remember, that this macro isn't defined by compiler itself, instead it always done on buildsystem side. And Android.mk does not define it sometimes.

Maybe _DEBUG can be set in CMake/Android.mk directly.

noodlecollie commented 6 years ago

I was planning to use it so that certain bits of my code deliberately won't be available in release builds. I will look into defining it in the CMake files though.

noodlecollie commented 6 years ago

In addition, I've discovered that the DEBUG_BREAK macro isn't set up for Mac. The following pages are how I got it to work for my own code:

https://developer.apple.com/library/content/qa/qa1361/_index.html https://stackoverflow.com/questions/37299/xcode-equivalent-of-asm-int-3-debugbreak-halt

revelator commented 6 years ago

both _DEBUG and NDEBUG are used as preprocessor commands in msvc to distinguish between release and debug builds, but no the compiler itself has no notion of these. DEBUGBREAK is however supported on the later versions of msvc, i think it was supported from msvc2010 and forward. The debug and release macros stem back from the quake days because quake used to be built with a DOS version of gcc (djgpp).

noodlecollie commented 6 years ago

I've also noticed that the debugger doesn't break on Linux when errors are thrown, because GDB_BREAK doesn't seem to be defined. Is this something that should be defined in the CMake files, or in code?

EDIT: Additionally, StackOverflow says the following with respect to NDEBUG:

CMake adds -DNDEBUG to the CMAKE_C_FLAGS_{RELEASE, MINSIZEREL} by default. So, you can use #ifndef NDEBUG.

noodlecollie commented 6 years ago

Closing this - it's fixed in the above pull request.