c++ - 表达式不是积分常量 clang libc++ threading

标签 c++ c++11 makefile clang constexpr

我试图在我的 linux 机器 (ubuntu) 上编译一个非常简单的线程程序,但即使我指定了 libc++,clang 似乎仍然向我抛出错误。我的程序是:

#include <iostream>
#include <thread>

void call_from_thread() {
    std::cout << "Hello, World!" << std::endl;
}

int main()
{
    std::thread t1(call_from_thread);

    t1.join();
    return 0;
}

生成文件:

CC=clang++
CFLAGS=-std=c++11 -stdlib=libc++ -pthread -c -Wall
#proper declaration of libc++, but still an error...
LDFALGS=
SOURCES=main.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=bimap

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
        $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
        $(CC) $(CFLAGS) $< -o $@

具体错误:

In file included from main.cpp:2:
In file included from /usr/include/c++/4.6/thread:37:
/usr/include/c++/4.6/chrono:666:7: error: static_assert expression is not an
      integral constant expression
      static_assert(system_clock::duration::min()
      ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [main.o] Error 1

我不确定为什么 clang 不使用 libc++,因为如果我没记错的话,clang 将使用这个库编译线程。感谢您的帮助!

最佳答案

在某些(早期)版本的 libc++ 中,某些函数未标记为 constexpr,这意味着它们不能在 static_assert 中使用。您应该检查 system_clock::duration::min() 是否确实以这种方式标记。 [您可能需要查看 numeric_limits,因为我似乎记得那是问题所在]

好消息是,如果这是问题所在,那么您可以自己将 constexpr 添加到数字限制头文件中;它不会引起任何其他问题。

关于c++ - 表达式不是积分常量 clang libc++ threading,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16597707/

相关文章:

c++ - 如何为采用一个参数的类初始化 shared_ptr vector ?

c++ - 包含 <map> 后的编译错误

c++ - 如何为许多自包含 cpp 文件的文件夹创建 */* ?

c++ - 俄罗斯农民乘法算法的时间复杂度?

c++ - 错误 : expected a declaration

c++ - 寻找毕达哥拉斯三重态(欧拉计划)

函数模板内部的 C++ 函数重载解析取决于该函数是否在命名空间中定义?

c++ - 根据对象类型切换

makefile - 通过相同的 Makefile 生成 Makefile 的包含文件

c - make命令替换代码中的常量