Re: Problem compiling simple C program



John Salmon wrote:
Sven Joachim <svenjoac@xxxxxx> wrote in news:aiYRm-2Tc-23@xxxxxxxxxxxxxxxx:

On 2008-04-15 20:39 +0200, John Salmon wrote:

I'm running Debian Etch on a PC. When I try to compile the following (called test.c);

#include <math.h>
#include <stdio.h>

int main()
{
double
val = 1.55;

printf("sine: %g\n", sin(val));

return 0;
}

using the command line

gcc -Wall -o test test.c

I get

/tmp/cciDV02m.o: In function `main':
test.c:(.text+0x21): undefined reference to `sin'
collect2: ld returned 1 exit status
You need to link to the math library by specifying -lm _at the end_ of
the gcc command line, otherwise the linker does not know about the sin()
function.

When I compile the equivalent C++ program using the apropriate C++ parameters, everything goes great. Have I neglected to load a Debian package? Any help will be appreciated.
That is to be expected, because C++ programs are automatically linked
against the math library. In C you have to tell the linker to use it
with -lm.

Sven



That solved the linking problem. Now, after a successful compilation, when I run 'test' I gen no output. ???


In the directory where you source and output executable is, try "./test".

You are not getting any output probably because you are inadvertently using the system "test" command.

To avoid such introduction traps, I usually make such example programs with names quite different from any commands that the system may have, for example by using my initials or by using a number in the source file. Using "mytest" or "example1" would have been a better choice in your case.

->HS


--
To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx with a subject of "unsubscribe". Trouble? Contact listmaster@xxxxxxxxxxxxxxxx



Relevant Pages

  • Re: Problem compiling simple C program
    ... the gcc command line, otherwise the linker does not know about the sin ... http://www.pronews.com offers corporate packages that have access to 100,000+ newsgroups ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx ...
    (Debian-User)
  • Re: Problem compiling simple C program
    ... the gcc command line, otherwise the linker does not know about the sin ... In C you have to tell the linker to use it ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx ...
    (Debian-User)
  • Re: How do you make this Open Source work on Solaris 10?
    ... After running the tar -xvf command, I logged in /bin/csh and did the ... checking for gcc... ... checking for C compiler default output... ... checking how to recognise dependent libraries... ...
    (comp.unix.shell)
  • Re: How do you make this Open Source work on Solaris ?
    ... After running the tar -xvf command, I logged in /bin/csh and did the ... checking for gcc... ... checking for C compiler default output... ... checking how to recognise dependent libraries... ...
    (comp.unix.programmer)
  • Re: Problem in calling c programs and compiling them in tcl/tk
    ... using exec gcc filename.c and later exec ./a.out filename.c. ... the execution of a command that might generate a Tcl error. ... saying "any time I see this, ... The important thing is if i try to run the same using gcc helloworld.c ...
    (comp.lang.tcl)