简体   繁体   中英

Undefined reference to function never used/defined/anything?

I have a C function as follows:

static uint32_t initrd_read(fs_node_t *node, 
    uint32_t offset, uint32_t size, uint8_t *buffer) {

    initrd_file_header_t header = file_headers[node->inode];
    if (offset > header.length)
        return 0;
    if (offset+size > header.length)
        size = header.length-offset;
    memcopy(buffer, header.offset+offset, size);
    return size;
}

When linked with the rest of the program, an undefined reference to 'memcpy' is thrown. memcpy is never used in the code, and is not defined. The code is linked free-standing, so it is not conflicting with a C library. For some reason the linker thinks that the above function is calling memcpy at the beginning of the function call, and I'm not sure why.

Why could this be happening?

memcpy could be used implicitly by the compiler to perform "long" copying operations (like struct assignment). For example, in your code you are doing

initrd_file_header_t header = file_headers[node->inode];

which looks like a good candidate for something that will actually be translated into an memcpy call.

Is there a reason you create a copy of that initrd_file_header_t object instead of accessing the original directly? I don't see you modify that object, so you could just do

const initrd_file_header_t *header = &file_headers[node->inode];

and access the fields as header->length etc. That probably will eliminate that implicit call to memcpy .

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