Function declarations using macros to support multiple platforms

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












I wrote a C++ plugin that retrieves screen pixel. It works on Windows and Android. I noticed that dllexport is only required on Windows but not on Android and causes error on other platform other than Windows. I fixed that by using #if macro to make it use dllexport only on Windows.



Here is the fixed header code:



#ifndef ANDROIDSCREENSHOT_NATIVE_LIB_H
#define ANDROIDSCREENSHOT_NATIVE_LIB_H

#define DLLExport __declspec(dllexport)

extern "C"
defined(WINAPI_FAMILY)
DLLExport void initScreenPointPixel(void* buffer, int x, int y, int width, int height);
DLLExport void updateScreenPointPixelBufferPointer(void* buffer);
DLLExport void updateScreenPointPixelCoordinate(int x, int y);
DLLExport void updateScreenPointPixelSize(int width, int height);
int GetScreenPixels(void* buffer, int x, int y, int width, int height);

#else
void initScreenPointPixel(void *buffer, int x, int y, int width, int height);
void updateScreenPointPixelBufferPointer(void *buffer);
void updateScreenPointPixelCoordinate(int x, int y);
void updateScreenPointPixelSize(int width, int height);
int GetScreenPixels(void *buffer, int x, int y, int width, int height);
#endif

#endif //ANDROIDSCREENSHOT_NATIVE_LIB_H


It works and fixes the problem but I don't like the fact that I have to declare each of these functions twice. Once in the #if and another in the #else statement. I have many functions if not thousands and if I go this route, the code would be unnecessary long. The simple goal is to remove dllexport on non Windows platform without declaring the function twice like I am currently doing.



Is there a better way to do this with macro without having to declare each function again? Any solution without macros is also good as long as I don't have to declare each function multiple times.







