An attempt to use / enable / compile a third-party libmagic library. C / C ++ file type detection

After finding a way to determine the file stream file type, I found that the unix command uses libmagic, and I'm trying to use the library myself, but I cannot get it to work. I rarely integrated third-party code into my own, so probably most of my problem as well.

Why:

I do this because I have a portable application for viewing a graphic image, which will have to determine the types of archive files (rar, zip, more?) From a given file name, and then the types of image files inside. I hope I can use libmagic for Windows and Linux (and Mac), so if that is not the case, stop me now b / c I need to find something else.

Attempt

I found someone doing something similar , but I canโ€™t keep track of what they are doing, and I have no idea how to compile / run anything at all to start a mess.

My first instinct was to do something like:

// fileTypeTest.cpp, placed in file-5.03/src/ (source from link above) #include <stdio.h> #include "magic.h" int main() { magic_t myt = magic_open(MAGIC_CONTINUE|MAGIC_ERROR/*|MAGIC_DEBUG*/|MAGIC_MIME); magic_load(myt,NULL); printf("magic output: '%s'\n",magic_file(myt,__FILE__)); magic_close(myt); return 0; } 

then compile something like:

 $ gcc magic.c -o magic.o $ g++ fileTypeTest.cpp -o fileTypeTest magic.o 

which (obviously?) doesn't work. I donโ€™t even know where to start, what questions to ask, or if this is the right direction to solve my initial problem in the first place.


Edit: now I have

 #include <stdio.h> #include <magic.h> int main(int argc, char* argv[]) { if (argc != 2) { printf("bad arguments"); return 0; } magic_t myt = magic_open(MAGIC_CONTINUE|MAGIC_ERROR/*|MAGIC_DEBUG*/|MAGIC_MIME); magic_load(myt,NULL); printf("magic output: '%s'\n", magic_file(myt, argv[1])); magic_close(myt); return 0; } 

compilation with:

 $ g++ -L/usr/lib -libmagic fileTypeTest.cpp -o fileTypeTest 

working. I had to go to synaptic and install libmagic-dev. I will need to check if I can just copy the /usr/lib/libmagic.a file to the source directory when compiling my application in Windows MingW, but that will be on a different issue later, I suppose.

+3
c ++ c include file-type
source share
3 answers

__FILE__ is a reserved preprocessing character macro used for debugging / logging purposes. Consider this as an example:

 // This file is called test.c
 char * p = NULL;
 if (! (p = malloc ((1 * sizeof (char) + 1)))) {
    printf ("Error in file:% s @ line% d \ n \ tMalloc failed \ n", __FILE__, __LINE__);
    exit (-1);
 }

If the malloc call failed, you will see the output in the above example as follows:

 Error in file: test.c @ line 23
        Malloc failed

Notice how the code loads the source code. The above example illustrates the use of this.

I think your code should be something like this:

 // fileTypeTest.cpp, placed in file-5.03 / src / (source from link above)
 #include <stdio.h>
 #include "magic.h"
 int main (int argc, char ** argv) {
   if (argc> 1) {
      magic_t myt = magic_open (MAGIC_CONTINUE | MAGIC_ERROR / * | MAGIC_DEBUG * / | MAGIC_MIME);
      magic_load (myt, NULL);
      printf ("magic output: '% s' \ n", magic_file (myt, argv [1]));
      magic_close (myt);
   }
   return 0;
 }

The above code checks to see if there is a parameter that is passed to this program, and the parameter will be the file name, i.e. argv[0] indicates the executable name (compiled binary), argv[1] points to an array of characters (string) indicating the name of the file in question.

Compile it:

  g ++ -I / usr / include -L / usr / lib / libmagic.so fileTestType.cpp -o fileTestType
 g ++ -L / usr / lib -lmagic fileTestType.cpp -o fileTestType

Edit: Thanks to Alok for pointing out the error here ...

If you do not know where libmagic is located, find it in the / usr / local / lib and / usr / local / include directories - it depends on your installation.

See this to find predefined macros here .

Hope this helps, Regards, Tom.

+1
source share

I do not know why you think the above โ€œobviouslyโ€ does not work. See How to mix C and C ++ in the C ++ FAQ for details.

It seems that magic.h has corresponding extern "C" { } enclosures. So compiling your code with g++ should work well. You can #include <magic.h> in your .cpp file and use all libmagic functions.

  • Is there a specific error you get?
  • I have not tested the use of libmagic functions.
  • You need to link to libmagic. The g++ command requires -lmagic .
  • Since magic.h is most likely in the standard location, you should use #include <magic.h> .

Let us know why you need your mistake for more specific help.

0
source share

Where is magic.h in the file system? Does the preprocessor find this? If not, use -I<path> .

0
source share

All Articles