c - 对 xxx include 的 undefined reference 不起作用

标签 c gcc compilation compiler-errors

我正在尝试编译一个 .c 文件,但我得到了“ undefined reference ”错误,即使我放置了必要的包含。顺便说一句,正如您在下面我的代码标题中看到的那样,#include drive.h 没有任何问题,但是对于#include 硬件,gcc 似乎忽略了这一行。

这是我的代码,谢谢 :)

顺便说一句,我是这样编译的:gcc drive.c

 #include <stdlib.h>
 #include <stdio.h>
 #include <assert.h>
 #include "include/hardware.h"
 #include "drive.h"

 static void empty_it(){
  return ;
 }

int main(int argc , char** argv){

unsigned int numCyl ; 
unsigned int numSec ; 

unsigned char* buffer ; 

unsigned int i ;

/* init hardware */
/* on initialise toujours le materiel de la meme facon*/
if(init_hardware("hardware.ini") == 0){
    fprintf(stderr, "hardware initialisation error ! \n");
    exit(EXIT_FAILURE) ;
}

/* Interrupt handlers */
for(i=0 ;i<16;i++){
    IRQVECTOR[i] = empty_it ; 
}

/* Allows all IT */
_mask(1);

assert(argc == 3);
numCyl = atoi(argv[1]) ;
numSec = atoi(argv[2]) ;

buffer = malloc(sizeof(char) *HDA_SECTORSIZE);
assert(buffer);

read_sector(numCyl , numSec , buffer);

for(i=0;i<HDA_SECTORSIZE;i++)
    printf("%01X\n", buffer[i]);

free(buffer) ; 

return EXIT_SUCCESS ;
}

最佳答案

包含头文件不会添加函数的定义,只会添加它们的声明。要添加定义,您可以:

  1. 将定义它们的源文件添加到编译器命令中。
  2. 将目标(已编译文件)文件添加到编译器命令。
  3. 链接到外部静态库或共享库。

您的问题本身不包含可以帮助您的具体信息,例如 read_sector() 是什么?但您当前问题的一般答案是这样的。

关于c - 对 xxx include 的 undefined reference 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40350657/

相关文章:

c - Optarg 和命令行参数

c - 在 C 中重新定义 const char*

c - 使 `too many arguments in call to function` 在 Clang 中成为错误

linux - 使用非主要 libc 编译,(即 eglibc、uClibc)

c++ - 如何仅在C/C++项目中处理宏而不进行编译?

C/C++ VS Code 扩展抛出构建错误 : "The task provider for "C/C+ +"tasks unexpectedly provided a task of type "shell"."

c# - C# 编译器/优化命令行选项会影响 JITter 吗?

c - 我在哪里可以合法地在 C99 中声明变量?

java - Eclipse编译将大量.class文件放入包文件夹中

css - 编译后登录表单出现奇怪的边框