简体   繁体   English

clang++ 编译器挂着 -std=c++17 -O3

[英]clang++ compiler is hanging with -std=c++17 -O3

I am using this command line to compile my program.我正在使用这个命令行来编译我的程序。

clang++ -std=c++17 -O3 main.cpp -o main

I have started the compiler 20 minutes ago, and it is just hanging.我在 20 分钟前启动了编译器,它只是挂起。 I terminate the compiler, and try to compile it again and it is still hanging.我终止了编译器,并尝试再次编译它,它仍然挂起。 If I use the exact same command line, but without the -O3 the compiler completes instantly, but with the -O3 it is hanging.如果我使用完全相同的命令行,但没有-O3编译器会立即完成,但使用-O3它会挂起。

The code that it is compiling is relatively simple, without any errors.它正在编译的代码比较简单,没有任何错误。 What is going on?到底是怎么回事?

#include <ctime>    // for time()
#include <cstdlib>  // for srand(), rand(), size_t, EXIT_SUCCESS

#include <iostream>
#include <string>
#include <vector>

using std::cout;
using std::endl;
using std::string;
using std::vector;


int main()
{
    vector<string> messages;
    messages.push_back(string("“Blessed are those who are persecuted because of righteousness, for theirs is the kingdom of heaven.”"));
    messages.push_back(string("“Let the little children come to me, and do not hinder them, for the kingdom of heaven belongs to such as these.”"));

    /* Literally 10000 more quotes from the Bible. */


    srand(time(NULL));

    cout << messages[ rand() % messages.size() ] << endl;

    return EXIT_SUCCESS;
}

What is going on?到底是怎么回事?

If you want to keep all the strings in the program (instead of reading them from a file) I would replace the std::vector<std::string> with a const std::vector<std::string_view> or maybe even a const std::vector<const char*> and initialize it with all the strings:如果您想将所有字符串保留在程序中(而不是从文件中读取它们),我会将std::vector<std::string>替换为const std::vector<std::string_view>甚至一个const std::vector<const char*>并用所有字符串初始化它:

#include <ctime>    // for time()
#include <cstdlib>  // for srand(), rand(), size_t, EXIT_SUCCESS

#include <iostream>
#include <string_view>
#include <vector>

int main() {
    const std::vector<std::string_view> messages{
        "“Blessed are those who are persecuted because of righteousness, for theirs is the kingdom of heaven.”",
        "“Let the little children come to me, and do not hinder them, for the kingdom of heaven belongs to such as these.”",
        /* Literally 10000 more quotes from the Bible. */
    };

    srand(time(NULL));

    std::cout << messages[ rand() % messages.size() ] << '\n';
}

I wasn't patient enough to wait for the compiler to finish compiling your original code.我没有足够的耐心等待编译器完成编译您的原始代码。 The above compiled in ~1 second.以上编译在〜1秒内。

Note: There's a <random> header which gives you access to much better pseudo random number generation than rand() .注意:有一个<random> header 可以让您获得比rand()更好的伪随机数生成。 You should look into using that instead.您应该考虑使用它。 The end of your program would look like something like this using that:你的程序的结尾看起来像这样使用它:

    std::mt19937 prng(std::random_device{}());
    std::uniform_int_distribution<std::size_t> dist(0, messages.size() - 1);

    std::cout << messages[ dist(prng) ] << '\n';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 Mac OSX 上使用 clang++ 编译 C++17 - Unable to compile C++17 with clang++ on Mac OSX 不能在 Clang++ 的 C++17 中禁用 RVO/NRVO? - RVO/NRVO can not be disabled in C++17 in Clang++? clang-6和-std = c ++ 17 —无法调用bind(2) - clang-6 and -std=c++17 — can't invoke bind(2) C ++ 17 std :: variant头文件(clang 6.0.0) - C++17 std::variant header file (clang 6.0.0) clang++ 和 g++ 之间的不同行为(使用 c++17) - Different behavior between clang++ and g++ (using c++17) C ++ 17-使用自定义分配器进行节点提取/重新插入-适用于clang ++ / libc ++,但不适用于libstdc ++ - C++17 - node extraction/reinsertion with custom allocator - works with clang++/libc++ but not libstdc++ 当-std = c ++ 17在编译器输出中时,使用-std ++ 17标志请求为std :: variant启用c ++ 17支持的编译器 - Compiler requesting c++17 support to be enabled for std::variant by using the -std++17 flag when -std=c++17 is in compiler output GCC 和 Clang 不编译 std::hash<std::nullptr_t> 在 C++17 中 - GCC and Clang don't compile std::hash<std::nullptr_t> in C++17 使用C ++中的Clang进行模糊的部分特化17 - Ambiguous partial specializations with Clang in C++17 致命错误:在带有 -std=c++17 的 clang 6.0 中找不到“charconv”文件 - fatal error: 'charconv' file not found in clang 6.0 with -std=c++17
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM