alpine
alpine

Reputation: 1037

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?

Upvotes: 0

Views: 84

Answers (1)

David Berry
David Berry

Reputation: 41236

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)

Upvotes: 1

Related Questions