如何通過條件編譯,利用不同編譯器的預置宏定義來區分不同編譯器,實現跨平台編譯?
想要VS Dev Codeblocks Linux的預置宏定義
就是#if.....#elif.....#endlif...
謝謝!!
關於各種編譯器的預定義宏,boost的文檔中有一份很好的總結:http://www.boost.org/doc/libs/1_65_1/doc/html/predef/reference.html
比如說這些是比較常見的:
_WIN32 // Windows
__linux // Linux
__clang__ // Clang
__GNUC__ // GCC(需要注意的是Clang也會定義這個宏)
_MSC_VER // MSVC
定義:
paltform_config.h:
#pragma once
#if !(defined(DEBUG) || defined(_DEBUG)) !defined(NDEBUG)
#define NDEBUG
#endif
#if !(defined(DEBUG) || defined(_DEBUG)) !defined(_NDEBUG)
#define _NDEBUG
#endif
#if(__cplusplus &< 201103L !defined(_MSC_VER)) || (defined(_MSC_VER) (_MSC_VER &< 1700)) ||
(defined(__GLIBCXX__) __GLIBCXX__ &< 20130322L)
#define $cpp11 $no
#define $cpp03 $yes
#else
#define $cpp11 $yes
#define $cpp03 $no
#endif
// Thread Local Storage
#if defined(__MINGW32__) || defined(__SUNPRO_C) || defined(__xlc__) || defined(__GNUC__) ||
defined(__clang__) || defined(__GNUC__) // __INTEL_COMPILER on linux
// MingW, Solaris Studio C/C++, IBM XL C/C++,[3] GNU C,[4] Clang[5] and Intel
// C++ Compiler (Linux systems)
#define $tls(x) __thread x
#else
// Visual C++,[7] Intel C/C++ (Windows systems),[8] C++Builder, and Digital
// Mars C++
#define $tls(x) __declspec(thread) x
#endif
// OS utils. Here is where the fun starts... good luck
#define $quote(...) #__VA_ARGS__
#define $comment(...) $no
#define $uncomment(...) $yes
#define $yes(...) __VA_ARGS__
#define $no(...)
#define $on(v) (0 v(+1)) // usage: #if $on($msvc)
#define $is $on // usage: #if $is($debug)
#define $has(...) $clang(__has_feature(__VA_ARGS__)) $celse(__VA_ARGS__) // usage: #if $has(cxx_exceptions)
#if defined(_WIN32)
#define $windows $yes
#define $welse $no
#else
#define $windows $no
#define $welse $yes
#endif
#ifdef __APPLE__
#define $apple $yes
#define $aelse $no
#else
#define $apple $no
#define $aelse $yes
#endif
#ifdef __linux__
#define $linux $yes
#define $lelse $no
#else
#define $linux $no
#define $lelse $yes
#endif
#ifdef __ANDROID__
#define $android $yes
#define $aelse $no
#else
#define $android $no
#define $aelse $yes
#endif
// Compiler utils
#if ULONG_MAX == 4294967295
#define $bits64 $yes
#define $bits32 $no
#else
#define $bits64 $no
#define $bits32 $yes
#endif
#if defined(NDEBUG) || defined(_NDEBUG) || defined(RELEASE)
#define $release $yes
#define $debug $no
#else
#define $release $no
#define $debug $yes
#endif
#if defined(NDEVEL) || defined(_NDEVEL) || defined(PUBLIC)
#define $public $yes
#define $devel $no
#else
#define $public $no
#define $devel $yes
#endif
#if defined(__GNUC__) || defined(__MINGW32__)
#define $gnuc $yes
#define $gelse $no
#else
#define $gnuc $no
#define $gelse $yes
#endif
#if defined(__MINGW32__)
#define $mingw $yes
#else
#define $mingw $no
#endif
#ifdef _MSC_VER
#define $msvc $yes
#define $melse $no
#else
#define $msvc $no
#define $melse $yes
#endif
#ifdef __clang__
#define $clang $yes
#define $celse $no
#else
#define $clang $no
#define $celse $yes
#endif
#if $on($msvc) || $on($gnuc) || $on($clang)
#define $undefined_compiler $no
#else
#define $undefined_compiler $yes
#endif
#if $on($windows) || $on($linux) || $on($apple) || $on($android)
#define $undefined_os $no
#else
#define $undefined_os $yes
#endif
template &
inline bool eval()
{
return true;
}
template &<&>
inline bool eval&
{
return false;
}
#define runtime_eval(_x) eval&()
使用:
#include "paltform_config.h"
#if $on($windows)
.....
#elif $on($apple)
....
#elif $on($linux)
....
#else
....
#endif
推薦閱讀:
※有沒有免費的雲主機啊,只要能跑Python 就好?
※為什麼一家靠做流氓軟體起家的360,現在可以成為一家安全公司?
※為什麼在微軟三月份就發布公告和更新的情況下兩個月後仍有很多人中了「永恆之藍」?
※為什麼寫入U盤時是按兆,刪除時是按項?
※擁有手機靚號是怎樣一種體驗?