Home > Blockchain >  Cannot include libexif in C on Kali linux
Cannot include libexif in C on Kali linux

Time:01-30

I'm trying to include libexif to my project in C, I add whole libexif folder to my project folder, then used ./configure and then make command as README told me to do so. Still I'm getting warnings while I try to compile my programm. Warining: In file included from main.c:5: libexif-0.6.24/libexif/exif-data.h:31:10: fatal error: libexif/exif-byte-order.h: Nie ma takiego pliku ani katalogu 31 | #include <libexif/exif-byte-order.h> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~ compilation terminated.

Photo of my project folder

And my code:

#include<stdlib.h>
#include<stdio.h>
#include<ncurses.h>
#include<string.h>
#include"libexif-0.6.24/libexif/exif-data.h"
//#include"DevIL/include/IL/il.h"
//Function that allow user to open folder where he want to do something with photos. It returns pointer to file with list of all files that are in folder pointed by user
FILE *openFolder(char* destination){
    char c;
    char *systemInput=malloc(sizeof(char)*123);
    strcat(systemInput,"ls ");
    strcat(systemInput,destination);
    strcat(systemInput," > systemOutput.txt");
    system(systemInput);
    FILE *file=fopen("systemOutput.txt","r");
    free(systemInput);
    return file;
}
//Function that allows to show photo
void showPhoto(char* userInput, char* destination){
    char *systemInput=malloc(sizeof(char)*150);
    strcat(systemInput,"open ");
    strcat(systemInput,destination);
    strcat(systemInput,userInput);
    system(systemInput);
    free(systemInput);
    free(userInput);
    return;
}
int main(){
    //defining variables
    char* userInput=malloc(sizeof(char)*100);
    char* destination=malloc(sizeof(char)*100);
    char c;
    //opening ncurses window
    initscr();
    refresh();
    //asking user for folder path which contain photos
    printw("EXIF file reader\n\n");
    printw("Please enter the folder path where the photos are located:\n");
    scanw("%s", destination);
    FILE *file=openFolder(destination);
    while(1){
        clear();
        printw("EXIF file reader\n\nType numer of action that you want to perform\n\n");
        printw("1. Open photo\n");
        printw("\n\n\n\n\n\n\n\n                Press q to exit programm");
        c=getch();
        if(c=='1'){
            clear();
            userInput=malloc(sizeof(char)*100);
            printw("EXIF file reader\n\n");
            if(file){
                while((c=getc(file))!=EOF){
                    printw("%c", c);
                }
            }
            printw("\n\n");
            printw("Please enter photo that you want to open:\n");
            scanw("%s", userInput);
            showPhoto(userInput,destination);
        }
        else if(c=='q'){
            free(destination);
            free(userInput);
            endwin();
        }
    }
}

I'm sorry if this is some trivial problem, but I'm new to C and using external libraries.

CodePudding user response:

Invalid arguments to strcat():

char *systemInput=malloc(sizeof(char)*123);
strcat(systemInput,"ls ");

The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte.

But the contents of systemInput are uninitialized. So this invokes undefined behaviour.

But you do not need malloc() here, just allocate an array of fixed size.

Ignoring the return value of library functions:

FILE *file=fopen("systemOutput.txt","r");

The call to fopen() might fail for around 45 different reasons. Your code should check its return value. It returns NULL to signal failure.

Similarly, you do not check the result of malloc().

Memory leak:

I do not understand the point of:

userInput=malloc(sizeof(char)*100);

This overwrites the value of userInput. You have now lost all access to the original memory and have no way to free() it. (Have leaked memory)

Double free():

You already free()d userInput in showPhoto, so why are trying to free() it again in main()? It will corrupt the heap and result in undefined behaviour (yes, again).

That being said, you have to specify some compiler options and link with the library, as this comment mentions:

I think you'll need to add something like -Llibexif-0.6.24 to your compiler options (assuming gcc, might be different for others) so it can pick up other exif-related header files. Do this in your Makefile (in CFLAGS), or in your CMakeLists.txt, or whatever is appropriate for your build system.

– @pmacfarlane

  • Related