I am fairly new to C and do not understand why I am getting these errors when I try to compile my program using g++ ./main.c. I have tried looking at other resources and I am unable to find the answers I need. If there is a solution that you already know of, please post it here as well.
/tmp/ccSGRAcp.o: In function `producer(void*)':
main.c:(.text+0x12): undefined reference to `sem_wait'
main.c:(.text+0x1c): undefined reference to `sem_wait'
main.c:(.text+0xa0): undefined reference to `sem_post'
main.c:(.text+0xaa): undefined reference to `sem_post'
/tmp/ccSGRAcp.o: In function `consumer(void*)':
main.c:(.text+0xc5): undefined reference to `sem_wait'
main.c:(.text+0xcf): undefined reference to `sem_wait'
main.c:(.text+0x153): undefined reference to `sem_post'
main.c:(.text+0x15d): undefined reference to `sem_post'
/tmp/ccSGRAcp.o: In function `main':
main.c:(.text+0x17e): undefined reference to `sem_init'
main.c:(.text+0x192): undefined reference to `sem_init'
main.c:(.text+0x1a6): undefined reference to `sem_init'
main.c:(.text+0x1c1): undefined reference to `pthread_create'
main.c:(.text+0x1dc): undefined reference to `pthread_create'
main.c:(.text+0x1f7): undefined reference to `pthread_create'
main.c:(.text+0x212): undefined reference to `pthread_create'
main.c:(.text+0x22d): undefined reference to `pthread_create'
/tmp/ccSGRAcp.o:main.c:(.text+0x248): more undefined references to `pthread_create' follow
collect2: error: ld returned 1 exit status
The code I am trying to compile is
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <pthread.h>
#define N 10000000
typedef struct
{
char const* buf[N];
char in;
char out;
sem_t mutex;
sem_t full;
sem_t empty;
} bufferItems;
bufferItems sharedBuffer;
void *producer(void *arg) {
while(1) {
sem_wait(&sharedBuffer.empty);
sem_wait(&sharedBuffer.mutex);
sharedBuffer.buf[sharedBuffer.in] = "X";
sharedBuffer.in = (sharedBuffer.in+1)%N;
printf("Producer ");
printf("%c", sharedBuffer.in);
printf("\n");
sem_post(&sharedBuffer.mutex);
sem_post(&sharedBuffer.full);
}
}
void *consumer(void *arg){
while(1){
sem_wait(&sharedBuffer.full);
sem_wait(&sharedBuffer.mutex);
sharedBuffer.buf[sharedBuffer.out] = NULL;
sharedBuffer.out = (sharedBuffer.out+1)%N;
printf("Consumer ");
printf("%c", sharedBuffer.out);
printf("\n");
sem_post(&sharedBuffer.mutex);
sem_post(&sharedBuffer.empty);
}
}
int main(void) {
sem_init(&sharedBuffer.mutex, 0, 0);
sem_init(&sharedBuffer.full, 0, 0);
sem_init(&sharedBuffer.empty, 0, N);
pthread_t p1;
pthread_t p2;
pthread_t p3;
pthread_t p4;
pthread_t c1;
pthread_t c2;
pthread_t c3;
pthread_t c4;
// create four producer threads
pthread_create(&p1,NULL,producer,NULL);
pthread_create(&p2,NULL,producer,NULL);
pthread_create(&p3,NULL,producer,NULL);
pthread_create(&p4,NULL,producer,NULL);
// create four consumer threads
pthread_create(&c1,NULL,consumer,NULL);
pthread_create(&c2,NULL,consumer,NULL);
pthread_create(&c3,NULL,consumer,NULL);
pthread_create(&c4,NULL,consumer,NULL);
}
Add the -pthread param to pull in all the threading stuff for linking
g++ ./main.c -pthread
Related
This question already has answers here:
Undefined reference to pthread_create in Linux
(16 answers)
Closed 1 year ago.
So I am writing a program in C that creates 4 threads that produce/consume from buffers. I initialized all the threads in my main function but I am getting the following error. Does anyone know why? I ran it on my local zsh shell on macOS and it works fine. But when I try running it on my school's server, I think it's linux with bash, it gives me the errors.
flip1 ~/CS344/assignment4 1022$ gcc -std=gnu99 -o line-processor line_processor2.c
/tmp/ccYF7Kqe.o: In function `main':
line_processor2.c:(.text+0x7b5): undefined reference to `pthread_create'
line_processor2.c:(.text+0x7d9): undefined reference to `pthread_create'
line_processor2.c:(.text+0x7fd): undefined reference to `pthread_create'
line_processor2.c:(.text+0x821): undefined reference to `pthread_create'
line_processor2.c:(.text+0x832): undefined reference to `pthread_join'
line_processor2.c:(.text+0x843): undefined reference to `pthread_join'
line_processor2.c:(.text+0x854): undefined reference to `pthread_join'
line_processor2.c:(.text+0x865): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
Below is my main function
int main()
{
pthread_t inputThread, lineSeparatorThread, plusSignThread, outputThread;
pthread_attr_t attr;
// Set up Sentinal Values at the begining of each buffer to indicate whether or not
// the buffer line has been read or not
for (int i = 0; i < BUFSIZE; i++)
{
buffer1[i][0] = -1;
buffer2[i][0] = -1;
buffer3[i][0] = -1;
}
// Initialize a pthread attribute structure to set up joinable threads
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
// Initialize mutex and condition variables
pthread_mutex_init(&mutex1, NULL);
pthread_mutex_init(&mutex2, NULL);
pthread_mutex_init(&mutex3, NULL);
pthread_cond_init(&readyBuffer1, NULL);
pthread_cond_init(&readyBuffer2, NULL);
pthread_cond_init(&readyBuffer3, NULL);
// Set up the thread in reverse order so that the readers/consumers will pend
// waiting for the writers/consumers to start up
pthread_create(&outputThread, &attr, output_thread, NULL);
usleep(100); // Force the program to allow output thread to actually come up and pend on readyBuffer 3 first
pthread_create(&plusSignThread, &attr, plus_sign_thread, NULL);
usleep(100); // Force the program to allow plus_sign_thread thread to actually come up first
pthread_create(&lineSeparatorThread, &attr, line_separator_thread, NULL);
usleep(100); // Force the program to allow line_separator_thread thread to actually come up first
pthread_create(&inputThread, &attr, input_thread, NULL);
pthread_join(inputThread, NULL);
pthread_join(lineSeparatorThread, NULL);
pthread_join(plusSignThread, NULL);
pthread_join(outputThread, NULL);
// Freeing up memory.
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&mutex1);
pthread_mutex_destroy(&mutex2);
pthread_mutex_destroy(&mutex3);
pthread_cond_destroy(&readyBuffer1);
pthread_cond_destroy(&readyBuffer2);
pthread_cond_destroy(&readyBuffer3);
return 0;
}
And lastly, my #include statements and buffer variables.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <ctype.h>
#define BUFSIZE 50
// Buffers can hold up to 50 lines that can be 1000 characters.
char buffer1[BUFSIZE][1000]; //inputThread + lineSeparatorThread
char buffer2[BUFSIZE][1000]; //lineSeparatorThread + plus_sign_thread
char buffer3[BUFSIZE][1000]; //plus_sign_thread + output_thread
Its look like your'e missing compilation flag -pthread.
try to put the flag as last.
gcc test.c -o test -std .... -pthread
From man7:
int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void *),
void *restrict arg);
Compile and link with -pthread.`
https://man7.org/linux/man-pages/man3/pthread_create.3.html
I am learning CUDD package for research purposes. I have got one sample code from which I have tried to learn the basic functionalities. But I am getting error during compilation.
I have already set the paths for the header.
#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include "cudd.h"
#include "util.h"
void print_dd(DdManager *gbm, DdNode *dd, int n, int pr)
{
printf("Ddmanager nodes : %ld \n",Cudd_ReadNodeCount(gbm));
printf("Ddmanager vars : %d \n",Cudd_ReadSize(gbm));
printf("Ddmanager reorderings :%d\n",Cudd_ReadReorderings(gbm));
printf("DdManager memory % ld",Cudd_ReadMemoryInUse(gbm));
Cudd_PrintDebug(gbm,dd,n,pr);
}
void write_dd(DdManager *gbm, DdNode *dd, char * filename)
{
FILE *outfile;
outfile=fopen(filename,"w");
DdNode **ddnodearray=(DdNode **)malloc(sizeof(DdNode*));
ddnodearray[0]=dd;
Cudd_DumpDot(gbm,1,ddnodearray,NULL,NULL,outfile);
free(ddnodearray);
fclose(outfile);
}
int main(int argc, char *argv[])
{
DdManager *gbm;
char filename[30];
gbm=Cudd_Init(0,0,CUDD_UNIQUE_SLOTS,CUDD_CACHE_SLOTS,0);
DdNode *bdd=Cudd_bddNewVar(gbm);
Cudd_Ref(bdd);
bdd=Cudd_BddToAdd(gbm,bdd);
print_dd(gbm,bdd,2,4);
sprintf(filename,"./bdd/graph.dot");
write_dd(gbm,bdd,filename);
Cudd_Quit(gbm);
return 0;
}
I am getting some error during compilation.
gcc -I /home/subhadip/cudd-3.0.0 -I /home/subhadip/cudd-3.0.0/util -I /home/subhadip/cudd-3.0.0/cudd transfer1.c /home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a -o transfer1
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-cuddAPI.o): In function `Cudd_ExpectedUsedSlots':
/home/subhadip/cudd-3.0.0/cudd/cuddAPI.c:1835: undefined reference to `exp'
/home/subhadip/cudd-3.0.0/cudd/cuddAPI.c:1844: undefined reference to `exp'
/home/subhadip/cudd-3.0.0/cudd/cuddAPI.c:1850: undefined reference to `exp'
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-cuddCache.o): In function `cuddCacheProfile':
/home/subhadip/cudd-3.0.0/cudd/cuddCache.c:816: undefined reference to `exp'
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-cuddUtil.o): In function `Cudd_CountMinterm':
/home/subhadip/cudd-3.0.0/cudd/cuddUtil.c:595: undefined reference to `pow'
/home/subhadip/cudd-3.0.0/cudd/cuddUtil.c:595: undefined reference to `pow'
/home/subhadip/cudd-3.0.0/cudd/cuddUtil.c:595: undefined reference to `pow'
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-cuddUtil.o): In function `Cudd_LdblCountMinterm':
/home/subhadip/cudd-3.0.0/cudd/cuddUtil.c:729: undefined reference to `powl'
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-cuddUtil.o): In function `Cudd_CountMinterm':
/home/subhadip/cudd-3.0.0/cudd/cuddUtil.c:595: undefined reference to `pow'
/home/subhadip/cudd-3.0.0/cudd/cuddUtil.c:595: undefined reference to `pow'
/home/subhadip/cudd-3.0.0/cudd/cuddUtil.c:595: undefined reference to `pow'
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-epd.o): In function `EpdNormalizeDecimal':
/home/subhadip/cudd-3.0.0/epd/epd.c:834: undefined reference to `pow'
/home/subhadip/cudd-3.0.0/epd/epd.c:834: undefined reference to `pow'
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-epd.o):/home/subhadip/cudd-3.0.0/epd/epd.c:452: more undefined references to `pow' follow
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-cuddAnneal.o): In function `siftBackwardProb':
/home/subhadip/cudd-3.0.0/cudd/cuddAnneal.c:671: undefined reference to `exp'
/home/subhadip/cudd-3.0.0/cudd/cuddAnneal.c:671: undefined reference to `exp'
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-cuddAnneal.o): In function `cuddAnnealing':
/home/subhadip/cudd-3.0.0/cudd/cuddAnneal.c:229: undefined reference to `log'
/home/subhadip/cudd-3.0.0/cudd/cuddAnneal.c:229: undefined reference to `log'
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-cuddAnneal.o): In function `siftBackwardProb':
/home/subhadip/cudd-3.0.0/cudd/cuddAnneal.c:671: undefined reference to `exp'
collect2: error: ld returned 1 exit status
I have tried to statically link the libraries but the there is a problem. How can I fix it?
You compiled cudd and generated a static library. Now you need to link with it:
gcc .. <other options> ... transfer1.c /home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a -o transfer1
Note that the order of files matter.
I can guess that for C++ support you have to link with cplusplus/.libs/libobj.a and for dddmp support you need symbols exported in dddmp/.libs/libdddmp.a.
After using the above solution, if the error still persists,
For me, I have not made the folder named 'bdd' in the proper location for the code line:
sprintf(filename, "./bdd/graph.dot");
Now, it is executing for me.
I'm having trouble compiling my code, which uses a mutex (so uses pthread locks and conditions). I've tried including the header file, compiling with -pthread or -lpthread, but I'm still getting an error. Help would be much appreciated.
This is the error output:
implicit declaration of function ‘Pthread_mutex_lock’ [-Wimplicit-function-declaration]
Pthread_mutex_lock(&lock); //locked
^
/tmp/cchVS47i.o: In function getMessage1':
hw3.c:(.text+0x22): undefined reference toPthread_mutex_lock'
hw3.c:(.text+0x50): undefined reference to Pthread_mutex_lock'
/tmp/cchVS47i.o: In functiongetMessage2':
hw3.c:(.text+0x13e): undefined reference to `Pthread_mutex_lock'
collect2: error: ld returned 1 exit status
And here's relevant sections of my code (edited for clarity):
#define _GNU_SOURCE
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>
char message[1001];
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condition = PTHREAD_COND_INITIALIZER;
void *getMessage1()
{
Pthread_mutex_lock(&lock); //locked
....
}
int main(void)
{
pthread_t id1;
pthread_t id2;
pthread_create((&id1), NULL, getMessage1, NULL);
pthread_create((&id2), NULL, getMessage2, NULL);
...
return 0;
}
It's the capital P you have in
Pthread_mutex_lock(&lock); //locked
in the beginning of the function getmessage1().
Your compiler is complaining that it has not seen a declaration for that function in the compiling phase. Also it is complaining about that in the linking phase. You are including all the right libraries because it is not complaining about any of the other functions that are correctly typed.
The correct name of the function is pthread_mutex_lock().
In original k&r C it is possible to use functions without declarations, although compilers warn about them. In more modern versions of C (99) this has been deprecated.
I'm trying out the examples in the xmlrpc-c documentation:
#include <stdio.h>
#include <xmlrpc.h>
#include <xmlrpc_server.h>
//#include <xmlrpc_server_abyss.h>
#include <xmlrpc_abyss.h>
#include <xmlrpc-c/base.h>
#include <xmlrpc-c/util.h>
static xmlrpc_value *
sample_add(xmlrpc_env * const envP,
xmlrpc_value * const paramArrayP,
void * const serverContext) {
xmlrpc_int32 x, y, z;
/* Parse our argument array. */
xmlrpc_decompose_value(envP, paramArrayP, "(ii)", &x, &y);
if (envP->fault_occurred)
return NULL;
/* Add our two numbers. */
z = x + y;
/* Return our result. */
return xmlrpc_build_value(envP, "i", z);
}
int
main (int const argc,
const char ** const argv) {
xmlrpc_server_abyss_parms serverparm;
xmlrpc_registry * registryP;
xmlrpc_env env;
xmlrpc_env_init(&env);
registryP = xmlrpc_registry_new(&env);
xmlrpc_registry_add_method(
&env, registryP, NULL, "sample.add", &sample_add, NULL);
serverparm.config_file_name = argv[1];
serverparm.registryP = registryP;
printf("Starting XML-RPC server...\n");
xmlrpc_server_abyss(&env, &serverparm, XMLRPC_APSIZE(registryP));
return 0;
}
I try to compile using gcc:
gcc source.c
nohting fancy and I get:
/tmp/ccfGuc6A.o: In function sample_add':
source.c:(.text+0x38): undefined reference toxmlrpc_decompose_value'
source.c:(.text+0x6d): undefined reference to xmlrpc_build_value'
/tmp/ccfGuc6A.o: In functionmain':
source.c:(.text+0x96): undefined reference to xmlrpc_env_init'
source.c:(.text+0xa5): undefined reference toxmlrpc_registry_new'
source.c:(.text+0xd8): undefined reference to xmlrpc_registry_add_method'
source.c:(.text+0x117): undefined reference toxmlrpc_server_abyss'
collect2: error: ld returned 1 exit status
these functions exist in the:
/usr/include/xmlrpc-c/base.h
whihc I have referenced:
include
I think I'm not passing the right options to link, I don't know how it's done though.
thanks
You definitely don't pass the correct argument for the linker. Just including a header file doesn't actually make the linker link with the library, you need to use the -l (lower-case L) option to tell the linker which libraries you need to link with, like
gcc source.c -lxmlrpc
I believe that xml-rpc-c comes with a helper program, intended to help you get the linking right. Its documented here
http://xmlrpc-c.sourceforge.net/doc/xmlrpc-c-config.html
This question already has answers here:
Undefined reference to `pow' and `floor'
(6 answers)
Closed 9 years ago.
I wrote this function
int *constructST(int arr[], int n)
{
// Allocate memory for segment tree
int x = (int)(ceil(log2(n))); //Height of segment tree
int max_size = 2*(int)pow(2, x) - 1; //Maximum size of segment tree
int *st = malloc(max_size*sizeof(int));
// Fill the allocated memory st
constructSTUtil(arr, 0, n-1, st, 0);
// Return the constructed segment tree
return st;
}
and I have included the following libraries math.h ,stdlib.h, stdio.h but I get the following error
/tmp/ccg4X72c.o: In function `constructST':
tree.c:(.text+0x3f4): undefined reference to `log2'
tree.c:(.text+0x40b): undefined reference to `ceil'
tree.c:(.text+0x433): undefined reference to `pow'
collect2: error: ld returned 1 exit status
Any help why I am getting this error though I have included the math.h .
Including <math.h> ensures that your program knows the function prototypes for these functions, so it can tell that you're calling them correctly, but it doesn't actually link the library code in to them. For that you will have to add the right linker flag when you build it, usually -lm:
gcc -o myprog myprog.c -lm