【问题标题】:C++: Redefinition of global variableC++:重新定义全局变量
【发布时间】:2013-11-14 21:17:32
【问题描述】:

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

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}

我已尽我所能解决此问题,但我无法通过 Google 搜索找到此问题的答案。我是 C++ 新手,但我认为 #pragma once 是我所需要的。

【问题讨论】:

    标签: c++ redefinition


    【解决方案1】:

    你有

    #include "thread-support.cpp"
    

    在 标题 thread.h 中,这会导致问题。不要包含它,而是单独编译并链接它。 (Makefile已经正确完成了,所以只需从头文件中删除包含)

    【讨论】:

    • @tehAlgorithmist 正如我所说,你的 Makefile 已经处理好了,除了从标题中删除带有 #include "thread-support.cpp" 的行之外,不需要做任何事情。
    • 不错的丹尼尔弗雷。绝对应避免包含源文件(例如 c 和 cpp 文件)。
    • 对于略读您的回答,我深表歉意。它导致了更多错误:)......但我会在回来寻求帮助之前自己尝试这些错误。感谢您的帮助。
    猜你喜欢
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多