This question already has answers here:
Undefined reference to pthread_create in Linux
(16 answers)
Closed 7 years ago.
I am writing a c code on ubuntu that creates a certain number of threads
I have already added pthreads library but when I run the code
it ends up with this error
Threads.cc:(.text+0x128): undefined reference to 'pthread_create'
Threads.cc:(.text+0x15b): undefined reference to 'pthread_join'
I am using ubuntu 15.04 virtual machine.
I have tried many suggested solutions, non of them worked!
any help would be appreciated
I have already added pthreads library [..]
No. You haven't. If you did you wouldn't get this problem. You probably mean to say you included <pthread.h>. But that doesn't link with pthreads library.
Add pthread at the end of your compilation options. For example,
gcc -o out myfile.c -pthread
Related
I am using strlcpy and strlcat in place of strncat/cpy; however, whenever I go to compile it
GCC -o Project Project.c it will continuously through me errors saying:
Undefined reference to strlcpy; Undefined reference to 'strlcat'
My code:
#include <bsd/string.h>
strlcpy(path, ARTICLEPATH, sizeof(ARTICLEPATH));
strlcat(path, ARTICLEPATH, sizeof(path));
I have added the library to my file, but it seems to continue to throw me the error:
#include <bsd/string.h>
Is there something else I need to do? Or is there an another alternative to using strncpy that utilizes null byte termination?
EDIT: As background, I am on ubuntu 20.04
As is (sort of) explained in the libbsd man page, you need to link the libbsd library as well as including the header. So add -lbsd to your command line when linking. For a simple program, you might do
gcc -o prog prog.c -lbsd
Note that the ordering of options is important here, see Why does the order in which libraries are linked sometimes cause errors in GCC? If you put -lbsd before your source file on the command line, it will probably not work.
The way you asked the question suggests you might have some confusion about the difference between a header and a library, and the roles that each one plays. You may want to read What's the difference between a header file and a library?
(This is almost a duplicate of when I use strlcpy function in c the compilor give me an error, but that question is more generic and some of the answers aren't applicable to Ubuntu specifically, so I thought a separate answer would be useful.)
This question already has answers here:
Are nested functions a bad thing in gcc ? [closed]
(11 answers)
Implementation of nested functions
(1 answer)
Nested function in C
(9 answers)
Closed 3 years ago.
I wrote a small program in C on Ubuntu having function definitions within the main function and compiled it using GCC. It compiled and ran without error. However, writing the same program in Codeblocks on Windows doesn't compile, and issues an error that says function-definition is not allowed here.
So, does GCC on Ubuntu allow function definitions within a function? Will it always run perfectly (given that it has compiled and there is no logical error in the program)?
This question already has answers here:
sqrt() function link error
(5 answers)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
I have a problem that when i compile a program whit pow or cos it be like:
main':<br/>just-test.c:(.text+0x3d): undefined reference tosqrt'
just-test.c:(.text+0x5e): undefined reference to 'pow'
collect2: error: ld returned 1 exit status
p.s: "im using linux and atom editor"
You don't need to download anything. But you have to tell the linker, that it should add the math library to your executable. Do this by adding -lm to the linker command line.
-l adds a library to linking, and the math library is simply called m.
This question already has answers here:
Linking with GCC and -lm doesn't define ceil() on Ubuntu
(3 answers)
undefined reference to "only some math.h" functions
(4 answers)
Why do you have to link the math library in C?
(14 answers)
gcc: why is the -lm flag needed to link the math library? [duplicate]
Closed 5 years ago.
On an online C compiler called jdoodle, I tried this simple snippet below:
#include<math.h>
#include<stdio.h>
int main(void)
{
double f = 1.2;
//printf("%f\n", ceil(f));
printf("%f\n", ceil(1.2));
return 0;
}
It prints:
2.000000
Which is what I expected.
But when I change code to:
printf("%f\n", ceil(f));
//printf("%f\n", ceil(1.2));
The compiler complains:
/tmp/ccv6kz5w.o: In function `main':
jdoodle.c:(.text+0x23): undefined reference to `ceil'
collect2: error: ld returned 1 exit status
Its fairly simple and clear from the man page for ceil() that it takes a double variable as the only argument.
When I changed the compiler version to 5.3.0 from 7.2.0, both codes were compiled successfully and generated the expected output.
Why is the updated version of compiler complaining about it then?
If compiler is right about complaining it, can anyone tell me why ceil(f); would be a problematic piece of code so that gcc-7.2.0 is not considering it valid, surprisingly assigning 'an undefined reference error' to a valid library function?
Update: I tried the same snippet with codechef online compiler with C-GCC6.3, it compiles fine and generates the expected output.
The man page for ceil(3) document that:
you need to #include <math.h>
and that you should
Link with -lm.
You forgot to configure your online compiler to link with -lm; perhaps the one you are using don't offer such an option.
I recommend compiling on your own computer.
This question already has answers here:
Getting undefined reference to 'clock_gettime' error, for curl program
(2 answers)
Closed 5 years ago.
I have this piece of c code that I am in charge of speeding up. The code is put onto a RasPi and compiled. Two years ago the code was put on and compiled and it works. Now when I try to compile the same file it says there's an undefined reference to 'clock_gettime'. I looked it up and that function is defined in time.h. I thought that maybe that header wasn't installed or called in the code. At the beginning of the code it does say #include so that's not the problem. I checked if the time.h header was installed on the RasPi and it was there with the other headers. I opened it up with nano and the clock_gettime function was defined, so that's not the problem. What should I do? How do I solve this problem?
clock_gettime(2):
#include <time.h>
...
Link with -lrt (only for glibc versions before 2.17).