我正在尝试一些代码。这是我的主要内容:
#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;
}
使用相关的.c和.h文件:
// 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文件:
#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;
}
我收到此错误:
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
为什么?
答案 0 :(得分:1)
这看起来像链接器错误。您没有正确编译您的程序。仅仅包含一个标题并不能告诉编译器如何为声明的函数和变量找到代码。
但是,它也可能是C ++ vs C问题。如果您的main()
编译为C ++,则必须在#include
中包装C标头的extern "C" { ... }
。
当然,如果它是C ++,那么将Bitstream
实现为C模块而非实际类是非常不正统的。