[英]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.