简体   繁体   English

未定义的函数引用从未使用/定义/任何东西?

[英]Undefined reference to function never used/defined/anything?

I have a C function as follows: 我有一个C函数如下:

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. 当与程序的其余部分链接时,将引发undefined reference to 'memcpy'undefined reference to 'memcpy' memcpy is never used in the code, and is not defined. memcpy从未在代码中使用,也未定义。 The code is linked free-standing, so it is not conflicting with a C library. 代码是独立链接的,因此它与C库没有冲突。 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. 由于某种原因,链接器认为上面的函数在函数调用开始时调用memcpy ,我不知道为什么。

Why could this be happening? 为什么会发生这种情况?

memcpy could be used implicitly by the compiler to perform "long" copying operations (like struct assignment). memcpy可以由编译器隐式使用,以执行“长”复制操作(如结构赋值)。 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. 对于实际上会被转换为memcpy调用的东西来说,它看起来很合适。

Is there a reason you create a copy of that initrd_file_header_t object instead of accessing the original directly? 您是否有理由创建该initrd_file_header_t对象的副本而不是直接访问原始对象? 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 . 并以header->length等方式访问字段。这可能会消除对memcpy隐式调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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