1

I've been attempting to include & use the pthreads library in my C++ project. I'm using Visual Studio 2022 Community Edition with C++ 14, and I'm building my project in a x64 configuration.

I installed the pthread library through the NuGet Package Manager, I have v2.9.1.4 of the pthreads package.

I was following this example for basic usage of the pthreads library, and I set up my thread-creating code as follows inside my main function:

pthread_t threads[ThreadNum];
ThreadData td[ThreadNum];
int rc;
int i;

for (i = 0; i < ThreadNum; i++) 
{
    cout << "Creating Thread " << i << "...\n";

    td[i].threadID = i;
    td[i].message = "Message";

    rc = pthread_create(&threads[i], NULL, TEST_FUNCTION, (void*)(&td[i]));

    if (rc) 
    {
        cout << "Error: Unable to create Thread " << rc << "!\n";

        exit(-1);
    }
}


pthread_exit(NULL);

There are no immediate errors thrown at me by VS, however upon a compilation attempt, the following errors are produced:

LNK2019 unresolved external symbol __imp_pthread_create referenced in function main
LNK2019 unresolved external symbol __imp_pthread_exit referenced in function main
LNK2001 unresolved external symbol __imp_pthread_exit

Above my main function definition, I made sure to include both pthread.h and cstdlib, so I'm quite a bit confused as to why my code here isn't compiling.

Where did I mess up here?

Thanks for reading my post, any guidance is appreciated!

Cosmin
  • 21,216
  • 5
  • 45
  • 60
Runsva
  • 365
  • 1
  • 7
  • 1
    You need to tell the linker to link against the pthread library – RoQuOTriX Jan 10 '23 at 10:03
  • 3
    Why use pthread in C++14, you have standard threads available. – Mat Jan 10 '23 at 10:16
  • 1
    Unless you're running on a platform with no C++11 or later support (and if you're using VS2022, you're not), there is no reason on earth to use pthreads, especially the shim libs contrived to "support" them on Windows. You should be using [standard library threading](https://en.cppreference.com/w/cpp/thread). – WhozCraig Jan 10 '23 at 10:22
  • Are you trying to link pthreads dynamically or statically? The `__imp_` prefix suggests that the linker is looking to import the functions from a DLL (i.e., they're marked with `__declspec(dllimport)` in the header). If you're trying to link pthreads statically, add `PTW32_STATIC_LIB` to the preprocessor definitions in the project settings; that should make the pthreads header remove that bit. – Wintermute Jan 10 '23 at 11:19
  • Thank you all for your comments. I tried using the standard thread library, and I managed to get it to work. – Runsva Jan 11 '23 at 02:02

1 Answers1

1

The example you are using is bad and incomplete. Try using std::thread instead of pthread, it will work and is easier to use.

Using pthread is complicated and not recommended. If you really want to keep using it, here are some starting points:

Cosmin
  • 21,216
  • 5
  • 45
  • 60
  • Thank you, I tried using the standard thread library and I managed to get it to work. It was simpler too. – Runsva Jan 11 '23 at 02:01