0

So I am creating a basic C program that needs to use multithreading. I am simply trying to apply the code in a very simple example yet it keeps failing with the same error. Does anyone know why? (I realize this code's functionality is useless, just want to know why it's failing.)

/* DriverLib Includes */
#include <ti/devices/msp432p4xx/driverlib/driverlib.h>

/* Standard Includes */
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h>

int x = 0; // 

void *loop_2() {
    while(1){
        Serial.println("Running from thread ...");
        delay(5000);
    }

}

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600); // msp430g2231 must use 4800
  pthread_t thread;
  pthread_create(&thread, NULL, loop_2, NULL); 
}

// the loop routine runs over and over again forever:
void loop() {
    Serial.println(x);
    delay(5000);

  }
}
}

ERROR:

Error: 'pthread_create' was not declared in this scope.
ee94jrlc
  • 1
  • 1
  • 2
  • 1
    How you are compiling the code, show us the command. Did you link your executable with `-lpthread` ? Do read manual page of [pthread_create](http://man7.org/linux/man-pages/man3/pthread_create.3.html) – Achal Nov 28 '19 at 06:44
  • Please refer the link below... Answer to the problem is here. https://stackoverflow.com/questions/59053585/why-do-you-need-lpthread/59053690#59053690 You have to compile with `-lpthread` flag – Prajwal Shetye Nov 28 '19 at 06:48
  • 2
    Is that the complete file? (If so, there are unbalanced `}`.) Is that the first error message? (If not, fix the earlier problems.) Is that really C? (If so, where does `Serial` come from?) – rici Nov 28 '19 at 07:29
  • The `loop`, `setup` and `Serial.xxx()` tells me you are not a desktop computer. For which device do you program, with which tools? – Mathieu Nov 28 '19 at 07:30
  • I am using CCS Cloud. But I switched to CCS Desktop and added the "-lpthread" to the executable and still does not work. It produces the same error. – ee94jrlc Nov 28 '19 at 07:39
  • The error appears to be a *compilation* error, not a link error. Using `-lpthread` would be relevant to the latter, but it is not relevant to the former. – John Bollinger Nov 28 '19 at 15:33
  • `#include`ing `pthread.h` should provide an appropriate declaration of `pthread_create`, and CCS apparently does support pthreads. But it is possible that it supports them only for certain target platforms, and it is furthermore possible that it plays games with declarations depending on target platform, such that `pthread_create` is not declared when compiling for your particular target. – John Bollinger Nov 28 '19 at 15:42

1 Answers1

0

Does anyone know why?

You didn't say on which platform you are building this code. Some platforms are not multithreaded by default, and require special compile and link flags.

If using GCC, the flag to use is usually -pthread.

Other compilers may require -D_REENTRANT at compile time and -lpthread at link time.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • It does compile correctly, but it mentions that it is built for Windows 32. I am trying to flash this onto the MSP432P401R so I am getting an error trying to run it on the board now. – ee94jrlc Nov 28 '19 at 18:28
  • @ee94jrlc There is *no way* you are getting `Error: 'pthread_create' was not declared in this scope.` at runtime -- it's clearly a compile-time error. You aren't telling a consistent story. – Employed Russian Nov 28 '19 at 20:49