How to compile a C program that uses math.h?
I am having problems in math.h header file and when I use square root function as in sqrt(d)
. But my compiler is not supporting this. Please advise me about this problem.
My Ubuntu version is 2012.
compiling gcc c
|
show 1 more comment
I am having problems in math.h header file and when I use square root function as in sqrt(d)
. But my compiler is not supporting this. Please advise me about this problem.
My Ubuntu version is 2012.
compiling gcc c
2
Whilst we love programming questions - the question must be related to Ubuntu - can you clarify in detail what the issue is here - examples etc?
– fossfreedom♦
Aug 15 '13 at 10:37
it is like sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))... my compiler is not supporting this command
– shreya
Aug 15 '13 at 10:43
1
please respond by editing your question. Give code examples, how you are compiling, version of ubuntu, version of the compiler, have you tried to do this on another compiler/distro. What happens etc?
– fossfreedom♦
Aug 15 '13 at 10:45
1
@shreya Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
– Braiam
Aug 15 '13 at 11:07
3
I really don't think this is off-topic. On many other OSes, like Windows, it is unnecessary to pass a special linker option to use the math library in C programs. Even on some Unix-like operating systems the standard C library implementation doesn't require this. This is a problem many users face when they switch to GNU/Linux systems like Ubuntu. I wish the question were better written to include the specific error--if we end up considering this on-topic then perhaps we should edit it--but this is not a general C programming question, it's a question about how to usegcc
on Ubuntu.
– Eliah Kagan
Aug 22 '17 at 11:28
|
show 1 more comment
I am having problems in math.h header file and when I use square root function as in sqrt(d)
. But my compiler is not supporting this. Please advise me about this problem.
My Ubuntu version is 2012.
compiling gcc c
I am having problems in math.h header file and when I use square root function as in sqrt(d)
. But my compiler is not supporting this. Please advise me about this problem.
My Ubuntu version is 2012.
compiling gcc c
compiling gcc c
edited Aug 15 '13 at 11:19
Eliah Kagan
82.6k22227369
82.6k22227369
asked Aug 15 '13 at 10:34
shreyashreya
78114
78114
2
Whilst we love programming questions - the question must be related to Ubuntu - can you clarify in detail what the issue is here - examples etc?
– fossfreedom♦
Aug 15 '13 at 10:37
it is like sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))... my compiler is not supporting this command
– shreya
Aug 15 '13 at 10:43
1
please respond by editing your question. Give code examples, how you are compiling, version of ubuntu, version of the compiler, have you tried to do this on another compiler/distro. What happens etc?
– fossfreedom♦
Aug 15 '13 at 10:45
1
@shreya Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
– Braiam
Aug 15 '13 at 11:07
3
I really don't think this is off-topic. On many other OSes, like Windows, it is unnecessary to pass a special linker option to use the math library in C programs. Even on some Unix-like operating systems the standard C library implementation doesn't require this. This is a problem many users face when they switch to GNU/Linux systems like Ubuntu. I wish the question were better written to include the specific error--if we end up considering this on-topic then perhaps we should edit it--but this is not a general C programming question, it's a question about how to usegcc
on Ubuntu.
– Eliah Kagan
Aug 22 '17 at 11:28
|
show 1 more comment
2
Whilst we love programming questions - the question must be related to Ubuntu - can you clarify in detail what the issue is here - examples etc?
– fossfreedom♦
Aug 15 '13 at 10:37
it is like sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))... my compiler is not supporting this command
– shreya
Aug 15 '13 at 10:43
1
please respond by editing your question. Give code examples, how you are compiling, version of ubuntu, version of the compiler, have you tried to do this on another compiler/distro. What happens etc?
– fossfreedom♦
Aug 15 '13 at 10:45
1
@shreya Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
– Braiam
Aug 15 '13 at 11:07
3
I really don't think this is off-topic. On many other OSes, like Windows, it is unnecessary to pass a special linker option to use the math library in C programs. Even on some Unix-like operating systems the standard C library implementation doesn't require this. This is a problem many users face when they switch to GNU/Linux systems like Ubuntu. I wish the question were better written to include the specific error--if we end up considering this on-topic then perhaps we should edit it--but this is not a general C programming question, it's a question about how to usegcc
on Ubuntu.
– Eliah Kagan
Aug 22 '17 at 11:28
2
2
Whilst we love programming questions - the question must be related to Ubuntu - can you clarify in detail what the issue is here - examples etc?
– fossfreedom♦
Aug 15 '13 at 10:37
Whilst we love programming questions - the question must be related to Ubuntu - can you clarify in detail what the issue is here - examples etc?
– fossfreedom♦
Aug 15 '13 at 10:37
it is like sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))... my compiler is not supporting this command
– shreya
Aug 15 '13 at 10:43
it is like sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))... my compiler is not supporting this command
– shreya
Aug 15 '13 at 10:43
1
1
please respond by editing your question. Give code examples, how you are compiling, version of ubuntu, version of the compiler, have you tried to do this on another compiler/distro. What happens etc?
– fossfreedom♦
Aug 15 '13 at 10:45
please respond by editing your question. Give code examples, how you are compiling, version of ubuntu, version of the compiler, have you tried to do this on another compiler/distro. What happens etc?
– fossfreedom♦
Aug 15 '13 at 10:45
1
1
@shreya Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
– Braiam
Aug 15 '13 at 11:07
@shreya Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
– Braiam
Aug 15 '13 at 11:07
3
3
I really don't think this is off-topic. On many other OSes, like Windows, it is unnecessary to pass a special linker option to use the math library in C programs. Even on some Unix-like operating systems the standard C library implementation doesn't require this. This is a problem many users face when they switch to GNU/Linux systems like Ubuntu. I wish the question were better written to include the specific error--if we end up considering this on-topic then perhaps we should edit it--but this is not a general C programming question, it's a question about how to use
gcc
on Ubuntu.– Eliah Kagan
Aug 22 '17 at 11:28
I really don't think this is off-topic. On many other OSes, like Windows, it is unnecessary to pass a special linker option to use the math library in C programs. Even on some Unix-like operating systems the standard C library implementation doesn't require this. This is a problem many users face when they switch to GNU/Linux systems like Ubuntu. I wish the question were better written to include the specific error--if we end up considering this on-topic then perhaps we should edit it--but this is not a general C programming question, it's a question about how to use
gcc
on Ubuntu.– Eliah Kagan
Aug 22 '17 at 11:28
|
show 1 more comment
2 Answers
2
active
oldest
votes
Append -lm
to the end of your gcc
command.
With all recent versions of GCC on GNU/Linux systems like Ubuntu, when you use the math library, you have to explicitly link to it. It is not automatically linked to along with the rest of the standard C library.
If you are compiling on the command-line with the gcc
or g++
command, you would accomplish this by putting -lm
at the end of the command.
For example: gcc -o foo foo.c -lm
add a comment |
If you are going to compile a C program with math.h library in LINUX using GCC or G++ you will have to use –lm option after the compile command.
gcc xyz.c -o xyz -lm
Here,
gcc is compiler command (compiler name)
xyz.c is a source file name.
-o is an option to specify the output file.
xyz is the name of the output file.
-lm is an option to link againt the math library (libm).
for more details here is the link containing complete article on it.
Compiling C program with math.h in Linux.
After the linking command, not the compile command. (Sorry for the nitpicking, but getting it right helps to understand other situations).
– mousomer
Sep 29 '16 at 7:02
add a comment |
protected by Sergiy Kolodyazhnyy Jan 23 at 11:28
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Append -lm
to the end of your gcc
command.
With all recent versions of GCC on GNU/Linux systems like Ubuntu, when you use the math library, you have to explicitly link to it. It is not automatically linked to along with the rest of the standard C library.
If you are compiling on the command-line with the gcc
or g++
command, you would accomplish this by putting -lm
at the end of the command.
For example: gcc -o foo foo.c -lm
add a comment |
Append -lm
to the end of your gcc
command.
With all recent versions of GCC on GNU/Linux systems like Ubuntu, when you use the math library, you have to explicitly link to it. It is not automatically linked to along with the rest of the standard C library.
If you are compiling on the command-line with the gcc
or g++
command, you would accomplish this by putting -lm
at the end of the command.
For example: gcc -o foo foo.c -lm
add a comment |
Append -lm
to the end of your gcc
command.
With all recent versions of GCC on GNU/Linux systems like Ubuntu, when you use the math library, you have to explicitly link to it. It is not automatically linked to along with the rest of the standard C library.
If you are compiling on the command-line with the gcc
or g++
command, you would accomplish this by putting -lm
at the end of the command.
For example: gcc -o foo foo.c -lm
Append -lm
to the end of your gcc
command.
With all recent versions of GCC on GNU/Linux systems like Ubuntu, when you use the math library, you have to explicitly link to it. It is not automatically linked to along with the rest of the standard C library.
If you are compiling on the command-line with the gcc
or g++
command, you would accomplish this by putting -lm
at the end of the command.
For example: gcc -o foo foo.c -lm
edited Aug 22 '17 at 11:50
answered Aug 15 '13 at 12:47
Eliah KaganEliah Kagan
82.6k22227369
82.6k22227369
add a comment |
add a comment |
If you are going to compile a C program with math.h library in LINUX using GCC or G++ you will have to use –lm option after the compile command.
gcc xyz.c -o xyz -lm
Here,
gcc is compiler command (compiler name)
xyz.c is a source file name.
-o is an option to specify the output file.
xyz is the name of the output file.
-lm is an option to link againt the math library (libm).
for more details here is the link containing complete article on it.
Compiling C program with math.h in Linux.
After the linking command, not the compile command. (Sorry for the nitpicking, but getting it right helps to understand other situations).
– mousomer
Sep 29 '16 at 7:02
add a comment |
If you are going to compile a C program with math.h library in LINUX using GCC or G++ you will have to use –lm option after the compile command.
gcc xyz.c -o xyz -lm
Here,
gcc is compiler command (compiler name)
xyz.c is a source file name.
-o is an option to specify the output file.
xyz is the name of the output file.
-lm is an option to link againt the math library (libm).
for more details here is the link containing complete article on it.
Compiling C program with math.h in Linux.
After the linking command, not the compile command. (Sorry for the nitpicking, but getting it right helps to understand other situations).
– mousomer
Sep 29 '16 at 7:02
add a comment |
If you are going to compile a C program with math.h library in LINUX using GCC or G++ you will have to use –lm option after the compile command.
gcc xyz.c -o xyz -lm
Here,
gcc is compiler command (compiler name)
xyz.c is a source file name.
-o is an option to specify the output file.
xyz is the name of the output file.
-lm is an option to link againt the math library (libm).
for more details here is the link containing complete article on it.
Compiling C program with math.h in Linux.
If you are going to compile a C program with math.h library in LINUX using GCC or G++ you will have to use –lm option after the compile command.
gcc xyz.c -o xyz -lm
Here,
gcc is compiler command (compiler name)
xyz.c is a source file name.
-o is an option to specify the output file.
xyz is the name of the output file.
-lm is an option to link againt the math library (libm).
for more details here is the link containing complete article on it.
Compiling C program with math.h in Linux.
edited Apr 21 '17 at 19:49
Community♦
1
1
answered Mar 12 '16 at 18:57
MikeMike
372
372
After the linking command, not the compile command. (Sorry for the nitpicking, but getting it right helps to understand other situations).
– mousomer
Sep 29 '16 at 7:02
add a comment |
After the linking command, not the compile command. (Sorry for the nitpicking, but getting it right helps to understand other situations).
– mousomer
Sep 29 '16 at 7:02
After the linking command, not the compile command. (Sorry for the nitpicking, but getting it right helps to understand other situations).
– mousomer
Sep 29 '16 at 7:02
After the linking command, not the compile command. (Sorry for the nitpicking, but getting it right helps to understand other situations).
– mousomer
Sep 29 '16 at 7:02
add a comment |
protected by Sergiy Kolodyazhnyy Jan 23 at 11:28
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
2
Whilst we love programming questions - the question must be related to Ubuntu - can you clarify in detail what the issue is here - examples etc?
– fossfreedom♦
Aug 15 '13 at 10:37
it is like sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))... my compiler is not supporting this command
– shreya
Aug 15 '13 at 10:43
1
please respond by editing your question. Give code examples, how you are compiling, version of ubuntu, version of the compiler, have you tried to do this on another compiler/distro. What happens etc?
– fossfreedom♦
Aug 15 '13 at 10:45
1
@shreya Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
– Braiam
Aug 15 '13 at 11:07
3
I really don't think this is off-topic. On many other OSes, like Windows, it is unnecessary to pass a special linker option to use the math library in C programs. Even on some Unix-like operating systems the standard C library implementation doesn't require this. This is a problem many users face when they switch to GNU/Linux systems like Ubuntu. I wish the question were better written to include the specific error--if we end up considering this on-topic then perhaps we should edit it--but this is not a general C programming question, it's a question about how to use
gcc
on Ubuntu.– Eliah Kagan
Aug 22 '17 at 11:28