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.

Dani AI

Generated

Quick, practical primer that fills gaps in the replies above.

Headers (the files you #include) give the compiler declarations and type information only; they do not contain the compiled code. The linker is the step that brings in implementations from libraries. That is why a missing symbol like cosf is a link-time error even if the header is present. was right on the distinction; this expands on what to do next and how to diagnose similar problems.

A simple, safe workflow:

gcc -c main.c            # create main.o from main.c
gcc main.o util.o -lfoo -o myprog   # link: objects first, libraries after

Notes: use -I/path to add custom header search paths and -L/path to add library search paths. The -l option uses the basename (libfoo.so / libfoo.a → -lfoo). Put library flags after your object files because the linker resolves symbols in a single pass.

Extra tips and common pitfalls:

  • If you build C++ use g++ to link; it automatically pulls in the C++ runtime (otherwise you'll need to add -lstdc++ manually).
  • For thread libraries prefer -pthread (it sets compile and link options consistently).
  • To inspect why a symbol is missing use nm or objdump on the object/library, and run gcc -v to see the exact link command the driver invokes.

For and the gcov question: coverage (.gcda) files are written when an instrumented program runs and exits cleanly; compile with GCC’s coverage options (for example the driver has a --coverage mode), then run the program. If ./a.out “hangs,” that’s runtime behavior — use strace or run it under gdb to find blocking calls (waiting for stdin, locks, dead loops). ’s point stands: a non-responsive binary is a program bug, not a linker problem.

Recommended Answers

All 5 Replies

>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/

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

> 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.

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.

> 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.