2016-03-23 26 views
0

以下代碼創建並讀取一個名爲MyMappedFile的文件。如果我在Linux(Ubuntu 14.04)上運行寫代碼,我可以將該文件複製到其他Linux機器上,並且它讀取正常。如果我不是轉移到OSX並嘗試運行它讀取的代碼,我得到一個錯誤:Boost託管映射文件:OSX和Linux不兼容?

libc++abi.dylib: terminating with uncaught exception of type boost::interprocess::lock_exception: boost::interprocess::lock_exception 

相反,寫在OSX文件都能夠在OSX上閱讀。但試圖在Linux下閱讀它們給我一個類似的消息:

terminate called after throwing an instance of 'boost::interprocess::lock_exception' 
    what(): boost::interprocess::lock_exception 

生成的文件是不同的。 (OSX上的Linux 4.8.4,4.8.5)

#include <boost/interprocess/managed_mapped_file.hpp> 
#include <boost/interprocess/managed_shared_memory.hpp> 
#include <boost/interprocess/containers/string.hpp> 

namespace bip=boost::interprocess; 

typedef bip::managed_shared_memory::segment_manager segment_manager_t; 
typedef bip::basic_string<char, std::char_traits<char>, bip::allocator<char, segment_manager_t> > shared_string; 

int main() { 
    bip::managed_mapped_file *infile = new bip::managed_mapped_file(bip::open_only, "MyMappedFile"); 
    shared_string *ptr = infile->find_or_construct<shared_string>("mystring")(infile->get_segment_manager()); 
    std::cout << "I got " << *ptr << std::endl; 
} 

兩款機器都使用GCC 4.8:

我這個代碼寫:

#include <boost/interprocess/managed_mapped_file.hpp> 
#include <boost/interprocess/managed_shared_memory.hpp> 
#include <boost/interprocess/containers/string.hpp> 

namespace bip=boost::interprocess; 

typedef bip::managed_shared_memory::segment_manager segment_manager_t; 
typedef bip::basic_string<char, std::char_traits<char>, bip::allocator<char, segment_manager_t> > shared_string; 

int main() { 
    bip::managed_mapped_file *outfile = new bip::managed_mapped_file(bip::create_only, "MyMappedFile", 1000); 
    outfile->find_or_construct<shared_string>("mystring")("bubza", outfile->get_segment_manager()); 
    outfile->flush(); 
} 

而與此讀書和Boost 1.55。

爲什麼這些平臺之間的文件不兼容?

請注意,我正在談論編寫代碼生成的MyMappedFile數據文件。通過編譯代碼生成的可執行文件不在機器之間傳輸:Linux代碼在Linux機器上編譯,而OSX代碼在OSX機器上編譯。

+0

'爲什麼這些平臺之間的文件不兼容?' 請分享一些例子,我們無法猜測。典型的原因包括永久性,或者如果您使用標準流,則會出現不同的區域設置。 – sbabbi

+0

您可以在http://so.con.com/MyMappedFile.linux上找到在Linux上編寫的文件,以及在http://so.con.com/MyMappedFile.osx上編寫的文件。 –

+0

Linux和OSX都很少-endian,並且它不會從文件的快速掃描中看出,是否存在排序問題。請注意,上面的代碼是所涉及的全部內容,所以如果使用標準流,則由Boost完成,而不是我的代碼。但我希望Boost使用'mmap()'而不是流。 –

回答

0

RE:爲什麼這些平臺之間的文件不兼容?

OS X和Linux使用不同的kernelsABI's,因此它們以不同的方式編譯代碼。 Linux通常使用ELF文件格式,而OS X使用Mach-O,這是平臺特定的。

編輯:不知道爲什麼你的輸出是不同的(它沒有共享)。

+0

這會如何特別引起Boost在不同平臺上編寫不同的輸出? –

+0

不確定我關注...因爲當您的代碼試圖在未編譯的平臺上執行它時,您的代碼會拋出一個'std:exception'。 –

+0

我什麼時候說輸出有什麼不同? –