1

コードをコンパイルしようとすると、次のようになります。

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"

これは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;

これは、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"

これが私のメイクファイルです:

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

1 に答える 1

4

あなたが持っている

#include "thread-support.cpp"

問題の原因となるヘッダーで。 thread.hインクルードせず、個別にコンパイルしてリンクしてください。(これは Makefile によって既に適切に行われているため、ヘッダーからインクルードを削除するだけです)

于 2013-11-14T21:19:33.060 に答える