Pthreads 和未定义的引用
我正在接近 C 中的 pthreads 库,我写下了一些虚拟代码以便熟悉,但是当我尝试编译此代码时,我得到了对 pthread_create 的未定义引用,即使我已经包含了正确的库和数量传递给函数的参数是正确的。你能帮我一下吗?
我的代码是:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
void *dummy_thread(void *arg)
{
pthread_exit(NULL);
}
void *dummy_fork(void *arg)
{
exit(0);
}
void pthread_perror(char *string, int errcode)
{
char errmsg[256];
strerror_r(errcode, errmsg, 256);
printf("%s:%s\n", string, errmsg);
}
int main(int argc, char** argv)
{
pthread_t *threads;
int rc, nt, *pids;
long threads_microsecs, fork_microsecs, t;
struct timeval ora, dopo;
float perc;
if (argc != 2)
{
printf("Numero di argomenti %d non valido.\n", argc);
exit(1);
}
nt = strtol(argv[1], NULL, 10);
if (nt < 0)
{
printf("Numero di thread non valido: %d", nt);
exit(1);
}
threads = (pthread_t *) malloc (nt * sizeof(pthread_t));
pids = (int *) malloc (nt * sizeof(int));
if (pids == NULL)
{
perror("malloc");
exit(1);
}
gettimeofday(&ora, NULL);
for (t = 0; t < nt; t++)
{
if (rc = pthread_create(&threads[t], NULL, dummy_thread, NULL))
{
pthread_perror("pthread_create", rc);
exit(1);
}
gettimeofday(&dopo, NULL);
threads_microsecs = dopo.tv_usec - ora.tv_usec;
threads_microsecs += 1000000 * (dopo.tv_sec - ora.tv_sec);
printf("Tempo per la creazione dei thread %ld nsec.\n", threads_microsecs);
gettimeofday(&ora, NULL);
for (t = 0; t < nt; t++)
{
if ((pids[t] = fork()) < 0)
{
perror("fork");
exit(1);
}
if (pids[t] == 0)
{
dummy_fork(NULL);
}
}
gettimeofday(&dopo, NULL);
fork_microsecs = dopo.tv_usec - ora.tv_usec;
fork_microsecs += 1000000 * (dopo.tv_sec - ora.tv_sec);
printf("Tempo per la creazione dei processi %ld nsec", fork_microsecs);
perc = 100 * (((float) fork_microsecs - threads_microsecs)/(float)fork_microsecs);
printf("(%.2f%%)\n", perc);
}
return (EXIT_SUCCESS);
}
I'm approaching pthreads library in C, I have written down some dummy code in order to get acquainted, but as I try to compile this code, I get an undefined reference to pthread_create, even if I have included the proper library and the number of parameters passed to the function is correct. Can you please help me out?
My code is:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
void *dummy_thread(void *arg)
{
pthread_exit(NULL);
}
void *dummy_fork(void *arg)
{
exit(0);
}
void pthread_perror(char *string, int errcode)
{
char errmsg[256];
strerror_r(errcode, errmsg, 256);
printf("%s:%s\n", string, errmsg);
}
int main(int argc, char** argv)
{
pthread_t *threads;
int rc, nt, *pids;
long threads_microsecs, fork_microsecs, t;
struct timeval ora, dopo;
float perc;
if (argc != 2)
{
printf("Numero di argomenti %d non valido.\n", argc);
exit(1);
}
nt = strtol(argv[1], NULL, 10);
if (nt < 0)
{
printf("Numero di thread non valido: %d", nt);
exit(1);
}
threads = (pthread_t *) malloc (nt * sizeof(pthread_t));
pids = (int *) malloc (nt * sizeof(int));
if (pids == NULL)
{
perror("malloc");
exit(1);
}
gettimeofday(&ora, NULL);
for (t = 0; t < nt; t++)
{
if (rc = pthread_create(&threads[t], NULL, dummy_thread, NULL))
{
pthread_perror("pthread_create", rc);
exit(1);
}
gettimeofday(&dopo, NULL);
threads_microsecs = dopo.tv_usec - ora.tv_usec;
threads_microsecs += 1000000 * (dopo.tv_sec - ora.tv_sec);
printf("Tempo per la creazione dei thread %ld nsec.\n", threads_microsecs);
gettimeofday(&ora, NULL);
for (t = 0; t < nt; t++)
{
if ((pids[t] = fork()) < 0)
{
perror("fork");
exit(1);
}
if (pids[t] == 0)
{
dummy_fork(NULL);
}
}
gettimeofday(&dopo, NULL);
fork_microsecs = dopo.tv_usec - ora.tv_usec;
fork_microsecs += 1000000 * (dopo.tv_sec - ora.tv_sec);
printf("Tempo per la creazione dei processi %ld nsec", fork_microsecs);
perc = 100 * (((float) fork_microsecs - threads_microsecs)/(float)fork_microsecs);
printf("(%.2f%%)\n", perc);
}
return (EXIT_SUCCESS);
}
使用
-pthread
编译(错误链接)Compile (err link) with
-pthread