Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'll address this specific function:</p> <pre><code>bool probe() { #ifdef TARGET_OS_MAC return probe_macosx(); #elif defined __linux__ return probe_linux(); #elif defined _WIN32 || defined _WIN64 return probe_win(); #else #error "unknown platform" #endif } </code></pre> <p>Writing it this way, as a chain of if-elif-else, eliminates the error because it's impossible to compile without either a valid return statement or hitting the #error.</p> <p>(I believe WIN32 is defined for both 32- and 64-bit Windows, but I couldn't tell you definitively without looking it up. That would simplify the code.)</p> <hr> <p>Unfortunately, you can't use #ifdef _WIN32 || _WIN64: see <a href="http://codepad.org/3PArXCxo" rel="nofollow noreferrer">http://codepad.org/3PArXCxo</a> for a sample error message. You can use the special preprocessing-only <em>defined</em> operator, as I did above.</p> <hr> <p>Regarding splitting up platforms according to functions or entire files (as <a href="https://stackoverflow.com/questions/3627127/writing-cross-platform-c-code-windows-linux-and-mac-osx/3627208#3627208">suggested</a>), you may or may not want to do that. It's going to depend on details of your code, such as how much is shared between platforms and what you (or your team) find best to keep functionality in sync, among other issues.</p> <p>Furthermore, you should handle platform selection in your build system, but this doesn't mean you can't use the preprocessor: use macros conditionally defined (by the makefile or build system) for each platform. In fact, this is the often the most practical solution with templates and inline functions, which makes it more flexible than trying to eliminate the preprocessor. It combines well with the whole-file approach, so you still use that where appropriate.</p> <p>You might want to have a single config header which translates all the various compiler- and platform-specific macros into well-known and understood macros that you control. Or you could add -DBEAKS_PLAT_LINUX to your compiler command line—through your build system—to define that macro (remember to use a prefix for macro names).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload