Re: Problem compiling simple C program



On 16/04/2008, John Salmon <salmonjj@xxxxxxxxxxx> 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. ???


"test" is a bash-builtin, and is a executable in /usr/bin/test, so
that is what you were running.
You can run your "test" command by typing
./test
or rename it to something else.


--
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
    ... >> I'm running Debian Etch on a PC. ... When I try to compile the ... > end_ of the gcc command line, otherwise the linker does not know ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx ...
    (Debian-User)
  • Re: --initrd
    ... In the past I could compile a kernel like that and when I install the ... which command I have used to generate it. ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx with a subject of "unsubscribe". ...
    (Debian-User)
  • Trouble with Ensoniq 5880 in ALSA
    ... I have tried to compile and insert the ALSA modules. ... To UNSUBSCRIBE, email to debian-user-request@lists.debian.org ...
    (Debian-User)
  • RE: Anjuta and RH9.
    ... If I try to build or just compile hello.c, ... command line message in the build window, but the command is not started. ... Subject: Anjuta and RH9. ... unsubscribe mailto:redhat-list-request@redhat.com?subject=unsubscribe ...
    (RedHat)
  • Re: Problem in calling c programs and compiling them in tcl/tk
    ... Are you using Tcl's glob command to do this? ... using exec gcc filename.c and later exec ./a.out filename.c. ... the execution of a command that might generate a Tcl error. ... i can call a c program and compile it? ...
    (comp.lang.tcl)