C Undefined reference

2019-08-05 07:04发布

I have the following code:

main.c

#include "checksum.h"

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

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

#ifndef CHECKSUM_H_INCLUDED
#define CHECKSUM_H_INCLUDED

void checksum(char *Buff);

#endif

When compiling I get the following error:

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

I can't figure out what the problem is?

3条回答
Viruses.
2楼-- · 2019-08-05 07:36

I think: in checksum.c, you should include checksum.h.

查看更多
孤傲高冷的网名
3楼-- · 2019-08-05 07:46

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

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

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

查看更多
该账号已被封号
4楼-- · 2019-08-05 07:46

You could also try gcc -o program.out main.c checksum.c which will compile and link both files together

查看更多
登录 后发表回答