Skip to main content



      Home
Home » Language IDEs » C / C++ IDE (CDT) » undefined reference to (any function in libm.a)(-lx flags should be after inputs)
undefined reference to (any function in libm.a) [message #1718549] Sat, 26 December 2015 21:02 Go to next message
Eclipse UserFriend
Hi all
Working with Eclipse IDE for C/C++ Developers
Version Mars.1 Release(4.5.1)

Using arm's gcc cross compiler tools in Windows 7 and CDT's internal builder.

For the compiler I use gcc (prefixed with arm-none-eabi-) with the -c option to produce all the object files (*.o)
For the linker I use gcc as well with the appropriate -Wl, flag where needed.

The compiler pass went without incident but the linker pass would seemingly not link to libm.a

I kept getting "undefined reference to 'somemathfunc' in the console output. No matter where I placed -lm in the linker flags text box I still got the same error.

It finally dawned on me, after pouring over console output messages that -lm was always placed before the input files

The only way I found to get -lm after the input object files was to alter the "Command line pattern" from the default:
${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}
to:
${COMMAND} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} ${FLAGS}

Surely I'm not the first one to try and link with libm.a or any other "none-standard" library?
Is there any other way of doing this as I fear placing all flag options at the end of the command line might bite me later on. I only want the -l option after the input object files, especially since after searching and searching I could not find anybody else on the net with a similar issue
Re: undefined reference to (any function in libm.a) [message #1718585 is a reply to message #1718549] Sun, 27 December 2015 15:38 Go to previous messageGo to next message
Eclipse UserFriend
When I do it (although not using arm), it's placed at the end of the command.

Build log:
Invoking: GCC C++ Linker
g++ -o "HelloWorldC++" ./src/HelloWorldC++.o -lm

I had set the toolchain to use libm using the Project ==> Properties ==> C/C++ Build ==> Settings linker Libraries dialog.
The command pattern is: ${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}

Because the values are transient, I later changed the command pattern to
${COMMAND} F=${FLAGS} OF=${OUTPUT_FLAG} O=${OUTPUT_PREFIX}${OUTPUT} I=${INPUTS}
and got:
g++ F= OF=-o O="HelloWorldC++" I= ./src/HelloWorldC++.o -lm

Note that -lm is in ${INPUTS}
${INPUTS}: ./src/HelloWorldC++.o -lm
${FLAGS): empty

It's possible these vary when using the cross compiler tool chain. In general, the library must also have been cross compiled for the target. So it may be that no one noticed before. However, it seems unlikely the tool chains differ in setting variables.

You could test this by putting id info before each variable as I have done. To avoid error messages change the command to "echo g++", or rather, the arm equivalent.

EDIT:
I reread your original post. It's not particularly clear but I would guess you used the linker settings dialog found under Miscellaneous instead of Libraries. IIRC, the Miscellaneous settings are placed in ${FLAGS}. It may be the source of your problem.

[Updated on: Sun, 27 December 2015 16:23] by Moderator

Re: undefined reference to (any function in libm.a) [message #1718590 is a reply to message #1718585] Sun, 27 December 2015 19:36 Go to previous message
Eclipse UserFriend
David Vavra wrote on Sun, 27 December 2015 15:38
...but I would guess you used the linker settings dialog found under Miscellaneous instead of Libraries. IIRC, the Miscellaneous settings are placed in ${FLAGS}. It may be the source of your problem.

Thanks for the reply David.
That's exactly the source of my problem. Under Linker->Miscellaneous the text box is clearly marked "Linker flags". It seems I couldn't see the tree for the woods!

I've added the -l in other projects in this "linker flags" text box before and as it compiled without issue I gave it no more thought.

Revisiting those projects I now see that I added standard libraries that the linker is configured to add automatically and that removing the -l option from the "Linker flags" text box in those projects still allows them to compile error free.

It escapes me how I missed that it's the libraries dialog where you add libraries Embarrassed
Previous Topic:Eclipse server?! (read it carefully :-)
Next Topic:Problem with arduino in eclipse
Goto Forum:
  


Current Time: Thu Nov 06 20:26:03 EST 2025

Powered by FUDForum. Page generated in 0.05998 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top