コードライトを使用して、Ubuntu で Pthreads を使用して単純な C プログラムをコンパイルしようとしています。
コンパイラとして GCC を選択し、プロジェクト設定の C コンパイラ オプションに「-pthread」を追加しました。"undefined reference to pthread_create"
しかし、コンパイル時にエラーがスローされます。
コマンドラインから手動でGCCを呼び出すと、ビルドして実行します。
しかし、問題のある行をコメントアウトすると、コードライトでエラーなしでコンパイルされます。
したがって、コンパイル時に codelite が -pthread コンパイラ フラグを追加していないと思われます。
どんな助けでも大歓迎です。
これが私のコードからの抜粋です:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
//function prototypes
void * monitor_thread (void* data);
int main(int argc, char *argv[])
{
// exit program if no arguments are supplied
if(argc < 3)
{
printf("Not enough arguments passed \n");
return 0;
}
// parse arguments
int arrivalProbability = atoi(argv[1]);
int departureProbability = atoi(argv[2]);
// exit the program in entered values are not valid
if (arrivalProbability >=90 || departureProbability >=90)
{
printf("Argument(s) exceed a probability of 90%% \n");
return 0;
}
else if (arrivalProbability == 0 || departureProbability == 0)
{
printf("Argument(s)entered are zero or not intergers \n");
return 0;
}
// create the threads
int thr_id; /* thread ID for the newly created thread */
pthread_t p_thread; /* thread's structure */
int monitor = 1; /* monitor thread identifying number */
int arrival = 2; /* arrival thread identifying number */
int departure = 3; /* departure thread identifying number */
// monitor thead
// "undefined reference to pthread_create"
thr_id = pthread_create(&p_thread, NULL, monitor_thread, (void*)&monitor);
return 0;
}
void * monitor_thread (void* data)
{
int monitorThreadID = *((int*)data); /* thread identifying number */
printf("monitor thread");
sleep(1);
}