You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GCC报undefined reference错误:函数已定义编译仍报错

编译时undefined reference错误排查:string_linked_list_push函数链接失败

用GCC编译可执行文件时触发undefined reference to string_linked_list_push错误,该函数已在头文件声明、源文件实现并编译为.o文件,错误来自graph.o中的函数调用。


相关代码

初始Makefile

kosaraju: kosaraju.o string_linked_list.o graph.o [...]
    gcc -o ../kosaraju kosaraju.o string_linked_list.o graph.o [...] -Wall
    make clean
    
kosaraju.o: 
    gcc -o kosaraju.o kosaraju.c -c -Wall
    
graph.o:
    gcc -o graph.o graph.c -c -Wall
    
string_linked_list.o:
    gcc -o string_linked_list.o string_linked_list.c -c -Wall

...

graph.c(调用函数的代码段)

#include "string_linked_list.h"
#include "graph.h"
#include ...

...

void dfs1_step(char * node, string_linked_list * topological_sort, /*more stuff*/)
{
    {
     // DFS_step implementation
    }

    // after iteration marks as visited
    string_linked_list_push(topological_sort, node);
}

string_linked_list.h(函数声明)

#ifndef STRING_LINKED_LIST
#define STRING_LINKED_LIST
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>

typedef struct string_linked_list_node
{
    char * value;
    struct string_linked_list_node * next;
} string_linked_list_node;

typedef struct string_linked_list
{
    struct string_linked_list_node * head;
    struct string_linked_list_node * tail;
    uint16_t count;
} string_linked_list;

bool string_linked_list_push(string_linked_list * list, char * value);
// more prototypes
#endif 

string_linked_list.c(函数实现)

#include "graph.h"
#include "string_linked_list.h"
#include //more stuff

// Problematic function
bool string_linked_list_push(string_linked_list * list, char * value)
{
    string_linked_list_node * new_node = malloc(sizeof(string_linked_list_node));
    new_node->value = calloc(1, NODE_ID_LEN);
    if (new_node == NULL || new_node->value == NULL)
    {
        return false;
    }
    strncpy(new_node->value, value, NODE_ID_LEN);
    new_node->next = list->head->next;
    list->head->next = new_node;
    return true;
}

// more functions

编译错误信息

执行make后输出:

gcc -o ../kosaraju kosaraju.c scanner.o map.o string_linked_list.o graph.o -Wall
/usr/bin/ld: graph.o: in function `dfs1_step':
graph.c:(.text+0x48a): undefined reference to `string_linked_list_push'
collect2: error: ld returned 1 exit status

已尝试的解决方法

  • 确认函数实现文件正确、签名与头文件完全匹配
  • 修改Makefile尝试将string_linked_list直接链接到graph.o,两种写法都试过(带/不带-c选项):
    graph.o: string_linked_list.o
        gcc -o graph.o string_linked_list.o graph.c -Wall
    
    graph.o:
        gcc -o graph.o string_linked_list.c graph.c -Wall
    

问题依旧存在。


错误原因分析

  1. 链接顺序问题:GCC链接器按从左到右的顺序查找符号,若graph.o放在string_linked_list.o前面,链接器处理graph.o时找不到目标符号,后续遇到string_linked_list.o也不会回溯补全。可调整链接命令中目标文件顺序,把string_linked_list.o移到graph.o之前:

    gcc -o ../kosaraju kosaraju.o string_linked_list.o graph.o scanner.o map.o [...] -Wall
    
  2. 符号名被C++ mangling(混合编译场景):若项目存在C代码,而string_linked_list.c是C文件,头文件未加extern "C"声明会导致符号不匹配。若有C源文件,需在头文件中添加:

    #ifdef __cplusplus
    extern "C" {
    #endif
    // 原头文件内容
    #ifdef __cplusplus
    }
    #endif
    
  3. 目标文件残留问题:初始Makefile链接后立即执行make clean,可能导致后续重新编译时依赖失效。手动删除所有.o文件,重新执行make,避免旧文件干扰。

  4. 函数未被编译进目标文件:用nm string_linked_list.o命令查看该文件中是否存在string_linked_list_push符号,若不存在,说明函数实现可能被条件编译排除,或源文件未正确编译。


内容的提问来源于stack exchange,提问作者Milky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 08:25:35