share|improve this question



























    up vote
    3
    down vote

    favorite












    I wrote a C++ plugin that retrieves screen pixel. It works on Windows and Android. I noticed that dllexport is only required on Windows but not on Android and causes error on other platform other than Windows. I fixed that by using #if macro to make it use dllexport only on Windows.



    Here is the fixed header code:



    #ifndef ANDROIDSCREENSHOT_NATIVE_LIB_H
    #define ANDROIDSCREENSHOT_NATIVE_LIB_H

    #define DLLExport __declspec(dllexport)

    extern "C"
    defined(WINAPI_FAMILY)
    DLLExport void initScreenPointPixel(void* buffer, int x, int y, int width, int height);
    DLLExport void updateScreenPointPixelBufferPointer(void* buffer);
    DLLExport void updateScreenPointPixelCoordinate(int x, int y);
    DLLExport void updateScreenPointPixelSize(int width, int height);
    int GetScreenPixels(void* buffer, int x, int y, int width, int height);

    #else
    void initScreenPointPixel(void *buffer, int x, int y, int width, int height);
    void updateScreenPointPixelBufferPointer(void *buffer);
    void updateScreenPointPixelCoordinate(int x, int y);
    void updateScreenPointPixelSize(int width, int height);
    int GetScreenPixels(void *buffer, int x, int y, int width, int height);
    #endif

    #endif //ANDROIDSCREENSHOT_NATIVE_LIB_H


    It works and fixes the problem but I don't like the fact that I have to declare each of these functions twice. Once in the #if and another in the #else statement. I have many functions if not thousands and if I go this route, the code would be unnecessary long. The simple goal is to remove dllexport on non Windows platform without declaring the function twice like I am currently doing.



    Is there a better way to do this with macro without having to declare each function again? Any solution without macros is also good as long as I don't have to declare each function multiple times.







    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I wrote a C++ plugin that retrieves screen pixel. It works on Windows and Android. I noticed that dllexport is only required on Windows but not on Android and causes error on other platform other than Windows. I fixed that by using #if macro to make it use dllexport only on Windows.



      Here is the fixed header code:



      #ifndef ANDROIDSCREENSHOT_NATIVE_LIB_H
      #define ANDROIDSCREENSHOT_NATIVE_LIB_H

      #define DLLExport __declspec(dllexport)

      extern "C"
      defined(WINAPI_FAMILY)
      DLLExport void initScreenPointPixel(void* buffer, int x, int y, int width, int height);
      DLLExport void updateScreenPointPixelBufferPointer(void* buffer);
      DLLExport void updateScreenPointPixelCoordinate(int x, int y);
      DLLExport void updateScreenPointPixelSize(int width, int height);
      int GetScreenPixels(void* buffer, int x, int y, int width, int height);

      #else
      void initScreenPointPixel(void *buffer, int x, int y, int width, int height);
      void updateScreenPointPixelBufferPointer(void *buffer);
      void updateScreenPointPixelCoordinate(int x, int y);
      void updateScreenPointPixelSize(int width, int height);
      int GetScreenPixels(void *buffer, int x, int y, int width, int height);
      #endif

      #endif //ANDROIDSCREENSHOT_NATIVE_LIB_H


      It works and fixes the problem but I don't like the fact that I have to declare each of these functions twice. Once in the #if and another in the #else statement. I have many functions if not thousands and if I go this route, the code would be unnecessary long. The simple goal is to remove dllexport on non Windows platform without declaring the function twice like I am currently doing.



      Is there a better way to do this with macro without having to declare each function again? Any solution without macros is also good as long as I don't have to declare each function multiple times.







      share|improve this question













      I wrote a C++ plugin that retrieves screen pixel. It works on Windows and Android. I noticed that dllexport is only required on Windows but not on Android and causes error on other platform other than Windows. I fixed that by using #if macro to make it use dllexport only on Windows.



      Here is the fixed header code:



      #ifndef ANDROIDSCREENSHOT_NATIVE_LIB_H
      #define ANDROIDSCREENSHOT_NATIVE_LIB_H

      #define DLLExport __declspec(dllexport)

      extern "C"
      defined(WINAPI_FAMILY)
      DLLExport void initScreenPointPixel(void* buffer, int x, int y, int width, int height);
      DLLExport void updateScreenPointPixelBufferPointer(void* buffer);
      DLLExport void updateScreenPointPixelCoordinate(int x, int y);
      DLLExport void updateScreenPointPixelSize(int width, int height);
      int GetScreenPixels(void* buffer, int x, int y, int width, int height);

      #else
      void initScreenPointPixel(void *buffer, int x, int y, int width, int height);
      void updateScreenPointPixelBufferPointer(void *buffer);
      void updateScreenPointPixelCoordinate(int x, int y);
      void updateScreenPointPixelSize(int width, int height);
      int GetScreenPixels(void *buffer, int x, int y, int width, int height);
      #endif

      #endif //ANDROIDSCREENSHOT_NATIVE_LIB_H


      It works and fixes the problem but I don't like the fact that I have to declare each of these functions twice. Once in the #if and another in the #else statement. I have many functions if not thousands and if I go this route, the code would be unnecessary long. The simple goal is to remove dllexport on non Windows platform without declaring the function twice like I am currently doing.



      Is there a better way to do this with macro without having to declare each function again? Any solution without macros is also good as long as I don't have to declare each function multiple times.









      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 13 at 16:42









      200_success

      123k14142399




      123k14142399









      asked Apr 13 at 5:52









      Programmer

      1657




      1657




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          Keep the DLLExport macro on all platforms, but make the definition resolve to nothing unless you're on Windows. You might also want to rename the macro to something more neutral. Something like this:



          #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(WINAPI_FAMILY)
          # define EXPORT __declspec(dllexport)
          #else
          # define EXPORT
          #endif

          extern "C"

          EXPORT void initScreenPointPixel(void* buffer, int x, int y, int width, int height);



          You should also be aware of that the extern "C" stuff is only recognized by a C++ compiler. The related statements should also be conditional, e.g.:



          #ifdef __cplusplus
          extern "C"
          #endif

          ... declarations here

          #ifdef __cplusplus

          #endif





          share|improve this answer























          • "You should also be aware of that the extern "C" stuff is only recognized by a C++ compiler. " I know. That's why I tagged C++ in this question and also mentioned it in the first sentence of my question. It's a C++ plugin that uses C++ features and syntax that can never be compiled on C.
            – Programmer
            Apr 13 at 7:59










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191934%2ffunction-declarations-using-macros-to-support-multiple-platforms%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          6
          down vote



          accepted










          Keep the DLLExport macro on all platforms, but make the definition resolve to nothing unless you're on Windows. You might also want to rename the macro to something more neutral. Something like this:



          #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(WINAPI_FAMILY)
          # define EXPORT __declspec(dllexport)
          #else
          # define EXPORT
          #endif

          extern "C"

          EXPORT void initScreenPointPixel(void* buffer, int x, int y, int width, int height);



          You should also be aware of that the extern "C" stuff is only recognized by a C++ compiler. The related statements should also be conditional, e.g.:



          #ifdef __cplusplus
          extern "C"
          #endif

          ... declarations here

          #ifdef __cplusplus

          #endif





          share|improve this answer























          • "You should also be aware of that the extern "C" stuff is only recognized by a C++ compiler. " I know. That's why I tagged C++ in this question and also mentioned it in the first sentence of my question. It's a C++ plugin that uses C++ features and syntax that can never be compiled on C.
            – Programmer
            Apr 13 at 7:59














          up vote
          6
          down vote



          accepted










          Keep the DLLExport macro on all platforms, but make the definition resolve to nothing unless you're on Windows. You might also want to rename the macro to something more neutral. Something like this:



          #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(WINAPI_FAMILY)
          # define EXPORT __declspec(dllexport)
          #else
          # define EXPORT
          #endif

          extern "C"

          EXPORT void initScreenPointPixel(void* buffer, int x, int y, int width, int height);



          You should also be aware of that the extern "C" stuff is only recognized by a C++ compiler. The related statements should also be conditional, e.g.:



          #ifdef __cplusplus
          extern "C"
          #endif

          ... declarations here

          #ifdef __cplusplus

          #endif





          share|improve this answer























          • "You should also be aware of that the extern "C" stuff is only recognized by a C++ compiler. " I know. That's why I tagged C++ in this question and also mentioned it in the first sentence of my question. It's a C++ plugin that uses C++ features and syntax that can never be compiled on C.
            – Programmer
            Apr 13 at 7:59












          up vote
          6
          down vote



          accepted







          up vote
          6
          down vote



          accepted






          Keep the DLLExport macro on all platforms, but make the definition resolve to nothing unless you're on Windows. You might also want to rename the macro to something more neutral. Something like this:



          #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(WINAPI_FAMILY)
          # define EXPORT __declspec(dllexport)
          #else
          # define EXPORT
          #endif

          extern "C"

          EXPORT void initScreenPointPixel(void* buffer, int x, int y, int width, int height);



          You should also be aware of that the extern "C" stuff is only recognized by a C++ compiler. The related statements should also be conditional, e.g.:



          #ifdef __cplusplus
          extern "C"
          #endif

          ... declarations here

          #ifdef __cplusplus

          #endif





          share|improve this answer















          Keep the DLLExport macro on all platforms, but make the definition resolve to nothing unless you're on Windows. You might also want to rename the macro to something more neutral. Something like this:



          #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(WINAPI_FAMILY)
          # define EXPORT __declspec(dllexport)
          #else
          # define EXPORT
          #endif

          extern "C"

          EXPORT void initScreenPointPixel(void* buffer, int x, int y, int width, int height);



          You should also be aware of that the extern "C" stuff is only recognized by a C++ compiler. The related statements should also be conditional, e.g.:



          #ifdef __cplusplus
          extern "C"
          #endif

          ... declarations here

          #ifdef __cplusplus

          #endif






          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Apr 13 at 6:32


























          answered Apr 13 at 6:18









          rjnilsson

          1,661188




          1,661188











          • "You should also be aware of that the extern "C" stuff is only recognized by a C++ compiler. " I know. That's why I tagged C++ in this question and also mentioned it in the first sentence of my question. It's a C++ plugin that uses C++ features and syntax that can never be compiled on C.
            – Programmer
            Apr 13 at 7:59
















          • "You should also be aware of that the extern "C" stuff is only recognized by a C++ compiler. " I know. That's why I tagged C++ in this question and also mentioned it in the first sentence of my question. It's a C++ plugin that uses C++ features and syntax that can never be compiled on C.
            – Programmer
            Apr 13 at 7:59















          "You should also be aware of that the extern "C" stuff is only recognized by a C++ compiler. " I know. That's why I tagged C++ in this question and also mentioned it in the first sentence of my question. It's a C++ plugin that uses C++ features and syntax that can never be compiled on C.
          – Programmer
          Apr 13 at 7:59




          "You should also be aware of that the extern "C" stuff is only recognized by a C++ compiler. " I know. That's why I tagged C++ in this question and also mentioned it in the first sentence of my question. It's a C++ plugin that uses C++ features and syntax that can never be compiled on C.
          – Programmer
          Apr 13 at 7:59












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191934%2ffunction-declarations-using-macros-to-support-multiple-platforms%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          C++11 CLH Lock Implementation