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

前端 未结 5 1396
予麋鹿
予麋鹿 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 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 ;)

提交回复
热议问题