0

I've been trying to use magic.h and magiclib in my program to help with file type identification. However when I try compiling the program it states magic_open, MAGIC_MIME and MAGIC_CHECK are undeclared causing an error.

I've tried the following command (magicTest.c being my program)

gcc magicTest.c -o magicTest -lmagic

without success

#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include </usr/include/linux/magic.h>

int main(){
   struct magic_set *magic = magic_open(MAGIC_MIME|MAGIC_CHECK);
   magic_load(magic, NULL);
   printf("magic output: '%s'\n", magic_file(magic, "myText"));

   return 0;
}

Below is the actual errors and output i'm receiving

magicTest.c: In function ‘main’:
magicTest.c:7:30: warning: implicit declaration of function ‘magic_open’ [-Wimplicit-function-declaration]
    struct magic_set *magic = magic_open(MAGIC_MIME|MAGIC_CHECK);
                              ^
magicTest.c:7:41: error: ‘MAGIC_MIME’ undeclared (first use in this function)
    struct magic_set *magic = magic_open(MAGIC_MIME|MAGIC_CHECK);
                                         ^
magicTest.c:7:41: note: each undeclared identifier is reported only once for each function it appears in
magicTest.c:7:52: error: ‘MAGIC_CHECK’ undeclared (first use in this function)
    struct magic_set *magic = magic_open(MAGIC_MIME|MAGIC_CHECK);
                                                    ^
magicTest.c:8:4: warning: implicit declaration of function ‘magic_load’ [-Wimplicit-function-declaration]
    magic_load(magic, NULL);
    ^
magicTest.c:9:35: warning: implicit declaration of function ‘magic_file’ [-Wimplicit-function-declaration]
    printf("magic output: '%s'\n", magic_file(magic, "myText"));
                                   ^
magicTest.c:9:11: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
    printf("magic output: '%s'\n", magic_file(magic, "myText"));

Any help would be greatly appreciated!

EDIT

after running the command as jww suggested, shown below:

find /usr -name magic.h 2>/dev/null

My output is:

/usr/arm-linux-gnueabi/include/linux/magic.h
/usr/arm-linux-gnueabihf/include/linux/magic.h
/usr/include/magic.h
/usr/include/linux/magic.h
/usr/include/ImageMagick-6/magick/magic.h
/usr/src/linux-headers-4.13.0-39/include/uapi/linux/magic.h
/usr/src/linux-headers-4.13.0-37/include/uapi/linux/magic.h
/usr/src/linux-headers-4.13.0-38/include/uapi/linux/magic.h
/usr/src/linux-headers-4.13.0-32/include/uapi/linux/magic.h
/usr/src/linux-headers-4.13.0-41/include/uapi/linux/magic.h
/usr/src/linux-headers-4.13.0-36/include/uapi/linux/magic.h

Thanks again!

Kevag6
  • 135
  • 1
  • 1
  • 10
  • 2
    `#include ` --> `#include ` – Christian Gibbons Aug 29 '19 at 21:06
  • I'm still encountering the same issue, even after making this change to my code. Thanks for the reply! – Kevag6 Aug 30 '19 at 00:34
  • 1
    It looks like you are missing a header. Is `libmagic-dev` package (and possibly `libmagickcore-dev`) installed? – jww Aug 30 '19 at 01:52
  • I'll double check to make sure. Thanks for the feedback – Kevag6 Aug 30 '19 at 01:57
  • I've done a sudo apt-get install on both packages and still the issue resides. could I be linking the libraries wrong? thanks – Kevag6 Aug 30 '19 at 02:11
  • Perform a `find /usr -name magic.h 2>/dev/null` and report back with the results. You can add the additional information to your question by clicking *Edit* in the lower left-hand corner of the question, and then making the changes. – jww Aug 30 '19 at 03:44
  • 1
    Sorry, missed this: *"could I be linking the libraries wrong"*. You are experiencing a compiler error, not a linker error. The compiler cannot find [`magic.h`](https://www.daemon-systems.org/man/magic_open.3.html) (assuming the header has `magic_open` and firends). Eventually you will need to add a `-I` option for the compiler. You may need a linker option too, but you are not that far along (yet). – jww Aug 30 '19 at 03:48
  • Actually, looking at the `linux/magic.h` on my system, it doesn't define any functions at all. It just has a bunch of macro values. Is the `/usr/include/magic.h` different from `/usr/include/linux/magic.h`? I don't have the one in `/usr/include`. – Christian Gibbons Aug 30 '19 at 14:29

0 Answers0