user2793162
user2793162

Reputation:

Getting unresolved external symbol error

I am experimenting with some code. This is my main:

#include "stdafx.h"
#include "ByteStream.h"

int _tmain(int argc, _TCHAR* argv[])
{
    uint8_t x[28];

    ByteStream b;
    ByteStreamInit(&b, x, 28);

    for(int i = 0; i<28; i++)
    {
        ByteStreamAdvance(&b,1);
    }

    return 0;
}

With relevant .c and .h files:

// Bytestream.h    
#include <stdint.h>


    /*
     * Bytestream data structure
     */
    typedef struct
    {

      uint8_t * data;
      uint32_t length;
      uint32_t offset;

    } ByteStream;


    uint32_t ByteStreamNrOfAvailElements(ByteStream *bs);
    int32_t ByteStreamInit(ByteStream *bs, uint8_t *data, uint32_t len);
    int32_t ByteStreamCurrPos(ByteStream *bs);
    int32_t ByteStreamAdvance(ByteStream *bs, uint32_t n);

.c file:

#include "ByteStream.h"

int32_t ByteStreamInit(ByteStream *bs, uint8_t *data, uint32_t len)
{
    if (!bs)
        return -1;

    bs->data = data;
    bs->length = len;
    bs->offset = 0;

    return 0;
}

uint32_t ByteStreamNrOfAvailElements(ByteStream *bs)
{
    return bs->length - bs->offset;
}

int32_t ByteStreamCurrPos(ByteStream *bs)
{
    return bs->offset;
}

int32_t ByteStreamAdvance(ByteStream *bs, uint32_t n)
{
    if(n >= ByteStreamNrOfAvailElements(bs)) // We can move only forward -1 number of available elements
        return -1;

    bs->offset+=n;

    return 0;
}

I am getting this error:

Error   1   error LNK2019: unresolved external symbol "int __cdecl ByteStreamInit(struct ByteStream *,unsigned char *,unsigned int)" (?ByteStreamInit@@YAHPAUByteStream@@PAEI@Z) referenced in function _wmain  c:\Users\documents\visual studio 2012\Projects\ConsoleApplication7\ConsoleApplication7\ConsoleApplication7.obj  ConsoleApplication7

Error   2   error LNK2019: unresolved external symbol "int __cdecl ByteStreamAdvance(struct ByteStream *,unsigned int)" (?ByteStreamAdvance@@YAHPAUByteStream@@I@Z) referenced in function _wmain   c:\Users\documents\visual studio 2012\Projects\ConsoleApplication7\ConsoleApplication7\ConsoleApplication7.obj  ConsoleApplication7

Error   3   error LNK1120: 2 unresolved externals   c:\users\documents\visual studio 2012\Projects\ConsoleApplication7\Debug\ConsoleApplication7.exe    1   1   ConsoleApplication7

why?

Upvotes: 0

Views: 218

Answers (1)

unwind
unwind

Reputation: 400129

This looks like a linker error. You are not compiling your program correctly. Merely including a header does nothing to tell the compiler how to find the code for the declared functions and variables.

However, it could also be a C++ vs C problem. If your main() is compiled as C++ then you must wrap the #include of the C header in extern "C" { ... }.

Of course, if it's C++, then you're being pretty unorthodox to implement the Bitstream as a C module rather than as an actual class.

Upvotes: 1

Related Questions