C未定义参考 [英] C Undefined reference

查看:159
本文介绍了C未定义参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

main.c

#include "checksum.h"

void main()
{
    char *Buf ="GPGGA204502.005106.9813N11402.2921W1090.91065.02M-16.27M";
    checksum(Buf);
}

checksum.c

checksum.c

#include <stdio.h>
#include <string.h>

checksum(char *Buff)
{
    int i;
    unsigned char XOR;
    unsigned long iLen = strlen(Buff);
    printf("Calculating checksum...\n");
    for (XOR = 0, i = 0; i < iLen; i++)
        XOR ^= (unsigned char)Buff[i];
    printf("%X \n",XOR);
}

checksum.h

checksum.h

#ifndef CHECKSUM_H_INCLUDED
#define CHECKSUM_H_INCLUDED

void checksum(char *Buff);

#endif

编译时出现以下错误:

/tmp/ccFQS7Ih.o: In function `main':
main.c:(.text+0x18): undefined reference to `checksum'
collect2: ld returned 1 exit status

我不知道是什么问题?

推荐答案

您仅编译一个文件,而不是两个.更准确地说,您不是将文件链接在一起.

You are compiling only one file not both. More precisely, you are not linking the files together.

我不知道您的编译器,但是使用gcc,会是这样的:

I don't know your compiler, but with gcc, it would be something like this:

gcc -c main.c          <-- compile only
gcc -c checksum.c      <-- compile only
gcc main.o checksum.o  <-- link the two

编辑:要自动执行此过程,请查看 程序,该程序读取 Makefile

To automate this process, take a look at the make program which reads Makefiles.

这篇关于C未定义参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