1

如果有人可以向我解释编译所涉及的整个过程以及链接器的功能(所有这些“编程后台工作”),这可以让我实现整个方案。会很感激的。

错误是“未定义对 menu() 的引用”

我在 Linux 上并使用代码块在 C 中编程。所有文件都在同一个文件夹中。

我现在有 3 个文件:

maintest.c

#include <stdio.h>
#include "biblioteca.h"
int main() {
    menu(2, "opçao 1", "opçao 2");
}

文献资料库

#ifndef _BIBLIOTECA_H_
#define _BIBLIOTECA_H_

#define null '\0'
typedef enum { false, true } boolean;
typedef unsigned short uint;
typedef char* string;


void menu(int count, ...);

#endif

书目.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "biblioteca.h"

void menu(int count, ...) {
    va_list listPointer;
    va_start(listPointer, count);
    for(int i = 1; i <= count; i++) {
        char *string = va_arg(listPointer, char*);
        line(1, 1);;
        printf("%d ..... %s", i < count ? i : 0 , string);
    }
    va_end(listPointer);
}

如果有人可以试一试并向我解释发生了什么以及为什么文件没有相互链接,我将不胜感激。

4

1 回答 1

0

错误是“未定义对 menu() 的引用”可能是 IDE 链接器问题,或者您可能没有正确链接

第二个文件中有一个问题 stdlib.h包括sys/types.h 按需包含,并且此头文件包含此类型定义

typedef unsigned int uint;

在你的头文件中你也有同样的东西。

所以正因为如此,你会得到一个错误conflicting types

这可以通过删除第二个文件中包含的头文件来避免,或者您可以删除包含 stdlib.h(不推荐这样做)。

如果您使用 Linux。请尝试使用 GCC.use -Wall 选项来检查是否有任何警告。有意注释行(1,1);由于对 GCC 中行的未定义引用。请确保您的三个文件位于编译代码的同一目录中。否则,您需要提供文件的绝对路径。

 gcc -Wall maintest.c  biblioteca.c -o result   

./result

maintest.c

#include <stdio.h>
#include "biblioteca.h"
int main() {
    menu(2, "opç 1", "opç 2");
}

书目.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>

void menu(int count, ...) {
    int i;
    va_list listPointer;
    va_start(listPointer, count);
    for( i = 1; i <= count; i++) {
        char *string = va_arg(listPointer, char*);
    //    line(1, 1);
        printf("%d ..... %s", i < count ? i : 0 , string);
    }
    va_end(listPointer);
} 

文献资料库

#ifndef _BIBLIOTECA_H_
#define _BIBLIOTECA_H_

#define null '\0'
typedef enum { false, true } boolean;
typedef unsigned short uint;
typedef char* string;

void menu(int count, ...);

#endif
于 2013-09-07T15:46:21.153 回答