about gcc

Asked by prasad.ram

hi sir i am big fan of ubuntu
i write a program in c language that is ..
        double a,b=1;

it gives error like
/tmp/cc1CYiQo.o: In function `main':
sample.c:(.text+0x17): undefined reference to `exp'
collect2: ld returned 1 exit status
i don't know what is the reason
the same program runs with out any error on another complier like tc(windows) and on same gnu c++ complier
so what is the reason please help me...

Question information

English Edit question
Ubuntu gcc-defaults Edit question
No assignee Edit question
Last query:
Last reply:
Revision history for this message
mycae (mycae) said :

Using g++ automatically imports certain libraries (libstdc++ and libm) implicitly. using cc does not. gcc is a "wrapper" program around both of these (and more) programs.

If, in your example, you renamed your file to .cpp, then you would find it magically worked, because the implicit linker flags have changed.

If you just add -lm, in order to import the C math library (which is not included by default), then you would link successfully.

Revision history for this message
prasad.ram (prasad-ram126) said :

sir yes it's working
but i already include math.h header file so i did not what is the necessity
of -lm flag can you explain briefly

On Sun, Feb 26, 2012 at 7:20 PM, marcobra (Marco Braida) <
<email address hidden>> wrote:

> Your question #188894 on gcc-defaults in Ubuntu changed:
> https://answers.launchpad.net/ubuntu/+source/gcc-defaults/+question/188894
> Project: Ubuntu => gcc-defaults in Ubuntu
> --
> You received this question notification because you asked the question.

Revision history for this message
mycae (mycae) said :

You have to remember that when using a C compiler, compilation occurs in several phases.

The first phase is preprocessing, using the tool "cpp". This does all the #ifdef, #define and #include substitution, and results in a preprocessed C file.

Next, you have compilation, where syntax errors and the like are detected, and your code is converted into CPU instructions.

Finally, you have the linking step. The linking step creates the necessary hooks to call out to other library functions. One thing you could theoretically do, for example, is switch the normal maths library (libm) out for a debugging one, or a faster embedded library. Libraries provide function calls, like exp() and sqrt() - the computer probably does not have built in exponentiation or sqrt caculations, and probably uses tools like Newton-Raphson, or geometric sums in order to compute some of this using standard algorithms.

So #including *only* affects the preprocess stage, and does handle any functions implementations. It does not tell the compiler which maths library (ie a concrete sqrt function) to use. More than one sqrt() function could exist on your system, in theory, both providing an implementation compliant with the same header file


Can you help with this problem?

Provide an answer of your own, or ask prasad.ram for more information if necessary.

To post a message you must log in.