Question

When I try to compile my code I get:

thread-support.cpp: At global scope:
thread-support.cpp:18: error: redefinition of ‘Semaphore* Mutex’
thread.h:15: error: ‘Semaphore* Mutex’ previously declared here
thread-support.cpp:19: error: redefinition of ‘Semaphore* FreePots’
thread.h:16: error: ‘Semaphore* FreePots’ previously declared here
thread-support.cpp:20: error: redefinition of ‘Semaphore* Mother’
thread.h:18: error: ‘Semaphore* Mother’ previously declared here
thread-support.cpp:21: error: redefinition of ‘Semaphore* Nap’
thread.h:20: error: ‘Semaphore* Nap’ previously declared here
thread-support.cpp:22: error: redefinition of ‘int* availPots’
thread.h:21: error: ‘int* availPots’ previously declared here
thread-support.cpp:23: error: redefinition of ‘int* emptyPots’
thread.h:22: error: ‘int* emptyPots’ previously declared here
thread-support.cpp:24: error: redefinition of ‘int* totalPots’
thread.h:24: error: ‘int* totalPots’ previously declared here

For the sake of space I just included my included my includes and global variables. If you would like me to post the whole file just let me know.

This is thread.h:

#pragma once

#include "ThreadClass.h"
#include "thread-support.cpp"

extern Semaphore *Mutex;
extern Semaphore *FreePots; 
extern Semaphore *Mother;
extern Semaphore *Nap;
extern int *availPots;
extern int *emptyPots;
extern int *totalPots;

This is thread.cpp:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include "thread.h"

This is thread-support.cpp

#include <iostream>
#include <string.h>
#include <stdio.h>
#include "thread.h"

Semaphore *Mutex;
Semaphore *FreePots;
Semaphore *Mother;
Semaphore *Nap;
int *availPots;
int *emptyPots;
int *totalPots;

This is thread-main.cpp:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "thread.h"

Here is my makefile:

CC       = c++
FLAGS    =
CFLAGS   = -g -O2
DFLAGS   = -DPACKAGE=\"threadsystem\" -DVERSION=\"1.0\" -DPTHREAD=1 -DUNIX_MSG_Q=1 -DSTDC_HEADERS=1
IFLAGS   = -I/local/eit-linux/apps/ThreadMentor/include
TMLIB    = /local/eit-linux/apps/ThreadMentor/Visual/libthreadclass.a
TMLIB_NV    = /local/eit-linux/apps/ThreadMentor/NoVisual/libthreadclass.a

OBJ_FILE = thread.o thread-support.o thread-main.o
EXE_FILE = prog4

${EXE_FILE}: ${OBJ_FILE}
    ${CC} ${FLAGS}  -o ${EXE_FILE}  ${OBJ_FILE} ${TMLIB_NV} -lpthread

thread.o: thread.cpp
    ${CC} ${DFLAGS} ${IFLAGS} ${CFLAGS} -c thread.cpp

thread-support.o: thread-support.cpp
    ${CC} ${DFLAGS} ${IFLAGS} ${CFLAGS} -c thread-support.cpp

thread-main.o: thread-main.cpp
    ${CC} ${DFLAGS} ${IFLAGS} ${CFLAGS} -c thread-main.cpp

Visual: ${OBJ_FILE}
    ${CC} ${FLAGS}  -o ${EXE_FILE}  ${OBJ_FILE} ${TMLIB} -lpthread

clean:
    rm -f ${OBJ_FILE} ${EXE_FILE}

I've tried everything I could think of to fix this problem, but I am having trouble finding the answer to this question by Googling. I'm new to C++, but I thought that the #pragma once was what I needed.

Was it helpful?

Solution

You have

#include "thread-support.cpp"

in the header thread.h, which causes the problem. Don't include it but compile it separately and link it. (Which is already done properly by the Makefile, so just remove the include from the header)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top