Function declarations using macros to support multiple platforms
Clash 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.
c++ library macros portability
add a comment |Â
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.
c++ library macros portability
add a comment |Â
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.
c++ library macros portability
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.
c++ library macros portability
edited Apr 13 at 16:42
200_success
123k14142399
123k14142399
asked Apr 13 at 5:52
Programmer
1657
1657
add a comment |Â
add a comment |Â
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
"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
add a comment |Â
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
"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
add a comment |Â
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
"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
add a comment |Â
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
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
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
add a comment |Â
"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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password