Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:全局变量的重新定义_C++_Redefinition - Fatal编程技术网

C++ C++;:全局变量的重新定义

C++ C++;:全局变量的重新定义,c++,redefinition,C++,Redefinition,当我尝试编译代码时,我得到: 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’ t

当我尝试编译代码时,我得到:

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
为了节省空间,我只包含了include和global变量。如果你想让我发布整个文件,请告诉我

这是线程。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"
我已经想尽一切办法来解决这个问题,但是我在谷歌上很难找到这个问题的答案。我是C++新手,但我认为<>代码>语法>是我需要的。

你有

#include "thread-support.cpp"
标题中
thread.h
,这会导致问题。不包括它,但单独编译并链接它。(Makefile已经正确地完成了这项工作,因此只需从标题中删除include即可)

您已经完成了

#include "thread-support.cpp"

标题中
thread.h
,这会导致问题。不包括它,但单独编译并链接它。(这已经由Makefile正确完成,所以只需从标题中删除include)

@tehAlgorithmist正如我所说的,您的Makefile已经解决了这一问题,除了从标题中删除带有
#include“thread support.cpp”
的行之外,不需要做任何其他事情。丹尼尔·弗雷,干得好。当然应该避免包含源文件(例如c和cpp文件)。我很抱歉略读了您的答案。它导致了更多的错误:)…但我会在回来寻求帮助之前自己尝试这些错误。谢谢您的帮助。@tehAlgorithmist正如我所说,您的Makefile已经解决了这个问题,除了从标题中删除包含“thread support.cpp”的行之外,不需要做任何其他事情。丹尼尔·弗雷,祝您好运。当然应该避免包含源文件(例如c和cpp文件)。我很抱歉略读了您的答案。它导致了更多的错误:)…但我会在回来寻求帮助之前自己尝试这些错误。谢谢你的帮助。
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}
#include "thread-support.cpp"