c++ - g++、clang++、使用 libboost 的编译花絮——当 g++7 成功时,g++8 编译失败;

标签 c++ gcc boost clang travis-ci

我在 github repository 上有代码示例并在 travis-ci 上创建了一个构建便于复制。

最小的、完整的和可验证的例子

可能不是最小的,但我相信它足够小

它使用 boost.interprocess 创建共享内存区域库 ( boost::interprocess::managed_shared_memory ) 然后创建一个常规的 STL unordered_map从 boost 库中使用该区域的分配器。

该代码是我当前闭源库的精简版本,灵感来自问题 std::unordered_map with boost::interprocess allocator in shared memory - drawbacks? 中的答案。通过 sehe

#include <sys/mman.h>
#include <sys/syscall.h>
#include <functional>
#include <memory>
#include <unordered_map>
#include <string>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/shared_memory_object.hpp>

class Thing {
 public:
  volatile Thing *_parent;
  explicit Thing(Thing *parent) : _parent(parent) {}
};

namespace ipc = boost::interprocess;
using Segment = ipc::managed_shared_memory;
using Manager = Segment::segment_manager;
template <typename T> using Alloc = ipc::allocator<T, Manager>;
template <typename K, typename V, typename KH = std::hash<K>, typename KEq = std::equal_to<K>>
using HashMap = std::unordered_map<K, V, KH, KEq, Alloc<void>>;
typedef HashMap<pid_t, Thing> ThingMap;

int main() {
        boost::interprocess::shared_memory_object::remove("test");
        Segment my_segment{ipc::create_only, "test", 1ul<<40};
        Manager *my_manager = my_segment.get_segment_manager();
        ThingMap *my_map = my_segment.find_or_construct<ThingMap>("my_map")(my_manager);
        my_map->emplace(123, nullptr);
        printf("Hello world\n");
        return 0;
}

问题

1。 clang++需要 g++安装了吗?

使用 Ubuntu 14.04,如果我安装 clang++-6.0clang++-5.0不更新g++ (默认为 4.9 版),我最终遇到了编译错误。

这与没有libc++有关安装和 clang默认情况下不安装 c++ 库,而是使用系统中的内容——与 g++-4.9 捆绑在一起的那个?

2。我的代码需要 GNU 扩展吗?

显然,如果我指定 -std=c++17 , 它会失败 g++-8 .但是,它将通过 g++-7 成功和 g++-6 .

因为我正在安装 g++-8在 clang 构建中,它们也失败了。我的猜测是,如果我使用 g++-7 ,他们会成功的。 Build details with -std=c++17


非常感谢任何最佳实践建议。这是我第一次尝试使用 clang 的尝试之一。在travis-ci , 或 clang一般而言。

最佳答案

1。 clang++需要 g++安装了吗?

不——但会使用系统中可用的任何标准库。

在这种情况下,这是因为它是一个旧的库版本。安装 libstdc++-8-dev解决了这个问题。

2。我的代码需要 GNU 扩展吗?

是的。正如向我解释的那样 here通过@jonathan-wakely

the allocator's value type is not the same as the container's value type (which is exactly what the error tells you!)

GCC will accept it as an extension in -std=gnu++17 mode, but you compiled with -std=c++17 which disables the non-standard extension.

Shouldn't it be also flagged as an error in gcc-6 and gcc-7 with -std=c++17?

否,因为静态断言仅在 gcc 8 中添加.

标准方式是using HashMap = std::unordered_map<K, V, KH, KEq, Alloc<std::pair<const K, V>>>;

(感谢@jonathan-wakely 处理我的菜鸟问题!)

关于c++ - g++、clang++、使用 libboost 的编译花絮——当 g++7 成功时,g++8 编译失败;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51858747/

相关文章:

c++ - 推力:reduce_by_key 将 zip_iterator(tuple) 传递给自定义仿函数以按键检索平均值

iphone - Objective-C,引用属性

c++ - 服务器关闭时客户端上的 boost asio 写入操作被阻止

c++ - 使用具有 program_options::variables_map 作为参数的函数初始化 EXPECT_CALL 时出现编译器错误

C++,如何忽略 csv char * 中的逗号 (,)?

C++重载函数问题

c++ - 如何使用 C++ 打开目录

python - Theano : MissingGxx , g++ 不可用

c++ - 结构中的可变长度字符串和导致时间旅行的未定义行为

boost - CMake 的 find_package 包含仅 header 库