954,141 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

gcc header files and math libary

I am a new to linux and the gcc system I was just wondering if someone could answer a couple of questions about the including hearder files like the math.h file.

I was making a simple program and using some math trig functions. I keep getting the error cosf undefined text or some thing like that so I got to looking around online and found out it was a linker error. So I typed in the command

Gcc –o test test.c –lm

My question is do I have to add all the include files to the command line?
If not how do I know witch ones I do have to add to the line?
How do you add more then one-header file to the linker?

Does anyone know of a good online resource that would walk me through using the gcc complier.

guy40az
Junior Poster in Training
57 posts since Mar 2007
Reputation Points: 10
Solved Threads: 2
 

>My question is do I have to add all the include files to the command line?
You don't add headers (include files) on the command line. They're strictly for the compiler so that it has declarations for the names that will be linked in later. What you're doing is telling the linker to link with a specific library; in this case that library is the math library. For the most part the standard libraries are linked automagically, with the math library being an exception.

>Does anyone know of a good online resource that
>would walk me through using the gcc complier.
http://gcc.gnu.org/onlinedocs/

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Hi,

I happen to bump around wiht this problem too.

I have a .c file and multiple header files.

After executing the -lm flag, I managed to get the executable file. However, when I tried to ./a.out, the compiler just hangs there without any response.

The motivation for me to execute ./a.out is I would like the gcc to generate gcda file for me from gcov.

Please help. Thanks.

-Jason

ahjiefreak
Newbie Poster
11 posts since Dec 2007
Reputation Points: 10
Solved Threads: 1
 

> the compiler just hangs there without any response.
The compiler is all done if you've got an a.out file. If that isn't responding, it can only be your source code which is at fault. You need to post some for further enlightenment.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

The source code works ok, its just when I compile using

Gcc test.c I get the linker error but when I use the

Gcc –o test test.c –lm the –lm fixes the problem.

I was just wondering if the math library is the only library I had to add to the
Command line.

guy40az
Junior Poster in Training
57 posts since Mar 2007
Reputation Points: 10
Solved Threads: 2
 

> I was just wondering if the math library is the only library I had to add to the
> Command line.
It's the only one of the standard header files you can include.
But if you include other non-standard headers, you'll need to link with other non-standard libraries as well.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You