0

I'm trying to use powf function in an rtems application. When I call powf(a,b); inside Init() function, it compiles ok. But when I call powf in some other function, the compiler gives me 'undefined reference to powf' message even though I have those #include <math.h> and #include <float.h>. I event tried merging the file, but it is the same.

#define CONFIGURE_...
#define CONFIGURE_...
#include <rtems/confdefs.h>

rtems_task Init( rtems_task_argument ignored) 
{


powf(a,b); // ok

}

int my_other_func()
{

powf(c,d); // undefined reference error..

}

What can be the problem?

EDIT(ADD) : I added source code and makefile below. The compiled rtems OS package is specified by shell environment variable RTEMS_MAKEFILE_PATH.

Makefile :

include ../Makefile.base
_RAM_START = 0x60000000
XCFLAGS = -qnolinkcmds -T ../../lib/linkcmds.abts3 -D_RAM_START=$(_RAM_START)
XCFLAGS += -lm -DALDEBARAN_RTEMS

../Makefile.base :

#
#  RTEMS_MAKEFILE_PATH is typically set in an environment variable
#

PGM=${ARCH}/faster_rcnn.exe

# optional managers required
MANAGERS=all

# C source names

VPATH = ../src
VPATH += ../../../../abfrcnn/bare-c/lrn_layer

CSRCS = init.c
CSRCS += lrn_layer.c

CSRCS1 = $(notdir $(CSRCS))
COBJS_ = $(CSRCS1:.c=.o)

include $(RTEMS_MAKEFILE_PATH)/Makefile.inc
include $(RTEMS_CUSTOM)
include $(PROJECT_ROOT)/make/leaf.cfg
#XCFLAGS += -I../../include
COBJS = $(COBJS_:%=${ARCH}/%)

OBJS= $(COBJS) $(CXXOBJS) $(ASOBJS)

#all: ${ARCH} $(PGM) RUNTCL
all: ${ARCH} $(PGM)

$(PGM): $(OBJS)
    $(make-exe)

RUNTCL:
    echo 'system_init' > run.tcl
    echo 'load_image o-optimize/faster_rcnn.exe' >> run.tcl
    echo 'run $(_RAM_START)' >> run.tcl

clean:
    -$(RM) -r $(ARCH)

../src/init.c :

...
#include <math.h>

rtems_status Init(rtems_argument ignored)
{    
...
    printf(" pow(1.1,2.2) = %f\n", powf(1.1,2.2)); // <== powf compiles liks ok
    //zf_coco();

}

../../../../abfrcnn/bare-c/lrn_layer/lrn_layer.c

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

int lrn_layer(... args... )
{

 ...
            val = 1./powf((1.+a/(float)(k^2)*tmp),b); // method1
 ...
} // main ROI loop

The result of make command :

test -d o-optimize || mkdir o-optimize
sparc-ab-rtems-gcc --pipe -B/home/ckim/prj/abts/rtems-qt/rtems-4.10.99-kernel/build-rtems/rtems-package/sparc-ab-rtems/aldebaran2/lib/ -specs bsp_specs -qrtems   -Wall -qnolinkcmds -T ../../lib/linkcmds.abts3 -D_RAM_START=0x60000000 -lm -DALDEBARAN_RTEMS -O4    -mtune=v8 -msoft-float -fcommon -DTARGET_ALDEBARAN       -c   -o o-optimize/init.o ../src/init.c
../src/init.c:120:2: warning: missing braces around initializer [-Wmissing-braces]
  {0}, //   rtems_chain_control;
  ^
