Writing cross-platform C++ Code (Windows, Linux and Mac OSX)

前端 未结 5 1211
予麋鹿
予麋鹿 2021-01-29 19:48

This is my first-attempt at writing anything even slightly complicated in C++, I\'m attempting to build a shared library that I can interface with from Objective-C, and .NET app

相关标签:
5条回答
  • 2021-01-29 19:56

    I'll address this specific function:

    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
    }
    

    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.

    (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.)


    Unfortunately, you can't use #ifdef _WIN32 || _WIN64: see http://codepad.org/3PArXCxo for a sample error message. You can use the special preprocessing-only defined operator, as I did above.


    Regarding splitting up platforms according to functions or entire files (as suggested), 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.

    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.

    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).

    0 讨论(0)
  • 2021-01-29 20:06

    To add something more to this, other than the outstanding options above, the directives __linux__ and _WIN32 are known to the compiler, where the TARGET_OS_MAC directive was not, this can be resolved by using __APPLE__. Source: http://www.winehq.org/pipermail/wine-patches/2003-July/006906.html

    0 讨论(0)
  • 2021-01-29 20:08

    It seems none of TARGET_OS_MAC, __linux__, _WIN32 or _WIN64 is defined at the time you compile your code.

    So its like your code was:

    bool probe(){
    }
    

    That's why the compiler complains about reaching the end of a non-void function. There is no return clause.


    Also, for the more general question, here are my guidelines when developping multi-platform/architecure software/libraries:

    Avoid specific cases. Try to write code that is OS-agnostic.

    When dealing with system specific stuff, try to wrap things into "opaque" classes. As an example, if you are dealing with files (different APIs on Linux and Windows), try to create a File class that will embed all the logic and provide a common interface, whatever the operating system. If some feature is not available on one of the OS, deal with it: if the feature makes no sense for a specific OS, it's often fine to do nothing at all.

    In short: the less #ifdef the better. And no matter how portable your code is, test it on every platform before releasing it.

    Good luck ;)

    0 讨论(0)
  • 2021-01-29 20:09

    instead of repeating yourself and writing the same #ifdef .... lines again, again, and again, you're maybe better of declaring the probe() method in a header, and providing three different source files, one for each platform. This also has the benefit that if you add a platform you do not have to modify all of your existing sources, but just add new files. Use your build system to select the appropriate source file.

    Example structure:

    include/probe.h
    src/arch/win32/probe.cpp
    src/arch/linux/probe.cpp
    src/arch/mac/probe.cpp
    

    The warning is because probe() doesn't return a value. In other words, none of the three #ifdefs matches.

    0 讨论(0)
  • 2021-01-29 20:11

    The warning is because if none of the defines are actually defined then you have no return in your probe function. The fix for that is put in a default return.

    0 讨论(0)
提交回复
热议问题