Home > Blockchain >  pthread library "unresolved symbol" when including (C 14, VS 22)
pthread library "unresolved symbol" when including (C 14, VS 22)

Time:01-11

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!

CodePudding user response:

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:

  • Related