2013-11-14 126 views
1

的重新定義當我嘗試編譯我的代碼我得到:C++:全局變量

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 

對於空間的緣故,我只是包括我包括我的包括和全局變量。如果您希望我發佈整個文件,請告訴我。

這是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; 

這是thread.cpp:

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

這是線程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; 

這是線程主。 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" 

這裏是我的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} 

我用盡了一切我能想到的解決這個問題,但我有麻煩了谷歌搜索找到這個問題的答案。我是C++的新手,但我認爲#pragma once是我需要的。

回答

4

您在thread.h,這將導致該問題有

#include "thread-support.cpp" 

。不要包含它,但要單獨編譯並鏈接它。 (這已經通過Makefile正確完成,因此只需從頭文件中刪除include)

+0

@tehAlgorithmist正如我所說的,您的Makefile已經處理好了,除了刪除行之外,不需要執行任何操作頭文件中的#include「thread-support.cpp」。 –

+0

我對你的答案嗤之以鼻。它造成了更多的錯誤:)...但是我會在回來尋求幫助之前自己嘗試一下。感謝您的幫助。 – janovak

+0

好抓Daniel Larry。應該避免包含源文件(例如c和cpp文件)。 –