-1

So I'm working on a project for a class and I cannot seem to get things to work. 1) Did I do this right? 2) How do I get rid of the errors?

#include <iostream>
#include <thread>

using namespace std;


void countdown(){
    int count;
    count = 21;

    while (count<=0)
    {
        count--;
        cout << "Count is " << count << '.' << endl;
    }
}

int main() {
    std::thread t1(countdown);
    t1.join();
    int count1;
    count1 = 0;


    while (count1<20)
    {
        count1++;

        cout << "Count is " << count1 << '.' << endl;
    }

    return 0;
}

Error messages:

g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -Wl,--no-as-needed -o "src\\Critical7.o" "..\\src\\Critical7.cpp" 
..\src\Critical7.cpp: In function 'int main()':
..\src\Critical7.cpp:27:2: error: 'thread' is not a member of 'std'
  std::thread t1(countdown);
  ^
..\src\Critical7.cpp:28:2: error: 't1' was not declared in this scope
  t1.join();

I've tried setting things the way other posts have said but I can't seem to get it to work.

Jen
  • 9
  • 1
  • 1
    The error message would appear to have nothing to do with the code you posted. And, BTW, Eclipse is a godawful C++ development environment. –  Mar 30 '18 at 17:46
  • You need to use the `-pthread` linker option. see: https://stackoverflow.com/a/8649908/ – RHertel Mar 30 '18 at 17:47
  • Possible duplicate of [What are the correct link options to use std::thread in GCC under linux?](https://stackoverflow.com/questions/8649828/what-are-the-correct-link-options-to-use-stdthread-in-gcc-under-linux) – RHertel Mar 30 '18 at 17:48
  • 1
    `count = 21; while (count<=0) {}` how many times do you expect that loop to run for? – Galik Mar 30 '18 at 18:01

1 Answers1

0

Two modifications are necessary to run the code correctly:

  1. replace while (count<=0) with while(count>=0), else the loop in countdown() exits immediately.
  2. Use the -pthread linker option in order to compile the code.
RHertel
  • 23,412
  • 5
  • 38
  • 64
  • Thank you! Forgive me as I'm learning. Where do I put the -pthread linker option? – Jen Mar 31 '18 at 01:55
  • At the end of the compiler options, so that you have `g++ -std=c++11 -o Critical7 "..\\src\\Critical7.cpp" -pthread`. In eclipse CDT it should be possible to set such options in `Project->Properties->C/C++ Build->Setiings->Tool Settings->GCC C++ Compiler->Command` – RHertel Mar 31 '18 at 06:49