Saltar al contenido ForoHackers

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

Sistemas de compilaciónCToolchains y enlazadores
#16390 · · Citar · Denunciar

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 · · Citar · Denunciar

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 · · Citar · Denunciar

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 · · Citar · Denunciar

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 · · Citar · Denunciar

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 · · Citar · Denunciar

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

#16693 · · Citar · Denunciar

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

#16711 · · Citar · Denunciar

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.

No has iniciado sesión, así que esto se publica como anónimo: no se guarda con ello ninguna cuenta, nombre ni dirección, y no puede llevar enlaces ni imágenes. Inicia sesión o únete para publicar con tu nombre. Los registros de nuestro servidor web guardan la dirección y la hora de cada petición hasta 14 días; para que la tuya no quede en ellos, usa Tor. Para escribir al equipo de forma confidencial, usa el buzón. Firmar como fuente habitual (una clave guardada solo en este navegador)

Enviar tarda unos segundos aquí: tu navegador resuelve antes una prueba de trabajo contra el spam.

Responder