c - 在 Linux 中链接 - 寻找解释

标签 c linux compilation linker codeblocks

如果有人能给我解释一下编译的整个过程和链接器的功能(所有这些“编程幕后工作”),就可以让我了解整个方案。不胜感激。

错误是“对 menu() 的 undefined reference ”

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

我现在有 3 个文件:

维护测试.c

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

biblioteca.h

#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

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

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

最佳答案

错误是“undefined reference to menu()”可能是 IDE Linker 问题或者你可能没有正确链接

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

typedef unsigned int uint;

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

因此,您将收到 conflicting types 的错误

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

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

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

./result

维护测试.c

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

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

biblioteca.h

#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 - 在 Linux 中链接 - 寻找解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18674783/

相关文章:

c - 编译问题

c++ - 创建一个 fat16 模型

c - Makefile目标文件生成、变量替换等问题

ubuntu - 如何修复错误 : implicit declaration of function ‘setup_timer’

c - 如何检查消息是否传递到任何单个队列

将矩阵的每一行复制到一个临时数组

linux - 从 bash 脚本向 Mongo shell 传递命令,而不停止交互式 Mongo shell

linux - 准备计算机以学习系统调用和 Linux 内核模块

linux - 我如何使用 Qt Framework Installer 创建注册文件类型及其图标?

compilation - 有分析makefile文件的工具吗?