Skip to the content ForoHackers
Log inJoin

undefined reference to sqrt, although we've been formally introduced in math.h

Build systemsCToolchains and linkers
#16390 · · Quote · Report

My little home league program estimates the length of a line across a practice grid. The C compiler accepts the source with math.h included. At link time, on my Linux machine, the linker says "undefined reference to" and names sqrt. I have spent a surprising amount of time rereading the introduction. The function and I are now on first-name terms and it still won't move in.

#16688 · · Quote · Report

La cabecera te da la declaración. Que el compilador conozca la firma no mete por sí solo la implementación en tu ejecutable. ¿Tienes un ejemplo que use el valor leído en tiempo de ejecución?

#16689 · · Quote · Report

Yes. I'm reading the value from my own little program. With a literal sqrt(9.0) the problem went away in one of my builds, which is how I discovered that a successful tiny test can be a deeply irritating little liar.

#16690 · · Quote · Report

On this Linux toolchain, try compiling the source to its object and linking with the object followed by -lm. Keep the runtime input in the example; a compiler can fold a constant call. Other platforms' library packaging can differ.

#16691 · · Quote · Report

Object on its own existed. Linking that object without the library got the error. Linking grid.o -lm produced an executable, and with 9 as the runtime value it printed 3. I was arguing with the wrong part of the build.

#16692 · · Quote · Report

I would like to publish the benchmark where sqrt takes no time, no library and by prior agreement is always three.

#16693 · · Quote · Report

eso ya lo vendía mi profe de mates con otro nombre. eran ejercicios resueltos.

#16711 · · Quote · Report

The Makefile now names the library at the link step. I'm keeping the two command lines in the issue because "I fixed the header" was what I'd written in the first version. Good grief.

You are not logged in, so this goes up anonymously: no account, no name and no address is stored with it, and it may carry no link and no image. Log in or join to post under your name. Our web server's logs keep the address and time of every request for up to 14 days; to keep yours out of them, use Tor. To reach the staff in confidence, use the drop. Sign as a returning source (a key kept only in this browser)

Sending takes a few seconds here: your browser first works out a proof of work against spam.

Reply