0

When I was trying to build the Google test c++ project I caught the errors

Error   C3861   't1': identifier not found
Error   C2065   't1': undeclared identifier
Error   C2039   'thread': is not a member of 'std'
Error   C2065   'thread': undeclared identifier
Error   C2146   syntax error: missing ';' before identifier 't1'

My test code is:

#include <future>
#include <thread>

#include "pch.h"
TEST(...)
{
    // preconditions here

    std::thread t1([&] {
        Sleep(100);
        testee.enqueue(item);
        });
    
    t1.join();
    
    // other logic
}

Why I cannot use the std::thread and other C++11 features in my project? How can I do it?

Foo Boy
  • 9
  • 4
  • 5
    Did you `#include `? Which version of visual studio? Which compiler flags? – mch Dec 29 '21 at 13:32
  • @mch yes, I did `#include `. I use Visual studio 2017. Where I may see the compiler flags in the VS 2017? – Foo Boy Dec 29 '21 at 15:13
  • Please show a [mre] – Alan Birtles Dec 29 '21 at 15:25
  • @AlanBirtles, hope I edited as you want – Foo Boy Dec 29 '21 at 15:44
  • Visual Studio 2017 supports C++14 (default) or C++17 standards. It doesn't support older standards, so `` is for sure known. As @273K answered, the problem is caused by the precompiled header. – prapin Dec 29 '21 at 17:14
  • If you `#include ` in several files in your project, then it is probably best to `#include ` inside `pch.h`. That probably decreases total build time. – prapin Dec 29 '21 at 19:02

3 Answers3

2

#include "pch.h" must be first. Other #include directive prior #include "pch.h" are ignored.

273K
  • 29,503
  • 10
  • 41
  • 64
  • Indeed. **Everything** before `#include "pch.h"` is simply ignored by MSVC compiler (with `/Yu"pch.h"` compiler option). That includes `#include`, `#define`, class declaration, and even syntax errors like random text! I got caught a few times with that, so I am now enforcing with a Git hook that every `.cpp` begins with `#include "pch.h"`. – prapin Dec 29 '21 at 17:31
  • Nice comment! I have never experienced such troubles because I have never added code before #include. You are welcome to modify my answer or add your own one. – 273K Dec 29 '21 at 19:59
0

Why I cannot use the std::thread and other C++11 features in my project?

Potential reason 1: You cannot use C++11 features unless you use C++11 or a later standard.

Potential reason 2: You cannot use std::thread unless you include the header that defines std::thread. It's defined in the header <thread>.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

Regarding pch.h, you could refer to this issue. I tested the code you uploaded in VS2019, and there was no error in the code. The result is shown in the picture.

#include <iostream>
#include <future>
#include <thread>
#include<Windows.h>
int main()
{
    std::thread t1([&] {
        Sleep(1000);
        });

    t1.join();
} 

enter image description here

Regarding the use of C++11, you could set it in the project properties>configuration properties>general>C++ Languae Standard. I suggest you use C++14, because C++14 already contains the features of C++11.

enter image description here

Yujian Yao - MSFT
  • 945
  • 1
  • 3
  • 9