Shop OBEX P1 Docs P2 Docs Learn Events
"no float in printf: link with -lm" — Parallax Forums

"no float in printf: link with -lm"

amossamamossam Posts: 35
edited 2012-08-15 04:14 in Propeller 1
Hey

I'm using libtiny, and getting this:
    "no float in printf: link with -lm"
from propeller when i use this line:
    printf("X %f ", strtof(val));

function strtof return value in type double.

I tried with -lm in compiler line, to build my program, i tried rebuilding libtiny with that argument, but no difference.
I even tried to remove libtiny, and use stdio.h, but no help...

any hints? and yes, I need printf with float, at least for now...

thx

Comments

  • jazzedjazzed Posts: 11,803
    edited 2012-08-12 13:27
    Hi. Sorry, but there is no floating support in libtiny. That's one reason it's tiny.
  • amossamamossam Posts: 35
    edited 2012-08-12 13:38
    jazzed wrote: »
    Hi. Sorry, but there is no floating support in libtiny. That's one reason it's tiny.

    thx for fast reply...

    i suspected that, so I removed libtiny, and replaced tinyio.h with stdio.h, but no difference!? and I triple checked that there is no reference to libtiny in my project...
  • jazzedjazzed Posts: 11,803
    edited 2012-08-12 14:38
    amossam wrote: »
    thx for fast reply...

    i suspected that, so I removed libtiny, and replaced tinyio.h with stdio.h, but no difference!? and I triple checked that there is no reference to libtiny in my project...

    The header file does not determine the library being linked. Probably you have libtiny.a linked somewhere either via project manager or other linker options. You can check the build status. Right-click select-all, copy, paste here if you are not sure.
  • amossamamossam Posts: 35
    edited 2012-08-13 00:44
    jazzed wrote: »
    The header file does not determine the library being linked. Probably you have libtiny.a linked somewhere either via project manager or other linker options. You can check the build status. Right-click select-all, copy, paste here if you are not sure.

    Well, I don't use SimpleIDE, but Eclipse with my Makefile...
    and I did 'find . | xargs grep tiny' in my project folder, and that came out empty...
    Bu anyway, here is my Makefile...
    CC            = /opt/Development/parallax/parallax/bin/propeller-elf-gcc
    CFLAGS         = -Os -mlmm -I. -I /opt/Development/parallax/parallax/propeller-elf/include -Wall -fno-exceptions
    NM             = propeller-elf-nm
    
    SRC            =    main.cpp stepper.cpp gcode.cpp
    OBJ            =    $(SRC:.cpp=.o)
    EXE            =    main
    
    all: $(SRC) $(EXE)
    
    $(EXE):    $(OBJ)
        @(cd build; $(CC) $(LDFLAGS) $(CFLAGS) $(OBJ) -o $@)
    
    %.o:    %.cpp
        @(mkdir -p build)
        @(cd build; $(CC) $(CFLAGS) -c ../$< -o $@)
    
    clean:
        @(rm -rf build)
    

    and this is verbose output of make
    (mkdir -p build)
    (mkdir -p build)
    (cd build; /opt/Development/parallax/parallax/bin/propeller-elf-gcc -Os -mlmm -I. -I /opt/Development/parallax/parallax/propeller-elf/include -Wall -fno-exceptions -c ../main.cpp -o main.o)
    (cd build; /opt/Development/parallax/parallax/bin/propeller-elf-gcc -Os -mlmm -I. -I /opt/Development/parallax/parallax/propeller-elf/include -Wall -fno-exceptions -c ../stepper.cpp -o stepper.o)
    (mkdir -p build)
    (cd build; /opt/Development/parallax/parallax/bin/propeller-elf-gcc -Os -mlmm -I. -I /opt/Development/parallax/parallax/propeller-elf/include -Wall -fno-exceptions -c ../gcode.cpp -o gcode.o)
    ../gcode.cpp: In member function 'bool GCode::executeG(char*, char*)':
    ../gcode.cpp:112:12: warning: unused variable 'cmd' [-Wunused-variable]
    (cd build; /opt/Development/parallax/parallax/bin/propeller-elf-gcc  -Os -mlmm -I. -I /opt/Development/parallax/parallax/propeller-elf/include -Wall -fno-exceptions main.o stepper.o gcode.o -o main)
    

    i even tried to compile it manually from terminal, with almost no flags to gcc, and still the same...
  • David BetzDavid Betz Posts: 14,516
    edited 2012-08-13 04:43
    I don't see any -lm on your linker command line.
  • amossamamossam Posts: 35
    edited 2012-08-13 05:31
    David Betz wrote: »
    I don't see any -lm on your linker command line.

    this is probably then from last night... :blank: i tried zillion combinations...

    CFLAGS states like this:
    CFLAGS         = -Os -mlmm -lm -I. -I /opt/Development/parallax/parallax/propeller-elf/include -Wall -fno-exceptions 
    

    and curious thing, with or without -lm flag, resulting filesize that is uploaded to propeller is same to a byte!!

    when i come home after work, i'll try to create a simplest program using printf with %f and then will see...

    thx
  • ersmithersmith Posts: 6,096
    edited 2012-08-13 05:59
    The -lm argument, and any similar library option, must be passed *last* on the linker command line, after all object files. The reason is that the linker processes files in the order they appear on the command line, so if a library comes first nothing in it will be included (since library files are only included if they are needed by something else, and at that point in the link nothing has called any library functions yet!).
  • TorTor Posts: 2,010
    edited 2012-08-13 06:25
    As ersmith said.
    -lm shouldn't be in CFLAGS at all, it's a linker stage option, not a compile option. And CFLAGS shouldn't be part of the linking stage.
    $(EXE):    $(OBJ)
        @(cd build; $(CC) $(LDFLAGS) $(CFLAGS) $(OBJ) -o $@)
    
    should be changed to something like this:
    LIBS := -lm
    $(EXE):    $(OBJ)
        @(cd build; $(CC) $(LDFLAGS) $(OBJ) -o $@ $(LIBS) )
    

    -Tor
  • amossamamossam Posts: 35
    edited 2012-08-13 06:30
    ersmith wrote: »
    The -lm argument, and any similar library option, must be passed *last* on the linker command line, after all object files. The reason is that the linker processes files in the order they appear on the command line, so if a library comes first nothing in it will be included (since library files are only included if they are needed by something else, and at that point in the link nothing has called any library functions yet!).

    ok, it seems that that is the point! now, when i put -lm just before -o (last parameter) it gives me following error:
    /opt/Development/parallax/parallax/bin/../lib/gcc/propeller-elf/4.6.1/../../../../propeller-elf/bin/ld: main section `.text' will not fit in region `hub'
    /opt/Development/parallax/parallax/bin/../lib/gcc/propeller-elf/4.6.1/../../../../propeller-elf/bin/ld: region `hub' overflowed by 7032 bytes
    collect2: ld returned 1 exit status
    

    so, i suppose that now math library gets included, but it won't fit.... but that is some other problem! :cool: (maybe XMMC memory model?)

    thx for help
  • jazzedjazzed Posts: 11,803
    edited 2012-08-14 13:14
    amossam wrote: »
    ok, it seems that that is the point! now, when i put -lm just before -o (last parameter) it gives me following error:
    /opt/Development/parallax/parallax/bin/../lib/gcc/propeller-elf/4.6.1/../../../../propeller-elf/bin/ld: main section `.text' will not fit in region `hub'
    /opt/Development/parallax/parallax/bin/../lib/gcc/propeller-elf/4.6.1/../../../../propeller-elf/bin/ld: region `hub' overflowed by 7032 bytes
    collect2: ld returned 1 exit status
    

    so, i suppose that now math library gets included, but it won't fit.... but that is some other problem! :cool: (maybe XMMC memory model?)

    thx for help

    Try using 32bit doubles and/or simple printf.

    We have discussed ways to reduce code size for a while and are on the verge of adding it.
    For the time being XMMC is a fair option. It works with 64KB EEPROM, but it is slower than LMM.
  • amossamamossam Posts: 35
    edited 2012-08-14 16:19
    jazzed wrote: »
    Try using 32bit doubles and/or simple printf.

    We have discussed ways to reduce code size for a while and are on the verge of adding it.
    For the time being XMMC is a fair option. It works with 64KB EEPROM, but it is slower than LMM.

    yea, I found that and on propgcc wiki page I found some explanations, so now I'm using this:
    CFLAGS         = -Os  -mlmm -mfcache -Wall -fno-exceptions -fno-rtti -I. -I /opt/Development/parallax/parallax/propeller-elf/include -Dprintf=__simple_printf -Xlinker -s
    

    and with help of __simple_printf it still fits in LMM. :-D

    I still haven't tried 32bit doubles, but I did noticed one thing...

    i have some decimal numbers stored in char*. I tried using atof function, but it puts on 5th or 6th decimal place sometimes instead of 0 some random number, but if I use double, that doesn't happen!
    i don't know is that normal...

    since I'm parsing that char* anyway, i added code that if some chars are numbers it converts them to decimal number. and if I use float now, it's storing numbers correctly. also, it seems that my way is faster than atof (at least what I can see!)! :-D
  • jazzedjazzed Posts: 11,803
    edited 2012-08-14 20:24
    amossam wrote: »
    I still haven't tried 32bit doubles, but I did noticed one thing...

    i have some decimal numbers stored in char*. I tried using atof function, but it puts on 5th or 6th decimal place sometimes instead of 0 some random number, but if I use double, that doesn't happen!
    i don't know is that normal...

    Can you provide an example? Did you try strtod instead of atof by chance?

    Glad you have an alternative you like.
  • amossamamossam Posts: 35
    edited 2012-08-15 04:14
    jazzed wrote: »
    Can you provide an example? Did you try strtod instead of atof by chance?

    Glad you have an alternative you like.

    well, I created an example program and data, but it seems that main problem was my misunderstanding of float type capacity/accuracy, so it wasn't atof fault (but it is slower! :cool:) ... :innocent:
    strtod gives same result, because of float type capacity...

    my apologies, I haven't studied enough what is the problem, instead I jumped to first suspect! :blank:

    anyway, here is code
    #include <propeller.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    {
        char* chr;
        while (1) 
        {
            printf("Input: ");
            gets(chr);
            printf("Output: %f\n\n", atof(chr));
        }
        return 0;
    }
    

    and here is data:
    Input: 12.12
    Output: 12.120000
    
    Input: 123.123
    Output: 123.123000
    
    Input: 123456.12
    Output: 123456.120000
    
    Input: 12345678.12
    Output: 12345678.120000
    
    Input: 1234567890.12
    Output: 1234567890.120000
    
    Input: 12345678901.12
    Output: 12345678901.120001
    
    Input: 12345678901.123
    Output: 12345678901.122999
    
Sign In or Register to comment.