【问题标题】:undefined reference issue when including header包含标头时未定义的参考问题
【发布时间】:2021-06-02 09:56:44
【问题描述】:

我用 C 语言构建了一个项目,假设要创建一个“程序集”编译文件。 我有这些文件:

ma​​in.c

#include <stdio.h>
#include "FirstTransition.h"
#include "Constants.h"
int main() {
   return firstTransition(TEMP_FILE); 
}

FirstTransition.h

#ifndef FIRSTTRANSITIONH
#define FIRSTTRANSITIONH
int firstTransition(char*);
#endif

FirstTransition.c

/*This file contain the first transition method.*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Constants.h"
#include "Structs.h"
#include "UtilsFuncs.h"
int firstTransition (char *fileName)
{
    int IC=100,DC=0; /*IC - Instructions counter, DC - Data counter.*/
    FILE *insFile; /*Instructions file pointer.*/ 
    instNode *listOfInstructions;
    instNode *temp;
    if((insFile=fopen(fileName,"r"))==NULL)
    {
        perror("cannot open file!");
        return EXIT_FAILURE;
    }

    listOfInstructions = buildInstructionsList(insFile);
    temp = listOfInstructions;
    while(temp!=NULL)
    {
        printf("%s->",temp->words);
    }
    
    if (fclose(insFile))
    {
        perror("cannot close file!");
        return EXIT_FAILURE;
    }
    return(EXIT_SUCCESS);
}

UtilsFuncs.h

#ifndef UTILSFUNCSH
#define UTILSFUNCSH


#include <stdio.h>
#include "Structs.h"
instNode* buildInstructionsList(FILE *);

#endif 

UtilsFuncs.c

/*
This file contains all the utilites functions for the project.
*/
#include <stdlib.h>
#include <stdio.h>
#include "Structs.h"
#include "Constants.h"
#include "UtilsFuncs.h"

instNode* buildInstructionsList(FILE *insFile)
{
    char line[MAX_LINE_LEN] ={0};/*varible for reading the lines.*/
    instNode *head = NULL;
    instNode *pos = NULL;
    while (fgets(line, MAX_LINE_LEN, insFile))
    {
        if(pos == NULL) /*first insertion*/
        {
            head = (instNode*)malloc(sizeof(instNode));
            pos = head;
            head->words = line;
            head->next = NULL;
        }
        else
        {
            pos->next = (instNode*)malloc(sizeof(instNode));
            pos->next->words=line;
            pos->next->next=NULL;
            pos=pos->next;
        }
    }
    return head;
}

我也有这个ma​​kefile

myprog:main.o firstTransition.o 
    gcc -g -ansi -Wall -pedantic main.o firstTransition.o -o myprog 
main.o: main.c FirstTransition.h
    gcc -c -ansi -Wall -pedantic main.c -o main.o
firstTransition.o: FirstTransition.c Constants.h FirstTransition.h UtilsFuncs.h
    gcc -c -ansi -Wall -pedantic FirstTransition.c -o firstTransition.o
UtilsFuncs.o: UtilsFuncs.c Constants.h Structs.h UtilsFuncs.h
    gcc -c -ansi -Wall -pedantic UtilsFuncs.c -o UtilsFuncs.o

当我尝试在终端中执行“make”时出现此错误:

gcc -g -ansi -Wall -pedantic main.o firstTransition.o -o myprog /usr/bin/ld: firstTransition.o: in function firstTransition': FirstTransition.c:(.text+0x57): undefined reference to buildInstructionsList' collect2: error: ld returned 1 exit status make: *** [makefile:2: myprog] 错误 1

有什么问题?为什么我不能运行这段代码? 它不是为 UtilsFuncs 创建目标文件... 我试图删除标题保护,但它也没有帮助。

谢谢。

【问题讨论】:

  • 你没有将UtilsFuncs.o链接到你的主程序
  • 它甚至没有被创建...
  • 因为它没有列在依赖项中。
  • 谢谢!修好了。

标签: c ubuntu gcc makefile


【解决方案1】:

链接程序时,需要在 FirstTransition.o 之前添加 UtilsFuncs.o:

 gcc -g -ansi -Wall -pedantic main.o UtilsFuncs.o firstTransition.o -o myprog

【讨论】:

    猜你喜欢
    • 2011-10-30
    • 2010-10-15
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    相关资源
    最近更新 更多