undefined reference to `sqrt'
up vote
2
down vote
favorite
Part of my program is to calculate sqrt
of float number.
When I write sqrt(1.0f);
I success to compile the program,but when I write sqrt(-1.0f);
the compilation fails with undefined reference to 'sqrt'
- I suppose that in this case the nan
value will be returned...
I compile the program uing gcc.
When I compile it with visual studio it is compiled successfuly with negative argument to sqrt.
How the problem could be solved
Thank you
c nan sqrt
add a comment |
up vote
2
down vote
favorite
Part of my program is to calculate sqrt
of float number.
When I write sqrt(1.0f);
I success to compile the program,but when I write sqrt(-1.0f);
the compilation fails with undefined reference to 'sqrt'
- I suppose that in this case the nan
value will be returned...
I compile the program uing gcc.
When I compile it with visual studio it is compiled successfuly with negative argument to sqrt.
How the problem could be solved
Thank you
c nan sqrt
gcc myprog.c -o myprog -lm the -l to include a library m for the math library -lm.
– old_timer
Jan 8 '12 at 19:41
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Part of my program is to calculate sqrt
of float number.
When I write sqrt(1.0f);
I success to compile the program,but when I write sqrt(-1.0f);
the compilation fails with undefined reference to 'sqrt'
- I suppose that in this case the nan
value will be returned...
I compile the program uing gcc.
When I compile it with visual studio it is compiled successfuly with negative argument to sqrt.
How the problem could be solved
Thank you
c nan sqrt
Part of my program is to calculate sqrt
of float number.
When I write sqrt(1.0f);
I success to compile the program,but when I write sqrt(-1.0f);
the compilation fails with undefined reference to 'sqrt'
- I suppose that in this case the nan
value will be returned...
I compile the program uing gcc.
When I compile it with visual studio it is compiled successfuly with negative argument to sqrt.
How the problem could be solved
Thank you
c nan sqrt
c nan sqrt
edited Jan 8 '12 at 19:42
Pascal Cuoq
66.5k6124227
66.5k6124227
asked Jan 8 '12 at 19:39
Yakov
3,4561478147
3,4561478147
gcc myprog.c -o myprog -lm the -l to include a library m for the math library -lm.
– old_timer
Jan 8 '12 at 19:41
add a comment |
gcc myprog.c -o myprog -lm the -l to include a library m for the math library -lm.
– old_timer
Jan 8 '12 at 19:41
gcc myprog.c -o myprog -lm the -l to include a library m for the math library -lm.
– old_timer
Jan 8 '12 at 19:41
gcc myprog.c -o myprog -lm the -l to include a library m for the math library -lm.
– old_timer
Jan 8 '12 at 19:41
add a comment |
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
You have to add the -lm
flag on most Unix-based systems, as in:
Compile using:
gcc -c file.c
and then link using:
gcc -o program file.o -lm
Or if you don't want to separate the two compilation steps, simply write:
gcc -o program file.c -lm
add a comment |
up vote
4
down vote
Link with -lm
to link with the math library
Can you please give a reason for it. I tried including <math.h> in headers, but then facing the error as mentioned in the question.
– Akash Chandwani
Nov 27 '16 at 18:06
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
You have to add the -lm
flag on most Unix-based systems, as in:
Compile using:
gcc -c file.c
and then link using:
gcc -o program file.o -lm
Or if you don't want to separate the two compilation steps, simply write:
gcc -o program file.c -lm
add a comment |
up vote
5
down vote
accepted
You have to add the -lm
flag on most Unix-based systems, as in:
Compile using:
gcc -c file.c
and then link using:
gcc -o program file.o -lm
Or if you don't want to separate the two compilation steps, simply write:
gcc -o program file.c -lm
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
You have to add the -lm
flag on most Unix-based systems, as in:
Compile using:
gcc -c file.c
and then link using:
gcc -o program file.o -lm
Or if you don't want to separate the two compilation steps, simply write:
gcc -o program file.c -lm
You have to add the -lm
flag on most Unix-based systems, as in:
Compile using:
gcc -c file.c
and then link using:
gcc -o program file.o -lm
Or if you don't want to separate the two compilation steps, simply write:
gcc -o program file.c -lm
answered Jan 8 '12 at 19:44
user529758
add a comment |
add a comment |
up vote
4
down vote
Link with -lm
to link with the math library
Can you please give a reason for it. I tried including <math.h> in headers, but then facing the error as mentioned in the question.
– Akash Chandwani
Nov 27 '16 at 18:06
add a comment |
up vote
4
down vote
Link with -lm
to link with the math library
Can you please give a reason for it. I tried including <math.h> in headers, but then facing the error as mentioned in the question.
– Akash Chandwani
Nov 27 '16 at 18:06
add a comment |
up vote
4
down vote
up vote
4
down vote
Link with -lm
to link with the math library
Link with -lm
to link with the math library
answered Jan 8 '12 at 19:41
Manuel Selva
10.3k1863117
10.3k1863117
Can you please give a reason for it. I tried including <math.h> in headers, but then facing the error as mentioned in the question.
– Akash Chandwani
Nov 27 '16 at 18:06
add a comment |
Can you please give a reason for it. I tried including <math.h> in headers, but then facing the error as mentioned in the question.
– Akash Chandwani
Nov 27 '16 at 18:06
Can you please give a reason for it. I tried including <math.h> in headers, but then facing the error as mentioned in the question.
– Akash Chandwani
Nov 27 '16 at 18:06
Can you please give a reason for it. I tried including <math.h> in headers, but then facing the error as mentioned in the question.
– Akash Chandwani
Nov 27 '16 at 18:06
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8780603%2fundefined-reference-to-sqrt%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
gcc myprog.c -o myprog -lm the -l to include a library m for the math library -lm.
– old_timer
Jan 8 '12 at 19:41