I am attempting to compile my c++ code, and I continue getting the error:
/tmp/ccEsZppG.o:(.bss+0x0): multiple definition of `mailboxes'
/tmp/ccEZq43v.o:(.bss+0x0): first defined here
/tmp/ccEsZppG.o:(.bss+0xc0): multiple definition of `threads'
/tmp/ccEZq43v.o:(.bss+0xc0): first defined here
/tmp/ccEsZppG.o:(.bss+0x120): multiple definition of `semaphores'
/tmp/ccEZq43v.o:(.bss+0x120): first defined here
collect2: error: ld returned 1 exit status
Here is my code:
addem.cpp
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <semaphore.h>
#include <pthread.h>
#include "mailbox.h"
using namespace std;
void *sumUp(void *arg);
int main(int argc, char *argv[]) {
  int numThreads, minThreads, maxInt, minInt;
  if (argc < 3) {
    cout << "Error: Need three arguments" << endl;
    return 1;
  }
  numThreads = atoi(argv[1]);
  maxInt = atoi(argv[2]);
  minThreads = 1;
  minInt = 1;
  if (numThreads < 1) {
    cout << "Cannot work with less than one thread\n"
         << "It's okay but do better next time!\n"
         << "We'll work with 1 thread this time.\n";
    numThreads = minThreads;
  } else if (numThreads > MAXTHREAD) {
    cout << "Sorry, the max for threads is 10.\n"
         << "We'll work with 10 threads this time.\n";
    numThreads = MAXTHREAD;
  }
  if (maxInt < 1) {
    cout << "What do you want me to do? I can't count backwards!\n"
         << "I can barely count forwards! Let's make the max number\n"
         << "be 1 to save time\n";
    maxInt = minInt;
  }
  struct msg outgoingMail[numThreads];
  int divider = maxInt / numThreads;
  int count = 1;
  //initialize arrays (mailboxes, semaphores)
  for (int i = 0; i < numThreads; i++) {
    sem_init(&semaphores[i], 0, 1);
    outgoingMail[i].iSender = 0;
    outgoingMail[i].type = RANGE;
    outgoingMail[i].value1 = count;
    count = count + divider;
    if (i = numThreads - 1) {
      outgoingMail[i].value2 = maxInt;
    } else {
      outgoingMail[i].value2 = count;
    }
  }
  for (int message = 0; message < numThreads; message++) {
    SendMsg(message+1, outgoingMail[message]);
  }
  int thread;
  for (thread = 0; thread <= numThreads; thread++) {
    pthread_create(&threads[thread], NULL, &sumUp, (void *)(intptr_t)(thread+1));
  }
  struct msg incomingMsg;
  int total = 0;
  for (thread = 0; thread < numThreads; thread++) {
    RecvMsg(0, incomingMsg);
    total = total + incomingMsg.value1;
  }
  cout << "The total for 1 to " << maxInt << " using "
       << numThreads << " threads is " << total << endl;
  return 0;
}
void *sumUp(void *arg) {
  int index,total;
  index = (intptr_t)arg;
  struct msg message;
  RecvMsg(index, message);
  message.iSender = index;
  message.type = ALLDONE;
  total = 0;
  for (int i = message.value1; i <= message.value2; i++) {
    total += i;
  }
  SendMsg(0, message);
  
  return (void *) 0;
}
mailbox.cpp
#include <stdio.h>
#include <iostream>
#include "mailbox.h"
using namespace std;
int SendMsg(int iTo, struct msg &Msg) {
  if (safeToCall(iTo)) {
    cout << "Error calling SendMsg" << endl;
    return 1;
  }
  sem_wait(&semaphores[iTo]);
  mailboxes[iTo] = Msg;
  sem_post(&semaphores[iTo]);
  return 0;
}
int RecvMsg(int iFrom, struct msg &Msg) {
  sem_wait(&semaphores[iFrom]);
  if (safeToCall(iFrom)) {
    cout << "Error calling RecvMsg" << endl;
    return 1;
  }
  mailboxes[iFrom] = Msg;
  sem_post(&semaphores[iFrom]);
  return 0;
}
bool safeToCall(int location) {
  bool safe = !(location < 0 || location > MAXTHREAD + 1);
  return safe;
  //return true;
}
mailbox.h
#ifndef MAILBOX_H_
#define MAILBOX_H_
#define RANGE 1
#define ALLDONE 2
#define MAXTHREAD 10
#include <semaphore.h>
#include <pthread.h>
struct msg {
  int iSender; /* sender of the message (0 .. numThreads)*/
  int type; /* its type */
  int value1; /* first value */
  int value2; /* second value */
};
struct msg mailboxes[MAXTHREAD + 1];
pthread_t threads[MAXTHREAD + 1];
sem_t semaphores[MAXTHREAD + 1];
int SendMsg(int iTo, struct msg &Msg);
int RecvMsg(int iFrom, struct msg &Msg);
bool safeToCall(int location);
#endif
I am compiling the code with the command
g++ -o addem addem.cpp mailbox.cpp -lpthread
I have tried commenting out all of the function bodies in the source code to leave them as stub functions, and the same error occurs. The only way I have been able to compile the file is if I comment out the function bodies, and remove
#include "mailbox.h"
From at least one of the files. I feel it has to do with how I am initializing the arrays? But I cannot figure out a workaround.
