0

I am just hoping somebody can point out my mistake in my .h file and/or makefile for this code. I run my main program and I get an error when making it saying there is an undefined reference to 'threadClose(int)' and 'printSummary(int*, char*, int)'

I know I am just missing something here but I can't spot it for some reason

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

Header File

#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

C++ file where functions are defined:

#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;
};

EDIT: Definitely should have included the error:

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
Jadon Latta
  • 147
  • 1
  • 11
  • Try changing the order of binary files passed to the linker step – πάντα ῥεῖ Oct 25 '22 at 21:57
  • That will not help. Linker order only matters for _libraries_ and there are no libraries on the link line (according to the makefile). – MadScientist Oct 25 '22 at 21:58
  • You have provided a lot of info, but not the most important info. Please cut and paste, with formatting, the command line that make invoked (it is printed by make), then the error messages you got. It's not clear from your pidgin description of the errors whether the problem happens during _compiling_ or _linking_ and this is crucial information. Also, you show the source code but you don't say _which file_ this source code appears in. – MadScientist Oct 25 '22 at 22:00
  • 1
    Just in case, I'll point out that in your makefile you list three objects as prerequisites: `prodcon.o prodcon_support.o trans_sleep.o` but in the recipe link line you list only two of them: `prodcon.o trans_sleep.o` – MadScientist Oct 25 '22 at 22:01
  • @MadScientist you're completely right, that's my mistake. I have included the error. I didn't include the source file that the undefined reference is appearing in due to the length. – Jadon Latta Oct 25 '22 at 22:17
  • So, it's the link that failed. You say "the functions are defined in this C++ file", but you don't **name** the file that they're defined in. Since you don't tell us _which_ file those functions are defined in, we can't tell you what the problem is. However based on my previous answer I'm going to guess that they are defined in the file `prodcon_support.cpp`, which explains your error because you've left the `prodcon_support.o` file out of your link line. – MadScientist Oct 26 '22 at 14:08

0 Answers0