Skip to content

Commit 6376137

Browse files
committed
Added missing math library debuggging steps
1 parent ca76c88 commit 6376137

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

debugging/c_debugging.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,27 @@ Undefined reference
4040

4141
This is typically the result of an error during the linking phase. Make sure you are compiling all the C files you need (you can add more than one C file to your compile command).
4242

43+
### Undefined reference to 'sqrt' (or other math.h functions - floor, ceil, round, sin, tan, cos)
44+
45+
```(text)
46+
/usr/bin/ld: /tmp/ccm9wUlD.o: in function `main':
47+
mathstuff.c:(.text+0x26): undefined reference to `sqrt'
48+
/usr/bin/ld: mathstuff.c:(.text+0x1c9): undefined reference to `floor'
49+
/usr/bin/ld: mathstuff.c:(.text+0x24b): undefined reference to `ceil'
50+
/usr/bin/ld: mathstuff.c:(.text+0x373): undefined reference to `cos'
51+
/usr/bin/ld: mathstuff.c:(.text+0x3cf): undefined reference to `sin'
52+
/usr/bin/ld: mathstuff.c:(.text+0x42b): undefined reference to `tan'
53+
collect2: error: ld returned 1 exit status
54+
```
55+
56+
[VIDEO](https://youtu.be/oIwfFwmcAHQ)
57+
58+
This is happening because you are not linking with the math library, where the functions in `math.h` are defined. The fix here is to compile with the `-lm` flag, where the `-l` directs gcc to load a library and the `m` specifies the math library.
59+
60+
```(bash)
61+
gcc myfile.c --std=c99 -lm
62+
```
63+
4364
### C++ style comments not allowed
4465

4566
```(text)

0 commit comments

Comments
 (0)