c - C 函数(fgets、scanf、fread、fwrite、fopen)的未知行为

标签 c linux netbeans makefile libavcodec

我目前正处于计算机工程高级项目的最终开发和测试阶段。在使用库(Libjpeg、Libbmp、PocketSphinx、Libavcodec、Libavformat、Libavutil)设计和开发代码(C、Bash)并使用 netbeans 作为 IDE 之后。我遇到的问题是,在 Netbeans 中,代码可以完美地编译和链接,并且软件的执行也很好。但是,当我使用 MakeFile 在外部编译和链接代码时,函数如下:fopenfreadfwritefgetsfscanf 等停止工作...

GCcflags:

-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

同样的未知行为也影响了 PocketSphinx 的性能,现在无法打开 HMM。

非常感谢任何启发,因为我的最终报告将在下周进行。

------------ 更新------------

这是我的实际生成文件

.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

编译时错误:无 运行时错误:无

我首先注意到这部分代码中的问题。

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.");
        }

在使用 MakeFile 进行编译时,我总是收到“读取配置文件时出错”,但在 Netbeans 上却没有。

程序 输入:./macmp2 -c 配置.txt 输出:读取配置文件。

[*] fatal error :读取配置文件时出错。

正如我所说,代码是我高级项目的代码,我不能在这里发布。我很确定这个问题与链接器标志有关。程序在第一次迭代时停止。正如我之前所说,代码在使用 Netbeans 编译时有效,但在使用发布的 MakeFile 编译时停止工作。

------------ 更新------------

应 Jonathan Leffler 的要求,这里是 fopen_safe 和 malloc_safe 的包装器。

我实现的 fopen_safe 和 malloc_safe 代码。

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;
}

最佳答案

您的链接标志(您问题中的“ld-flags”)顺序错误。

链接器(ld,通常通过 gcc 调用)的程序参数顺序很重要。你应该把你的源文件放在第一位(对于 gcc),然后是你的目标文件(按照依赖的顺序),然​​后是库(从高层到低层系统)。例如,您的 -lpthread 应该位于 -lavformat

之后

但是你真的应该学习编写 Makefile-s。不知道你时间够不够好的做法是在项目开始时启动您的 Makefile 并对其进行改进(随着您改进代码)。

关于c - C 函数(fgets、scanf、fread、fwrite、fopen)的未知行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9281005/

相关文章:

linux - WCF 服务能否向基于 Linux 的 C++ 客户端应用程序提供发布/订阅事件?

linux - 如何在新的终端窗口或选项卡中使用从终端 shell 脚本设置的变量? (OSX)

javascript - Netbeans 8.0.2 的语法高亮和代码折叠不适用于 PHP、HTML、Javascript

c - forks和pipes实现linux编译器

Java 数组列表/列表错误

java - 关于在 netbeans IDE 中编辑 jbutton

c - 无法读取 C 中的 EOF 字符

c - 在计算三个值的中间值的程序中使用宏

c - 打印链表时出错

c++ - C 中正确的指针算法