2015-11-07 129 views
0

我在程序中使用了crc。在ViewController.mmViewDidLoad方法代码如下:xcode c代码编译错误

char *testChars = "test chars"; 
crc32(0, (unsigned char*)testChars, strlen(testChars)); 

crc32功能代码如下:

uint32_t crc32 (uint32_t crc, unsigned char *buf, size_t len) 
{ 
unsigned char *end; 
crc = ~crc; 
for (end = buf + len; buf < end; ++buf) 
    crc = crc32_table[(crc^*buf) & 0xff]^(crc >> 8); 
return ~crc; 

}

编译错误是:

Undefined symbols for architecture x86_64,"crc32(unsigned int, unsigned char*, unsigned long)", referenced from:-[ViewController viewDidLoad:] in ViewController.o

我将ViewController.mm更改为ViewController.m后,编译成功。为什么?

+2

您的标题不匹配的标签。 C还是C++? – user2079303

回答

1

您定义的crc可能在C文件中,而不是C++文件中,因此名称未被修改为类型特定的版本。

您在.mm文件中的用法与C++类似,因此将会名称错位。

通过将包含文件更改为.m,您可以针对C编译它,因此问题消失。

或者,你可以改变CRC文件有一个C++的扩展(C++或CC)

+0

非常感谢 – alpine