../src/init.c:120:2: warning: (near initialization for 'ald_sd_card_driver_table[0].queue.Chain') [-Wmissing-braces]
sparc-ab-rtems-gcc --pipe -B/home/ckim/prj/abts/rtems-qt/rtems-4.10.99-kernel/build-rtems/rtems-package/sparc-ab-rtems/aldebaran2/lib/ -specs bsp_specs -qrtems   -Wall -qnolinkcmds -T ../../lib/linkcmds.abts3 -D_RAM_START=0x60000000 -lm -DALDEBARAN_RTEMS -O4    -mtune=v8 -msoft-float -fcommon -DTARGET_ALDEBARAN       -c   -o o-optimize/lrn_layer.o ../../../../abfrcnn/bare-c/lrn_layer/lrn_layer.c
../../../../abfrcnn/bare-c/lrn_layer/lrn_layer.c: In function 'lrn_layer':
../../../../abfrcnn/bare-c/lrn_layer/lrn_layer.c:201:12: warning: 'w_idx' may be used uninitialized in this function [-Wmaybe-uninitialized]
  if (w_idx == w && h_idx == h) {
            ^
sparc-ab-rtems-gcc --pipe -B/home/ckim/prj/abts/rtems-qt/rtems-4.10.99-kernel/build-rtems/rtems-package/sparc-ab-rtems/aldebaran2/lib/ -specs bsp_specs -qrtems   -Wall -qnolinkcmds -T ../../lib/linkcmds.abts3 -D_RAM_START=0x60000000 -lm -DALDEBARAN_RTEMS -O4    -mtune=v8 -msoft-float -fcommon -DTARGET_ALDEBARAN      -L/opt/abde-rtems/lib/gcc/sparc-ab-rtems/4.8.2/soft -L/opt/abde-rtems/sparc-ab-rtems/lib/soft   -mtune=v8 -msoft-float -fcommon -DTARGET_ALDEBARAN   -o o-optimize/faster_rcnn.exe  o-optimize/init.o o-optimize/lrn_layer.o       
o-optimize/lrn_layer.o: In function `lrn_layer':
lrn_layer.c:(.text+0x4a8): undefined reference to `powf'
collect2: error: ld returned 1 exit status
make: *** [o-optimize/faster_rcnn.exe] Error 1
Chan Kim
  • 5,177
  • 12
  • 57
  • 112
  • 1
    "Undefined reference to ..." sounds like a linking problem. Do you have a `-lm` linker directive to include the math library? – Bo Persson Nov 12 '16 at 10:38
  • Please show a complete code example instead of a fraction of the code. There is no way the described problem can occur given the posted code. – Support Ukraine Nov 12 '16 at 11:03
  • @BoPersson Yes, I have it. The Makefile is set to use -lm for both files. – Chan Kim Nov 12 '16 at 14:02
  • @4386427 This is too complex to upload. I'm building an application on rtems os build and the Makefiles are including scripts in the OS build image. – Chan Kim Nov 12 '16 at 14:04
  • @4386427 I added source codes and the Makefile and the error message from make. Hope someone well versed could find my problem. – Chan Kim Nov 12 '16 at 14:18
  • Have you tried to comment out all `powf` in `lrn_layer.c` ? Can it compile in that case ? I have a feeling that it will fail in `init.c`. – Support Ukraine Nov 12 '16 at 14:48
  • Then init.c and lrn_layer.c are both compiled ok and I get o-optimized/faster_rcnn.ext which is the result of init.c (currently init.c doesn't call lrn_layer yet). – Chan Kim Nov 12 '16 at 14:52
  • The compile is OK for both files but the linker fails. Please try the following: Comment out **all** `powf` call in `lrn_layer.c` and compile/link the program. Does that work? – Support Ukraine Nov 12 '16 at 15:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127956/discussion-between-chan-kim-and-4386427). – Chan Kim Nov 12 '16 at 15:13

1 Answers1

0

The compile process is composed of compilation of each individual .c files and final linking. I found somehow the -lm option which was added through XCFLAGS is being applied to the compilation command but not in the linking command. So I added -lm option in the object list so that that option is naturally following the object list in the link command. (I guess the proper way is to add -lm to the XLDFLAG because I found the variable in rtems build tree : extra LD flags). I'll try later..

OBJS= $(COBJS) $(CXXOBJS) $(ASOBJS) -lm

Chan Kim
  • 5,177
  • 12
  • 57
  • 112