You are not logged in.

#1 2008-04-21 17:33:54

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

GCC Math.h Support Broken?? [SOLVED]

My source code:

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

int main(void) {
    float x, y;
    y = 5;
    x = log(y);
    printf("%f\n", x);
    return 0;
}

Running GCC:

bash-3.2# gcc -o /usr/bin/testlog /home/karam/testlog.c
/tmp/ccC4o2tO.o: In function `main':
testlog.c:(.text+0x19): undefined reference to `log'
collect2: ld returned 1 exit status

Running GCC with the verbose tag:

Using built-in specs.
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --prefix=/usr --enable-shared --enable-languages=c,c++,fortran,objc,obj-c++,treelang --enable-threads=posix --mandir=/usr/share/man --enable-__cxa_atexit --disable-multilib --libdir=/usr/lib --libexecdir=/usr/lib --enable-clocale=gnu --disable-libstdcxx-pch --with-tune=generic
Thread model: posix
gcc version 4.3.0 (GCC)
COLLECT_GCC_OPTIONS='-o' '/usr/bin/testlog' '-v' '-mtune=generic'
 /usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/cc1 -quiet -v /home/karam/testlog.c -quiet -dumpbase testlog.c -mtune=generic -auxbase testlog -version -o /tmp/cccPgY2e.s
ignoring nonexistent directory "/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/../../../../x86_64-unknown-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/include
 /usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/include-fixed
 /usr/include
End of search list.
GNU C (GCC) version 4.3.0 (x86_64-unknown-linux-gnu)
        compiled by GNU C version 4.3.0, GMP version 4.2.2, MPFR version 2.3.1.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: f09caaaff33a7d24655f4bdc6a5a8cad
COLLECT_GCC_OPTIONS='-o' '/usr/bin/testlog' '-v' '-mtune=generic'
 as -V -Qy -o /tmp/ccuax4mo.o /tmp/cccPgY2e.s
GNU assembler version 2.18 (x86_64-unknown-linux-gnu) using BFD version (GNU Binutils) 2.18
COMPILER_PATH=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/:/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/:/usr/lib/gcc/x86_64-unknown-linux-gnu/:/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/:/usr/lib/gcc/x86_64-unknown-linux-gnu/:/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/:/usr/lib/gcc/x86_64-unknown-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/:/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/:/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-o' '/usr/bin/testlog' '-v' '-mtune=generic'
 /usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/collect2 --eh-frame-hdr -m elf_x86_64 --hash-style=both -dynamic-linker /lib/ld-linux-x86-64.so.2 -o /usr/bin/testlog /usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/../../../../lib/crt1.o /usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/../../../../lib/crti.o /usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/crtbegin.o -L/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0 -L/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0 -L/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/../../.. /tmp/ccuax4mo.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/crtend.o /usr/lib/gcc/x86_64-unknown-linux-gnu/4.3.0/../../../../lib/crtn.o
/tmp/ccuax4mo.o: In function `main':
testlog.c:(.text+0x19): undefined reference to `log'
collect2: ld returned 1 exit status

Last edited by tony5429 (2008-04-26 14:45:30)

Offline

#2 2008-04-21 19:03:25

winch
Member
Registered: 2008-04-13
Posts: 43

Re: GCC Math.h Support Broken?? [SOLVED]

You also need to tell gcc to link with the math library.

gcc -o /usr/bin/testlog /home/karam/testlog.c -lm

Last edited by winch (2008-04-21 19:06:41)

Offline

#3 2008-04-26 14:45:11

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: GCC Math.h Support Broken?? [SOLVED]

It worked; thanks!

Offline

#4 2009-05-05 14:19:45

g_SG
Member
From: São Carlos/SP, Brazil
Registered: 2009-01-20
Posts: 11
Website

Re: GCC Math.h Support Broken?? [SOLVED]

The -lm flag works, but is there I way to let gcc do it automatically? I use Vim and gcc manually at home, but my professor wants us to use Netbeans in class, and it won't compile because of that.

Offline

#5 2009-05-05 15:08:02

GogglesGuy
Member
From: Rocket City
Registered: 2005-03-29
Posts: 610
Website

Re: GCC Math.h Support Broken?? [SOLVED]

g_SG wrote:

The -lm flag works, but is there I way to let gcc do it automatically? I use Vim and gcc manually at home, but my professor wants us to use Netbeans in class, and it won't compile because of that.

You mean it won't link.
No. GCC doesn't have a crystal ball to guess which libraries you need.

Offline

#6 2009-05-05 17:13:21

caelestis
Member
Registered: 2009-04-04
Posts: 88

Re: GCC Math.h Support Broken?? [SOLVED]

Why not? Why isn't #include <math.h> good enough?

Offline

#7 2009-05-05 17:15:17

djgera
Developer
From: Buenos Aires - Argentina
Registered: 2008-12-24
Posts: 723
Website

Re: GCC Math.h Support Broken?? [SOLVED]

g_SG wrote:

The -lm flag works, but is there I way to let gcc do it automatically? I use Vim and gcc manually at home, but my professor wants us to use Netbeans in class, and it won't compile because of that.

Can use make? So can generate a simple Makefile

Offline

#8 2009-05-05 19:49:33

HashBox
Member
Registered: 2009-01-22
Posts: 271

Re: GCC Math.h Support Broken?? [SOLVED]

I find it hard to believe that Netbeans wouldn't have a method of defining linker arguments, have you looked around for something like that?

Offline

#9 2009-05-08 15:56:35

dagle
Member
Registered: 2009-01-04
Posts: 13

Re: GCC Math.h Support Broken?? [SOLVED]

caelestis wrote:

Why not? Why isn't #include <math.h> good enough?

For building but not for linking.

Offline

#10 2009-05-08 16:03:19

Lexion
Member
Registered: 2008-03-23
Posts: 510

Re: GCC Math.h Support Broken?? [SOLVED]

lol I just got this error today! Use -lm for linking: 'gcc allyourobjfiles.o -o exefile -lm'


urxvtc / wmii / zsh / configs / onebluecat.net
Arch will not hold your hand

Offline

#11 2009-05-09 02:23:46

fumbles
Member
Registered: 2006-12-22
Posts: 246

Re: GCC Math.h Support Broken?? [SOLVED]

.

Last edited by fumbles (2020-09-26 11:44:03)

Offline

#12 2009-05-15 11:51:25

g_SG
Member
From: São Carlos/SP, Brazil
Registered: 2009-01-20
Posts: 11
Website

Re: GCC Math.h Support Broken?? [SOLVED]

djgera wrote:
g_SG wrote:

The -lm flag works, but is there I way to let gcc do it automatically? I use Vim and gcc manually at home, but my professor wants us to use Netbeans in class, and it won't compile because of that.

Can use make? So can generate a simple Makefile

True! That's so obvious I'm ashame I didn't think about it. Thanks!

Offline

Board footer

Powered by FluxBB