Boost管理的映射文件:在OSX和Linux之间不兼容
Boost managed mapped files: not compatible between OSX and Linux?
下面的代码创建并读取一个名为MyMappedFile
的文件。如果我在Linux(Ubuntu 14.04)上运行写代码,我可以将这个文件复制到其他Linux机器上,它读起来很好。如果我把它转移到OSX并尝试在上面运行读取代码,我会得到一个错误:
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
生成的文件不同。
我用这个代码写作:
#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();
}
阅读:
#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(Linux上为4.8.4,OSX上为4.8.5)和Boost 1.55。
为什么这些文件在这些平台之间不兼容?
请注意,我说的是由编写代码生成的MyMappedFile
数据文件。编译代码生成的可执行文件不会在机器之间传输:Linux代码在Linux机器上编译,OSX代码在OSX机器上编译。
RE:为什么这些平台之间的文件不兼容
OSX和Linux使用不同的内核和ABI,因此它们以不同的方式编译代码。Linux通常使用ELF
文件格式,而OS X使用Mach-O
,这是特定于平台的。
编辑:不知道为什么您的输出不同(未共享)。
相关文章:
- 在C++/Linux中设置单调时钟的一些技巧
- Linux的Cpp上的计时器
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 在Linux中哪里可以找到互斥、未来等的源代码
- 如何在linux终端中同时编译和运行c++代码
- 在Linux(而不是OSX)中链接时发生库冲突
- Cmake Mac OSX库链接问题:在Linux上进行编译,但在Mac上进行了编译
- Google Play游戏服务可以用于PC游戏吗?(Windows/Linux/OSX)
- OSX vs Linux:如何处理未签名的长和uint64_t
- Supporting linux/types.h OSX
- Boost管理的映射文件:在OSX和Linux之间不兼容
- 毫秒级精度的 Linux/OSX 时钟分辨率
- 函数正则表达式C++在 linux(CentOS) 中遇到错误,但在 OSX 中
- 尝试将使用 X11 的基于 Linux 的开源C++项目移植到 OSX 中。我已经安装了XQuartz:现在怎么办?
- OSX相当于Linux g++-multilib是什么?
- 尝试在 OSX 中创建多个帧缓冲区对象而不是 Linux 时出现问题
- 未定义对的引用.error(Linux)-在OSX中编译良好
- Linux和Mac OSX上的编译时间
- 在osx/win7/linux上不同的c++调用转换
- 2D游戏编程Linux, OSX,和Windows - c++ /Python/OpenGL