对xxx包含的未定义引用不起作用

时间:2022-08-15 15:19:59

I'm trying to compile a .c file but I get "undefined reference to" error even though I put the necessary includes. by the way as you can see in the header of my code bellow, I don't have any probleme with #include drive.h, but with #include hardware it's like gcc is ignoring this line.

我正在尝试编译.c文件但是我得到了“未定义的引用”错误,即使我放了必要的包含。顺便说一句,你可以在我的代码的标题中看到,我对#include drive.h没有任何问题,但是使用#include硬件就像gcc忽略了这一行。

here is my code and thank you :)

这是我的代码,谢谢:)

by the way I'm compiling like this: gcc drive.c

顺便说一句,我正在编译: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 个解决方案

#1


2  

Including a header file does not add the definition of functions, only their declarations. To add definitions you either:

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

  1. Add the source files defining them to the compiler command.
  2. 将定义它们的源文件添加到编译器命令。
  3. Add the object (already compiled files) files to the compiler command.
  4. 将对象(已编译的文件)文件添加到编译器命令。
  5. Link to an external static or shared library.
  6. 链接到外部静态或共享库。

Your question as it is, doesn't contain the specific information to help you, what is read_sector() for instance? But the general answer to your current problem is this.

你的问题是,它不包含帮助你的具体信息,例如read_sector()是什么?但是对你当前问题的一般回答是这样的。

#1


2  

Including a header file does not add the definition of functions, only their declarations. To add definitions you either:

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

  1. Add the source files defining them to the compiler command.
  2. 将定义它们的源文件添加到编译器命令。
  3. Add the object (already compiled files) files to the compiler command.
  4. 将对象(已编译的文件)文件添加到编译器命令。
  5. Link to an external static or shared library.
  6. 链接到外部静态或共享库。

Your question as it is, doesn't contain the specific information to help you, what is read_sector() for instance? But the general answer to your current problem is this.

你的问题是,它不包含帮助你的具体信息,例如read_sector()是什么?但是对你当前问题的一般回答是这样的。