Потоки в Codelite 6.1 на Ubuntu 14.04.1 x64

Я пытаюсь скомпилировать простую программу на C с Pthreads в Ubuntu с помощью codelite.

Я выбрал GCC в качестве своего компилятора и добавил "-pthread" в параметры компилятора C в настройках проекта. Но во время компиляции выдает ошибку "undefined reference to pthread_create".

Если я вызываю GCC через командную строку вручную, он собирается и запускается.

Но когда я комментирую оскорбительную строку, она компилируется без ошибок в codelite.

Поэтому я подозреваю, что 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);
}

person Lawrence Lee    schedule 25.08.2014    source источник
comment
неопределенная ссылка является ошибкой компоновщика. Это указывает на то, что параметр -pthread отсутствует в параметрах компоновщика.   -  person Pauli Nieminen    schedule 25.08.2014
comment
Вы пробовали -lpthread?   -  person Mabus    schedule 25.08.2014


Ответы (1)


Прямо на значке проекта в левом древовидном представлении выберите настройки -> общие настройки -> компоновщик -> параметры компоновщика.

И добавьте туда -pthreads Кстати, вставка журнала сборки может помочь другим людям помочь вам быстрее.

Эран

person Eran    schedule 25.08.2014
comment
Журнал сборки показал только неопределенную ссылку на pthread_create. Добавление «-pthread» в параметры компилятора и компоновщика помогло. - person Lawrence Lee; 26.08.2014
comment
это без s в конце! - person Anton Andreev; 12.03.2017