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

时间:2013-03-29 17:30:39

标签: c++ visual-c++ g++ c-preprocessor

我正在尝试使用默认预处理程序定义来确定应该根据平台和编译器编译代码的哪些部分的“干净方式”。

我目前的测试设置涉及带有Visual C ++编译器的Windows机器和带有g ++编译器的Debian。

目前我有这样的事情:

#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();

它有效,但我认为可能并且必须有一些我可以检查的更好的定义。或者也许是一些更理想的CMake方式? 建议?

2 个答案:

答案 0 :(得分:4)

这些链接上有一个很好的CompilerOperating SystemArchitecture预处理器名称列表。您可以为您支持/检测的系统和编译器进行分支。此外,使用Boost标头已经在boost/config/(参见boost/config/select_compiler_config.hpp作为编译器标志的一个示例)中完成了大量此项工作。不是每个人都喜欢包括Boost,这就是为什么第一组链接是特定于库特定支持的通用链接。

答案 1 :(得分:0)

OP在问题编辑中回答:

  

从我收集到的内容中,以下是实现这一目标的理想方式:

// 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();