I'm currently in the phase of final development and testing of my computer engineering senior project. After doing the design and development of the code (C, Bash) with libs (Libjpeg, Libbmp, PocketSphinx, Libavcodec, Libavformat, Libavutil) and using netbeans as IDE. The problem I have is that in Netbeans the code compiles and links perfectly and the execution of the software is fine too. However, when I compile and link the code externally using a MakeFile, functions like: fopen
, fread
, fwrite
, fgets
, fscanf
, etc stop working...
GCC-FLAGS:
-m32 -O3 -W -Wall -std=gnu99 -pedantic -Wbad-function-cast -Wcast-align -Wcast-qual \
-Wchar-subscripts -Winline -Wmissing-prototypes -Wnested-externs -Wpointer-arith \
-Wredundant-decls -Wshadow -Wstrict-prototypes -Wwrite-strings -Wformat-nonliteral \
-Wformat-security -ftrapv -lrt -Wno-unused \
-DMODELDIR=\"`pkg-config --variable=modeldir pocketsphinx`\" \
`pkg-config --cflags --libs pocketsphinx sphinxbase` \
`pkg-config --cflags --libs sndfile`
LD-FLAGS:
-I/usr/local/lib -I/usr/local/include -I/usr/local/lib/pkgconfig -lpthread \
-lpocketsphinx -lsndfile -ljpeg -lavformat -lavcodec -ldl -lasound -lz -lswscale \
-lavutil -lm
The same unknown behavior is also affecting the performance of PocketSphinx which is now not able to open HMM.
Any enlightenment is really appreciated as my final presentation is next week.
---------- UPDATE ----------
This is my actual MakeFile
.SUFFIXES: .o .c
.c.o: $(CC) -c $(CFLAGS) $<
# Compiler and Flags
CC = gcc
CFLAGS = -m32 -O3 -W -Wall -std=gnu99 -pedantic -Wbad-function-cast -Wcast-align -Wcast-qual -Wchar-subscripts -Winline -Wmissing-prototypes -Wnested-externs -Wpointer-arith -Wredundant-decls -Wshadow -Wstrict-prototypes -Wwrite-strings -Wformat-nonliteral -Wformat-security -ftrapv -Wno-unused -DMODELDIR=\"`pkg-config --variable=modeldir pocketsphinx`\"`pkg-config --cflags --libs pocketsphinx sphinxbase` `pkg-config --cflags --libs sndfile`
# Libraries
LIBS = -I/usr/local/lib -I/usr/local/include -I/usr/local/lib/pkgconfig -lpocketsphinx -lsndfile -ljpeg -lavformat -lavcodec -ldl -lasound -lz -lswscale -lavutil -lm
# Source files
SRC= libbmp.c state.c secure.c audio.c config.c engine.c helpers.c macmp2.c queue.c image.c video.c
# Object Files
OBJ= libbmp.o state.o secure.o audio.o config.o engine.o helpers.o macmp2.o queue.o image.o video.o
# Executable file
EXECUTABLE = macmp2
# Explicit rule
hist: $(OBJ)
$(CC) $(CFLAGS) -o $(EXECUTABLE) $(OBJ) $(LIBS)
clean:
rm -f *.o
rm -f $(EXECUTABLE)
# Implicit rules
audio.o: macmp2.h libbmp.h audio.c
config.o: macmp2.h libbmp.h config.c
engine.o: macmp2.h libbmp.h engine.c
helpers.o: macmp2.h libbmp.h helpers.c
image.o: macmp2.h libbmp.h image.c
libbmp.o: libbmp.h libbmp.c
macmp2.o: macmp2.h libbmp.h macmp2.c
queue.o: macmp2.h libbmp.h queue.c
secure.o: macmp2.h libbmp.h secure.c
state.o: macmp2.h libbmp.h state.c
video.o: macmp2.h libbmp.h video.c
Compile-Time Errors: None Run-Time Errors: None
I first notice the issue in this part of the code.
void load_configuration (macmp2_state * s)
{
register uint32_t it = 0x0; /* Iterator */
register uint32_t size = get_file_size (s); /* Configuration File Size */
char * new_entry = (char *) malloc_safe (sizeof (char) * LINE_LENGTH * 5, s);
char * error_msg = (char *) malloc_safe (sizeof (char) * LINE_LENGTH * 5, s);
char * filename = NULL; /* Filename Token */
char * pmode = NULL; /* Processing Mode Token */
char * token = NULL; /* String Token */
FILE * fp = NULL; /* File Pointer */
/* Temporary Variables */
video_file * v = (video_file *) malloc_safe (sizeof(*v), s);
image_file * i = (image_file *) malloc_safe (sizeof(*i), s);
audio_file * a = (audio_file *) malloc_safe (sizeof(*a), s);
#ifdef DEBUG
fprintf (stderr, "Reading configuration file.\n");
#endif
fp = fopen_safe (s->config_file, "r", s);
for (it = size; it > 0x0; it--)
{
/* Initializing Variables */
memset (new_entry, '\0', LINE_LENGTH * 5);
init_video_file (v);
init_image_file (i);
init_audio_file (a);
/* Extracting entry from configuration file */
if (fgets (new_entry, (LINE_LENGTH * 5 * sizeof(char)), fp) == NULL)
{
fatal_error (s, "Error reading configuration file.");
}
I'm always getting the "Error Reading Configuration file", when compiling with the MakeFile, however I don't get it on Netbeans.
Program Input: ./macmp2 -c configuration.txt Ouput: Reading configuration file.
[*] Fatal Error: Error reading configuration file.
As I said, the code is for my senior project, I can't post it here. I'm pretty sure the issue has to do with the Linker Flags. The program stops at the first iteration. As I said before the code works when compiled with Netbeans, but stops working when compiled using the posted MakeFile.
---------- UPDATE ----------
As requested by Jonathan Leffler, here are the wrappers for fopen_safe and malloc_safe.
Code for fopen_safe and malloc_safe which are both implemented by me.
void * malloc_safe (size_t size, macmp2_state * s)
{
void * ptr = malloc (size);
if (ptr == NULL)
{
fatal_error (s, "Memory could not be allocated.");
}
return ptr;
}
FILE * fopen_safe (const char * filename, const char * mode, macmp2_state * s)
{
FILE * stream = fopen (filename, mode);
if (stream == NULL)
{
fatal_error (s, "Stream could not be open.");
}
return stream;
}