3

I've been trying to run my program after compiling on a Ubuntu 12.04.4 LTS server without any luck. Note I've been developing on my Mac book without any issues.

I've already tried the following number of things:

  • add -fno-builtin to compile params
  • add -lm to compile params (Placing it on several different places before and after *.c files
  • added '#define _ISOC99_SOURCE' to source code files
  • used --std=gnu99 instead of --std=c99
  • add -lt to compile params
  • add -Wl,--no-as-needed to compile params

Can any one share with me what I'm doing wrong? Or what missing from my configuration? Below I added the content of my Makefile, output of compiling, the gcc -v output and the output when I try to run the program.

Output of program while trying to run it:

ext/adl-pure.a(adl-main.o): In function `adl_aggregate':
adl-main.c:(.text+0x1d89): undefined reference to `roundf'
adl-main.c:(.text+0x2503): undefined reference to `lroundf'
ext/adl-pure.a(adl-calc.o): In function `adl_get_am_day_target':
adl-calc.c:(.text+0x2f8): undefined reference to `lroundf'
ext/adl-pure.a(adl-calc.o): In function `adl_pal_percentage':
adl-calc.c:(.text+0x787): undefined reference to `floorf'
adl-calc.c:(.text+0x790): undefined reference to `lroundf'
collect2: ld returned 1 exit status

This is my make file:

# Makefile.lib

CC=gcc
AR=ar

CFLAGS=

.PHONY: all
.SUFFIXES:

ADL_SOURCES=adl-main.c adl-data.c adl-calc.c adl-fail.c adl-misc.c

all: adl-pure.a

adl-pure.a: $(ADL_SOURCES:.c=.o)
        $(AR) -rus $@ $^

%.o: %.c
        $(CC) -o $@ -MMD $< -lm -std=c99 -O2 -Wall -pedantic $(CFLAGS) -c

clean:
        rm -f adl-pure.a *.o *.d

-include $(ADL_SOURCES:.c=.d)

And this the output of it:

gcc -o adl-main.o -MMD adl-main.c -lm -std=c99 -O2 -Wall -pedantic -fPIC -c
gcc -o adl-data.o -MMD adl-data.c -lm -std=c99 -O2 -Wall -pedantic -fPIC -c
gcc -o adl-calc.o -MMD adl-calc.c -lm -std=c99 -O2 -Wall -pedantic -fPIC -c
gcc -o adl-fail.o -MMD adl-fail.c -lm -std=c99 -O2 -Wall -pedantic -fPIC -c
gcc -o adl-misc.o -MMD adl-misc.c -lm -std=c99 -O2 -Wall -pedantic -fPIC -c
ar -rus adl-pure.a adl-main.o adl-data.o adl-calc.o adl-fail.o adl-misc.o
ar: creating adl-pure.a

This is the output of gcc -v:

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --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.6 --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.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
Niels
  • 599
  • 5
  • 28
  • http://stackoverflow.com/questions/575936/how-am-i-incorrectly-using-the-round-function-in-c – user2485710 Mar 14 '14 at 09:18
  • What is the actual command that gives the undefined symbol error message in your post? – Lee Duhem Mar 14 '14 at 09:21
  • you should also take the habit of putting everything that is related to the linking phase at the end of your set of flags, with the name of the source/s file/s in the middle and everything else in the first part of your compiler invocation. – user2485710 Mar 14 '14 at 09:30
  • @user2485710 how would the command look like then in my case? – Niels Mar 14 '14 at 09:39
  • @leeduhem what do you mean with undefined symbol error? I inherited this C library and I'm very novice to C programming. – Niels Mar 14 '14 at 09:41
  • @Niels I meat those "undefined reference to XXX" messages. Which command those messages come from? – Lee Duhem Mar 14 '14 at 09:46
  • @leeduhem for example: week_data.act_points += roundf(day_data->act_points); and long percent = lround(cur_dao->value); – Niels Mar 14 '14 at 09:58
  • @Niels I mean the link command that you used to generate the executable file, not the calls to those functions. – Lee Duhem Mar 14 '14 at 10:00
  • I finally fixed my problem, the go library importing the compiled c library needed to import the library with -lm like so: `#cgo LDFLAGS: ext/adl-pure.a -lm` Thanks for all the help! – Niels Mar 14 '14 at 15:21

1 Answers1

2

This example

#include <math.h>
#include <stdio.h>
int main()
{
    float f = 33.33f;
    f = roundf(f);
    printf("%f\n", f);
    return (0);
}

compiles just fine with

gcc -std=c99 -fno-builtin a.c -lm

Change your Makefile accordingly and you will be fine.

user2485710
  • 9,451
  • 13
  • 58
  • 102
  • I changed my makefile accordingly this is the output now: .... gcc -std=c99 -fno-builtin -o adl-main.o -MMD adl-main.c -lm -O2 -Wall -pedantic -fPIC -c ... still no luck. – Niels Mar 14 '14 at 09:50
  • @Niels also take a look at the order of your flags, that's not what I did or what I suggested . linker flags to the _right_, source in the _middle_, compiler flags to the _left_ – user2485710 Mar 14 '14 at 09:55
  • @Niels What you do is compiling a source file to generate a object file (`gcc -c`), but your undefined-symbol message in the original post are linking errors. – Lee Duhem Mar 14 '14 at 10:05
  • @user2485710: I changed my built command to this output: gcc -std=c99 -fno-builtin -O2 -Wall -pedantic -c -fPIC -o adl-main.o -MMD adl-main.c -lm. Doesn that makes sense according to your previous answer on arranging compiler flags? – Niels Mar 14 '14 at 10:18
  • @Niels `-lm` does not make sense in `gcc -c ...`, although it will not cause error. – Lee Duhem Mar 14 '14 at 10:25
  • So how can I make it work because compiling without '-c' got me a bunch of other undefined reference errors – Niels Mar 14 '14 at 10:41