简体   繁体   中英

xcode c code compile error

I have used crc in my program. ViewDidLoad method code in ViewController.mm is below:

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

crc32 function code is below:

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;

}

compile error is :

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

After I changed ViewController.mm to ViewController.m , compiled successfully. Why?

Your defining of crc is probably in a C file, not a C++ file, so the name isn't being mangled to a type specific version.

Your usage in a .mm file will be as if for C++, and hence will be name mangled.

By changing the containing file to .m, you compile it against C and hence the issue goes away.

Alternatively, you could change the crc file to have a C++ extension (c++ or cc)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM