Search code examples
c++cpthreadsvisual-studio-2019raspbian

C/C++ Threading in Linux (Raspbian) using VS2019 on Windows 10 -pthread - Can't compile


I'm trying to do my first bit of threading but no matter what I've tried I can't get this to compile.

I've gone back to trying to compile some demo code and I'm getting the same problem as in my program.

If I run a simple print hello world it compiles and deploys the program fine and I can simply navigate to and run it directly on the Pi4.

Threading demo code

#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>


pthread_t tid[2];

void* doSomeThing(void* arg)
{
    unsigned long i = 0;
    pthread_t id = pthread_self();

    if (pthread_equal(id, tid[0]))
    {
        printf("\n First thread processing\n");
    }
    else
    {
        printf("\n Second thread processing\n");
    }

    for (i = 0; i < (0xFFFFFFFF); i++);

    return NULL;
}

int main(void)
{
    int i = 0;
    int err;

    while (i < 2)
    {
        err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
        if (err != 0)
            printf("\ncan't create thread :[%s]", strerror(err));
        else
            printf("\n Thread created successfully\n");

        i++;
    }

    sleep(5);
    return 0;
}

When I compile I get

Error       /home/pi/projects/cpp_raspbian_thread_101/obj/x64/Debug/main.o: in function `main':
Error       undefined reference to `pthread_create'
Error       ld returned 1 exit status

To resolve this I've tried to add -pthread or -lpthread to Project > Properties > Configuration Properties > C/C++ > Command Line > Addiitional Options

That does nothing, I'm not really sure if this is the correct place to put this. I'm building in VS2019 so I'm not building from the command line, I don't know where to add this argument.

I have also tried installing pthreads in NuGet but that doesn't help.

Other software like VSCode seem to have files that could add this to but I'm lost in VS2019

Any help is appreciated.

EDIT:

Thanks for responses

OK so as @Eljay suggested I'm trying to use std::thread (again) but have the same problem.

// thread example
#include <iostream>       
#include <thread>         

void foo()
{
    // do stuff...
}

int main()
{
    std::thread first(foo);     
    return 0;
}

Log file

  Validating sources
  Copying sources remotely to '10.0.0.2'
  Validating architecture
  Validating architecture
  Starting remote build
  Compiling sources:
  main.cpp
  Linking objects
/usr/bin/ld : error : /home/pi/projects/cpp_raspbian_thread_101/obj/ARM/Debug/main.o: in function `std::thread::thread<void (&)(), , void>(void (&)())':
/usr/include/c++/8/thread(135): error : undefined reference to `pthread_create'
collect2 : error : ld returned 1 exit status

So I'm back to the pthread_create problem again


Solution

  • OK both code examples now compile and run.

    As I originally thought, I needed to add -pthread somewhere in VS2019 and I was putting it in the wrong section.

    Go to Project Properties > Configuration Properties > Linker > Command Line

    Add -pthread to Additional Options box and Apply.

    I hope that saves someone else the 3 days it took me to sort it!