程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了对函数的未定义引用,使用 makefile 编译大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决对函数的未定义引用,使用 makefile 编译?

开发过程中遇到对函数的未定义引用,使用 makefile 编译的问题如何解决?下面主要结合日常开发的经验,给出你关于对函数的未定义引用,使用 makefile 编译的解决方法建议,希望对你解决对函数的未定义引用,使用 makefile 编译有所启发或帮助;

我目前正在处理一项作业,并尝试使用 makefile 对其进行编译,目前看起来像这样

all: mysHell

mysHell: main.o lineParser.o
    gcc -g -m32 -Wall -o main.o lineParser.o mysHell

main.o: main.c
    gcc -g -m32 -Wall main.c -o main.o

lineParser.o: lineParser.c
    gcc -g -m32 -Wall lineParser.c -o lineParser.o

.PHONY: clean

clean: 
    rm -f *.c mysHell

lineParser 包含一个 .c 文件和一个 .h 文件,我已经将 .h 文件包含在我的 main.c 中,因此它应该可以正确编译。但是,我无法编译,因为我收到了“对 'parseCmdlines' 和 'freeCmdlines'(lineParser 中的函数)的未定义引用。除了这两个函数之外,所有内容都可以使用当前的 makefile 正确编译,所以我相信问题出在在 makefile 中,但我不知道我需要更改什么。

@H_554_11@main.c

#include "lineParser.h"
#include <stdio.h>
#include <unistd.h>

voID execute(cmdline *pCmdlinE){
    int i = execv(pCmdline->arguments[0],pCmdline->arguments);
    if (i == -1)
        perror("Error: ");
}


int main (int argc,char* argv[],char* envp[])
{
    char cwd[MAX_ARGUMENTS];
    char userinput[2048];
    struct cmdline * command;
    if (getcwd(cwd,sizeof(cwd)) == NulL)
        perror("getcwd() error");
    else
        printf("Current Working Directory is: %s\n",cwd);
    printf("Write a command:\n");
    if( fgets (userinput,2048,stdin)!=NulL ) {
        command = parseCmdlines(userinput);
        freeCmdlines(command);
   }
}

解决方法

您应该使用 -c 选项让 GCC 只进行编译(创建目标文件,不链接以构建可执行文件)和 -o 选项指定输出文件,而不是输入文件之一。

all: mysHell

mysHell: main.o LineParser.o
    # put -o at proper place
    gcc -g -m32 -Wall main.o LineParser.o -o mysHell

main.o: main.c
    # add -c
    gcc -c -g -m32 -Wall main.c -o main.o

LineParser.o: LineParser.c
    # add -c
    gcc -c -g -m32 -Wall LineParser.c -o LineParser.o

.PHONY: clean

clean: 
    rm -f *.c mysHell

大佬总结

以上是大佬教程为你收集整理的对函数的未定义引用,使用 makefile 编译全部内容,希望文章能够帮你解决对函数的未定义引用,使用 makefile 编译所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。