Problem with GCC

Hello community,

I know this is not an algorithms related question, but, I was just testing some C programs for university here and found out that somehow my C compiler does NOT recognizes any functions included in the #include <math.h> header.

Even when I attempt to compile with -lm compiler flag to link the code against the math library it gives me an error of “undefined reference to sqrt, pow”…

I am running Ubuntu 12.10 and here is the result of my gcc -v command:

 gcc -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.2-2ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1)

I have checked gcc wiki and Ubuntu forums and got nowhere… Also, the same code compiled on IDEONE runs perfectly… Any help would be highly appreciated :slight_smile:

Thanks in advance,


Compile your program like this :

gcc -o estruturas structs_1.c -lm

NOTE that you need to put -lm flag in the end of the command. In newer versions of gcc the linker flag need to be placed in the end.


Thank you very much ron2794,

This was exactly what I needed :smiley: Now all is working properly

Best regards,

Bruno Oliveira

what is the exact command you are using to compile this program ?

gcc -lm -o estruturas structs_1.c

where structs_1.c is the source file…

Correct :slight_smile: