# Conditionalising C/C++ code: "#ifdef FOO" vs. "#if FOO"

DevFeed: [Conditionalising C/C++ code: "#ifdef FOO" vs. "#if FOO"](<https://devfeed.tech/articles/conditionalising-c-c-code-ifdef-foo-vs-if-foo-21568.md>)

Original publisher: [Read original article](<http://lackingrhoticity.blogspot.com/2015/01/conditionalising-c-ifdef-vs-if.html>)

Author: Mark Seaborn (noreply@blogger.com)

Published: 2015-01-21T20:14:00Z

Content type: article

Language: en

Sources: [Mark Seaborn](<https://devfeed.tech/sources/mark-seaborn.md>)

Topics: [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [Code](<https://devfeed.tech/topics/code.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>), [Chromium](<https://devfeed.tech/topics/chromium.md>), [MSVC](<https://devfeed.tech/topics/msvc.md>), [gcc](<https://devfeed.tech/topics/gcc.md>), [Windows](<https://devfeed.tech/topics/windows.md>)

Tags: [architecture](<https://devfeed.tech/tags/architecture.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [chromium](<https://devfeed.tech/tags/chromium.md>), [code](<https://devfeed.tech/tags/code.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [gcc](<https://devfeed.tech/tags/gcc.md>), [mistakes](<https://devfeed.tech/tags/mistakes.md>), [msvc](<https://devfeed.tech/tags/msvc.md>), [windows](<https://devfeed.tech/tags/windows.md>)

## AI overview

The article compares #ifdef with #if for conditional compilation based on operating system and CPU architecture. It explains that defined macros can enable warnings for misspelled names when GCC/Clang warnings are enabled, while MSVC's corresponding warning is difficult to use because of system-header warnings. It also notes that runtime if statements can compile-test code across platforms, provided the code does not depend on platform-specific functions.

## Source excerpt

Is it better to use #ifdef PLATFORM or #if PLATFORM when writing code that needs to be conditionalised according to OS, CPU architecture, etc.? Chromium's codebase uses the former. For example, it uses #ifdef OS_WIN or #if defined(OS_WIN), where OS_WIN is #defined on Windows (by build/build_config.h) and undefined elsewhere. In contrast, NaCl's codebase uses the latter. It uses #if NACL_WINDOWS or if (NACL_WINDOWS), where NACL_WINDOWS is always #defined: as 1 on Windows and 0 elsewhere. This latter approach has the benefit of catching some mistakes: If you mistype the macro name in #if NACL_WINDOWS, you can get a compiler warning. However, this only works if you enable GCC/Clang's -Wundef warning. Microsoft's compiler (MSVC) has a similar warning, /wd4668, but it's effectively unusable because it produces warnings about system header files. You can sometimes write if (PLATFORM) instead of #if PLATFORM. The if() version has the advantage that the code in the if() block will be compile-tested on all platforms, not just those where PLATFORM == 1. This can help catch mistakes earlier. This doesn't work if the code block uses functions that are only defined on PLATFORM, though. See also: The Great -Wundef purge