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