Search code examples
cstructkey-valuekey-value-coding

LNK 2001: Unresolve external symbol struct **


#include "stdafx.h"
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

//typedef struct kvNode kvNode;

struct kvNode
{
    char cKey[32];
    uint32_t iValue;
    struct kvNode *NEXT;
};

//Function declarations
void put(char *key[], int *value);
uint32_t get(char *key[]);
int del(char *key[]);
uint32_t hashkey(char *key);
void strUpper(char *);

const int arraylen = 10; //length of key array
kvNode *keys[]; //our key array

int main()
{
    //Create array of pointers to kvNodes
    keys[arraylen] = { NULL };
    return 0;
}

I'm pretty sure this is the block of code returning the error. The error happens during build time and is:

1>keyValue.obj : error LNK2001: unresolved external symbol "struct kvNode * * keys" (?keys@@3PAPAUkvNode@@A)

What I'm trying to do is practice a key value store in C (undoubtedly using more, shall we say, relaxed languages recently has dulled my C knife), so I define a key value node struct, then an array of pointers to those nodes. Following other questions on SO about forward declaring of structs I got the syntax for declaring a pointer to the struct inside the struct, but for some reason the compiler (Vis Studio) is coughing on an unresolved external symbol and I don't know why.


Solution

  • You need to declare a size for this array kvNode *keys[]; //our key array

    If the size is not known at compile time, how can the compiler compile it? It's being declared to have static lifetime, so it has to be given a fixed size, a fixed block of memory in your binary. I think the compiler is interpreting the above line as a declaration and not a definition, hence the link error.