2016-04-23 1 views
1

次のコードOSX vs Linux:unsigned longとuint64_tを扱う方法は?

#include <cstdio> 
#include <cstdint> 
#include <type_traits> 

int main() 
{ 
    static const char* b2s[] = { "no", "yes" }; 
    printf("%s\n", b2s[std::is_same<unsigned long, uint64_t>::value]); 
} 

戻りyes Linux上でコンパイルされ、noはOSX上でコンパイルした場合。

なぜこれがapparently normalだったのかを理解しながら読みました。 ライブラリを開発していてポータブルにしたい場合は、これをどのように処理できますか?私は各OSの好みに対応しなくてはなりませんか?

がfoo.h

#include <cstdint> 

template <class T> 
struct Foo 
{ 
    static const char id; 
}; 

foo.cpp私のライブラリの一部として、以前はコンパイルして、リンクされている

#include "foo.h" 
#define DEFINE_FOO(type_,id_)       \ 
    template <> const char Foo<type_>::id = id_;  \ 
    template <> const char Foo<const type_>::id = id_; 

DEFINE_FOO( bool,'a') 
DEFINE_FOO( int8_t,'b') 
DEFINE_FOO(uint8_t,'c') 
DEFINE_FOO(int16_t,'d') 
DEFINE_FOO(uint16_t,'e') 
DEFINE_FOO(int32_t,'f') 
DEFINE_FOO(uint32_t,'g') 
DEFINE_FOO(int64_t,'h') 
DEFINE_FOO(uint64_t,'i') 
DEFINE_FOO( float,'j') 
DEFINE_FOO( double,'k') 

// OSX requires this, but Linux considers it a re-definition 
// DEFINE_FOO(unsigned long,'l') 

:ここ

は一例です実行可能ファイル(例えば、 main.cpp)を作成する必要があるときです。通常、これは次のようになります。

$(CC) -c -Wall -std=c++0x -o foo.o foo.cpp 
$(CC) -Wall -std=c++0x -o main main.cpp foo.o 

これは、1つのプラットフォームでは動作しますが、他のプラットフォームでは動作しません。 DEFINE_FOO(unsigned long,'l')のコメントを外してfoo.cppにすると、OSXはうれしいですが、LinuxではFoo<uint64_t>が再定義されていると言います。およびその逆。

どのように正常になりますか?これに対処する "ポータブルな"方法はありますか?

答えて

1

それがポータブルにするために、私のような、this listを使用してターゲットOSに応じ#ifdef 条件文とマクロ呼び出しを囲みたい:私はunsigned longためlとしてuint64_tを設定

#ifdef __linux__ 
DEFINE_FOO(uint64_t,'l') 
#endif 
[...] 
#ifdef __APPLE__ 
DEFINE_FOO(unsigned long,'l') 
#endif 

注及びuint64_tはgccと64ビットアーキテクチャでほぼ同等です。

+0

ホーリー・モリー、このリストは非常に長いです:) – Sheljohn

+0

あなたが必要とするのは、古き良き時代のあなたのシステムをチェックすることです。** Find in Page ** :)。フラグとコンパイラとの互換性にも注意してください。 –

関連する問題