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

C++头文件声明函数出现undefined reference错误排查求助

问题描述

编译运行主程序时出现链接错误,提示threadClose(int)和printSummary(int*, char*, int)存在未定义引用。相关文件内容及错误信息如下:

Makefile

prodcon: prodcon.o prodcon_support.o trans_sleep.o prodcon.h
    g++ prodcon.o trans_sleep.o -o prodcon -pthread

prodcon.o: prodcon.cpp prodcon.h
    g++ -c prodcon.cpp -o prodcon.o -pthread -O

prodcon_support.o: prodcon_support.cpp prodcon.h
    g++ -c prodcon_support.cpp -o prodcon_support.o -pthread -O

trans_sleep.o: trans_sleep.cpp prodcon.h
    g++ -c trans_sleep.cpp -o trans_sleep.o

clean:
    @rm *.o prodcon

all: prodcon

头文件 prodcon.h

#ifndef prodcon_h
#define prodcon_h

#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include <chrono>

//Mutex declarations
pthread_mutex_t mutex_write = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_read = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_queue = PTHREAD_MUTEX_INITIALIZER;

//Condition declarations
pthread_cond_t cond_not_empty = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_not_full = PTHREAD_COND_INITIALIZER;

//Global Tracking Variables
int workCreated = 0;
int timesSlept = 0;
int workAskedFor = 0;
int workReceived = 0;
int workCompleted = 0;

//Global Variable declarations
auto startTime = std::chrono::high_resolution_clock::now();
FILE * logFile;
bool outOfInput;

//Struct for consumer arguments
struct cons_arg_struct {
    long threadId;
    char *outputFile;
};

void Trans(int n);

void Sleep(int n);

void printSummary(int threadRets[], char outputFile[], int numThreads);

int* threadClose(int threadCounter);

#endif

函数定义文件 prodcon_support.cpp

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <chrono>
#include <iostream>
#include "prodcon.h"

using namespace std;

void printSummary(int threadRets[], char outputFile[], int numThreads){
    FILE * logFile = fopen(outputFile, "a");
    fprintf(logFile, "SUMMARY\n  Work %9i\n  Ask %10i\n  Recieve %6i\n  Complete %5i\n  Sleep %8i\n", workCreated, workAskedFor, workReceived, workCompleted, timesSlept);
    for(int i = 0; i < numThreads; i++){
        fprintf(logFile, "  Thread %2i %5i\n", i, threadRets[i]);
    }
    fprintf(logFile, "Transactions per Second: %2.2f", (workCreated/(chrono::duration<double>(chrono::high_resolution_clock::now()-startTime).count())));
    fclose(logFile);
    return;
};

int* threadClose(int threadCounter){
    pthread_mutex_unlock(&mutex_queue);
    int* threadRet = (int*)malloc(sizeof(int));
    *threadRet = threadCounter;
    return threadRet;
};

错误信息

g++ -c prodcon.cpp -o prodcon.o -pthread -O
g++ prodcon.o trans_sleep.o -o prodcon -pthread
/usr/bin/ld: prodcon.o: in function `consumer(void*)':
prodcon.cpp:(.text+0x2f): undefined reference to `threadClose(int)'
/usr/bin/ld: prodcon.o: in function `main':
prodcon.cpp:(.text+0xce1): undefined reference to `printSummary(int*, char*, int)'
collect2: error: ld returned 1 exit status

问题原因与解决方法

核心问题

链接阶段找不到两个函数的实现,因为它们的定义在prodcon_support.o目标文件中,但链接命令里未包含该文件。查看Makefile的第一条规则:依赖列表写了prodcon_support.o,但实际编译命令只用到了prodcon.o和trans_sleep.o,漏掉了关键的目标文件。

修复步骤

修改Makefile的链接命令,加入prodcon_support.o:

prodcon: prodcon.o prodcon_support.o trans_sleep.o prodcon.h
    g++ prodcon.o prodcon_support.o trans_sleep.o -o prodcon -pthread

额外优化建议

  • 使用自动变量简化命令,避免手动列文件出错:
    prodcon: prodcon.o prodcon_support.o trans_sleep.o
        g++ $^ -o $@ -pthread
    
    $^代表所有依赖的目标文件,$@代表当前目标文件prodcon,后续新增目标文件无需手动修改命令。
  • 头文件无需作为链接阶段的依赖,可从第一条规则的依赖中移除prodcon.h。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 11:30:46