How to fix ext4magic src/recover.c:478:(.text+0x847): undefined reference to 'makedev'

Problem

When trying to compile ext4magic on modern Linux version, you’ll see an error like this:

gcc -O2 -g -D_FILE_OFFSET_BITS=64 -g -O2 -o ext4magic ext4magic-block.o ext4magic-dir_list.o ext4magic-ext4magic.o ext4magic-extent_db.o ext4magic-file_type.o ext4magic-hard_link_stack.o ext4magic-imap_search.o ext4magic-inode.o ext4magic-journal.o ext4magic-lookup_local.o ext4magic-magic_block_scan.o ext4magic-recover.o ext4magic-ring_buf.o ext4magic-util.o  -le2p -luuid -lblkid -lz -lbz2 -lmagic -lext2fs
/usr/bin/ld: ext4magic-recover.o: in function `recover_file':
/mnt/nas/ext4magic-0.3.2/src/recover.c:478:(.text+0x847): undefined reference to `makedev'
collect2: error: ld returned 1 exit status

Solution

The makedev function is part of the sys/sysmacros.h header file, which is not included in the ext4magic source code.

In order to fix this, you need to add the following line to the src/recover.c file:

#include <sys/sysmacros.h>

Or, to make it easier for you, just replace a single line in the recover.c file using the following shell script

sed -i -e 's^//header  util.h^#include <sys/sysmacros.h>^' src/recover.c

Now, retry compiling using make and the error should be gone.