1

I'm trying my hand at asynchronous function calls in C++ using std::async in accordance with this official cplusplus.com sample code.

Unfortunately, compilation fails. When running mingw32-make, I get the following errors:

  • 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>'

I also tried running make through WSL (Windows Subsystem for Linux), which essentially makes Linux bash available on Windows. Doesn't work on there either:

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'

I've tried the following:

  1. Updating my compiler using this SO answer.

  2. As noted above, attempting to make using two different approaches. Both fail.

  3. Using gcc instead of g++ (just a different compiler).

Here is 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;
}

And here is my makefile (I like to create one for each project just to practice):

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

I'm not sure why this code isn't compiling. I've run through it and cannot identify any errors. mingw updated successfully (I restarted my terminal afterwards).

Any help would be appreciated.

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
AlexH
  • 1,087
  • 2
  • 10
  • 29

1 Answers1

0
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'

This can be fixed by linking with pthread library, probably by adding -lpthread to your LINKER_FLAGS.

Paul
  • 13,042
  • 3
  • 41
  • 59
  • 1
    Hmm, that didn't work for some reason. On bash, it produced a massive wall of error text. Via `mingw32-make`, nothing changed (still fails). I'm signing off for tonight, but I'll be back tomorrow morning to review comments. Thanks anyway for your help! – AlexH Apr 06 '19 at 01:59
  • I'm not sure what is causing compiler errors (those that your `mingw32-make` produces). Looks like it uses the compiler which has incomplete support of `future` or something. I can't reproduce it with compilers I have locally. As for the linker error (missing symbols from `pthread` library, it could help if you post that "wall of error text", it may provide another hint. – Paul Apr 06 '19 at 02:01
  • [Here's](https://i.postimg.cc/wMxx8RJN/image.png) a screenshot. Strange stuff. (The wall of text happened because I temporarily tried using `gcc` again). – AlexH Apr 06 '19 at 11:58
  • Also tried [these steps](https://www.includehelp.com/c-programming-questions/error-undefined-reference-to-pthread-create-in-linux.aspx). No luck! :( – AlexH Apr 06 '19 at 12:39