gpt4 book ai didi

c++ - 无法运行简单的 std::async 和 std::future 测试程序。错误: "has initializer but incomplete type."发生了什么事?

转载 作者:太空宇宙 更新时间:2023-11-04 12:37:57 37 4
gpt4 key购买 nike

我正在尝试使用 std::async 在 C++ 中调用异步函数按照这个官方cplusplus.com示例代码。

不幸的是,编译失败了。运行时 mingw32-make ,我收到以下错误:

  • main.cpp:37:23: error: variable 'std::future<bool> the_future' has initializer but incomplete type

  • main.cpp:37:61: error: invalid use of incomplete type 'class std::future<bool>'

我也试过运行 make通过 WSL(Linux 的 Windows 子系统),这实质上使 Linux bash 在 Windows 上可用。在那里也不起作用:

main.o: In function `std::thread::thread<std::__future_base::_Async_state_impl<std::thread::_Invoker<std::tuple<bool (*)(int), long> >, bool>::_Async_state_impl(std::thread::_Invoker<std::tuple<bool (*)(int), long> >&&)::{lambda()#1}>(std::__future_base::_Async_state_impl<std::thread::_Invoker<std::tuple<bool (*)(int), long> >, bool>::_Async_state_impl(std::thread::_Invoker<std::tuple<bool (*)(int), long> >&&)::{lambda()#1}&&)':
/usr/include/c++/7/thread:122: undefined reference to `pthread_create'

我试过以下方法:

  1. 使用 this SO answer 更新我的编译器.

  2. 如上所述,尝试使用两种不同的方法。都失败了。

  3. 使用 gcc而不是 g++ (只是一个不同的编译器)。

这里是 main.cpp :

#include <iostream>
#include <string>
#include <cstdlib>
#include <future>


// a non-optimized way of checking for prime numbers
bool is_prime (int x)
{
std::cout << "Calculating. Please, wait..." << std::endl;
for (int i=2; i<x; ++i)
{
if(x % i == 0) return false;
}
return true;
}


void interpret_result (bool result)
{
if (result)
{
std::cout << "It is prime!" << std::endl;
}
else
{
std::cout << "It is not prime!" << std::endl;
}
}


int main()
{
long num = 313222313;

// The result of the asynchronous call to is_prime will be stored in the_future
std::future<bool> the_future = std::async (is_prime, num);

std::cout << "Checking whether " << num << " is a prime number!" << std::endl;

// Nothing beyond this line runs until the function completes
bool result = the_future.get();

// Interpret the result
interpret_result (result);

// So the cmd window stays open
system ("pause");
return 0;
}

这是我的 makefile(我喜欢为每个项目创建一个只是为了练习):

COMPILER = g++

COMPILER_FLAGS = -std=c++17 -Wall -g

LINKER_FLAGS =

EXECUTABLE = async

all: main clean

main: main.o
$(COMPILER) $(LINKER_FLAGS) -o $(EXECUTABLE) main.o

main.o: main.cpp
$(COMPILER) $(COMPILER_FLAGS) -c main.cpp

.PHONY: clean

clean:
rm *.o

我不确定为什么这段代码无法编译。我已经完成它并且无法识别任何错误。 mingw更新成功(之后我重新启动了我的终端)。

如有任何帮助,我们将不胜感激。

最佳答案

main.o: In function `std::thread::thread<std::__future_base::_Async_state_impl<std::thread::_Invoker<std::tuple<bool (*)(int), long> >, bool>::_Async_state_impl(std::thread::_Invoker<std::tuple<bool (*)(int), long> >&&)::{lambda()#1}>(std::__future_base::_Async_state_impl<std::thread::_Invoker<std::tuple<bool (*)(int), long> >, bool>::_Async_state_impl(std::thread::_Invoker<std::tuple<bool (*)(int), long> >&&)::{lambda()#1}&&)':
/usr/include/c++/7/thread:122: undefined reference to `pthread_create'

这可以通过链接 pthread 库来解决,可能是通过将 -lpthread 添加到您的 LINKER_FLAGS

关于c++ - 无法运行简单的 std::async 和 std::future 测试程序。错误: "has initializer but incomplete type."发生了什么事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55545140/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com