简体   繁体   English

默认预处理器定义和跨平台编译

[英]Default Preprocessor Definitions & Cross-Platform compiling

I'm trying to figure a "clean way" of using the default preprocessor definitions to determine what parts of my code should be compiled based on the platform and compiler. 我试图找到一种“干净的方式”,使用默认的预处理器定义来确定我的代码的哪些部分应该基于平台和编译器进行编译。

My current test setup involves a Windows machine with Visual C++ compiler and a Debian with g++ compiler. 我目前的测试设置涉及带有Visual C ++编译器的Windows机器和带有g ++编译器的Debian。

Currently I have something like this: 目前我有这样的事情:

#if defined (__GNUG__)
    #define ASMMath_EI __attribute__ ((__visibility__("default")))
#elif defined (WIN32)
    #ifdef ASMMath_EXPORTS
        #define ASMMath_EI __declspec(dllexport)
    #else
        #define ASMMath_EI __declspec(dllimport)
    #endif
#endif

extern void ASMMath_EI AsmProblemOne();

And it works, but I figure there might and must be some better definitions I can check for. 它有效,但我认为可能并且必须有一些我可以检查的更好的定义。 Or perhaps some more ideal way with CMake? 或者也许是一些更理想的CMake方式? Suggestions? 建议?

There is a nice listing of Compiler , Operating System , and Architecture preprocessor names at those links. 这些链接上有一个很好的编译器操作系统架构预处理器名称列表。 You could branch for the systems and compilers you care about supporting/detecting. 您可以为您支持/检测的系统和编译器进行分支。 Additionally a lot of this work is already done in boost/config/ (see boost/config/select_compiler_config.hpp as one example for compiler flags) using Boost headers. 此外,使用Boost标头已经在boost/config/ (参见boost/config/select_compiler_config.hpp作为编译器标志的一个示例)中完成了大量此项工作。 Not everyone likes including Boost, which is why the first set of links is generic from library specific support. 不是每个人都喜欢包括Boost,这就是为什么第一组链接是来自库特定支持的通用链接。

Answered by the OP in a question edit: OP在问题编辑中回答:

From what I gather the following would be ideal way to implement this: 从我收集到的内容中,以下是实现此目的的理想方式:

 // MasterHeader.h #if defined _MSC_VER // Defined by visual studio #define PROJ_TMP_DLL_IMPORT __declspec(dllimport) #define PROJ_TMP_DLL_EXPORT __declspec(dllexport) #else #if __GNUC__ >= 4 // Defined by GNU C Compiler. Also for C++ #define PROJ_TMP_DLL_IMPORT __attribute__ ((visibility ("default"))) #define PROJ_TMP_DLL_EXPORT __attribute__ ((visibility ("default"))) #else #define PROJ_TMP_DLL_IMPORT #define PROJ_TMP_DLL_EXPORT #endif #endif #ifdef PROJ_EXPORTS #define PROJ_API PROJ_TMP_DLL_EXPORT #else #define PROJ_API PROJ_TMP_DLL_IMPORT #endif 

- -

 // File.h #include "MasterHeader.h" extern void PROJ_API SomeFunction(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM