Undefined reference to function declared in header file C

Viewed 46

I have looked at a lot of posts with this error but none of them seem to have the simple case that I have.

libsuyash.h:

#ifndef libsuyash_h
#define libsuyash_h

#include<stdlib.h>

const int ENDEH = 1;
#define is_littleendian() ( (*(char*)&ENDEH) != 0 )

typedef unsigned char* ucp;
typedef u_int32_t uint32t;

unsigned char* xdatahex(unsigned char*);
int myfun(char, int);

#endif

libsuyash.c:

#include<string.h>
#include "libsuyash.h"

unsigned char* xdatahex(unsigned char* string) {

    if(string == NULL) 
       return NULL;

    size_t slength = strlen(string);
    if((slength % 2) != 0) // must be even
       return NULL;

    size_t dlength = slength / 2;

    ucp data = malloc(dlength);
    memset(data, 0, dlength);

    size_t index = 0;
    while (index < slength) {
        char c = string[index];
        int value = 0;
        if(c >= '0' && c <= '9')
          value = (c - '0');
        else if (c >= 'A' && c <= 'F') 
          value = (10 + (c - 'A'));
        else if (c >= 'a' && c <= 'f')
          value = (10 + (c - 'a'));
        else {
          free(data);
          return NULL;
        }

        data[(index/2)] += value << (((index + 1) % 2) * 4);

        index++;
    }

    return data;
}

int myfun(char a, int b) {
    return 69;
}

Following is the simple function where I am using it: clarglen.c:

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

int main(int argc, char* argv[]) {

    myfun('g', 8);

    ucp samplehexstr = "cafebabe";
    ucp hextodata = xdatahex(samplehexstr);

    for(int i = 0; i < 4; i++) {
        printf("%c,", *(hextodata++));
    }

    return 0;
}

I simply run: gcc clarglen.c (or sometimes with -o <outputfile>) and I get the following error:

/usr/bin/ld: /tmp/ccGDTY5N.o: in function `main':
clarglen.c:(.text+0x87): undefined reference to `myfun'
/usr/bin/ld: clarglen.c:(.text+0x9e): undefined reference to `xdatahex'
collect2: error: ld returned 1 exit status

I am clueless about what else I am missing. All files are in the same directory.

0 Answers
Related