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.
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.
Invalid arguments to
strcat()
: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:
The call to
fopen()
might fail for around 45 different reasons. Your code should check its return value. It returnsNULL
to signal failure.Similarly, you do not check the result of
malloc()
.Memory leak:
I do not understand the point of:
This overwrites the value of
userInput
. You have now lost all access to the original memory and have no way tofree()
it. (Have leaked memory)Double
free()
:You already
free()
duserInput
inshowPhoto
, so why are trying tofree()
it again inmain()
? 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:
– @pmacfarlane