2013-03-29 57 views
1

我想通过使用默认的预处理器定义来确定我的代码的哪些部分应该基于平台和编译器进行编译的“干净方式”。默认预处理器定义和跨平台编译

我目前的测试设置包括一台Windows计算机用Visual C++编译器和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更理想一些? 建议?

+0

注在Windows上使用GCC进行编译时,\ _ \ _ GNUG \ _ \ _也设置为true。更好地使用\ _ \ _ linux \ _ \ _。 – Ionic

+0

@Ionic图。我不确定我是否需要* nix或g ++的定义。 – TheDespite

+1

对不起,似乎* NIX特异性按照:http://gcc.gnu.org/wiki/Visibility – Ionic

回答

4

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

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(); 
+0

([在问题中回答编辑并转换为社区wiki](http://meta.stackoverflow.com/questions/267434/what-is-the-woole-action-when-the-answer-to-a-问题 - 被添加到所述阙)) –