问题描述
我有一个 GlobalVars.h 文件,我在其中声明了一些我想从其他 cpp 文件中使用的变量(互斥体、条件变量、整数...)。在那些 cpp 文件中,我将 GlobalVars.h 包含在包含保护中,当我编译整个项目时,它给了我下一个错误:
g++ -o exec/Manager obj/Manager.o -pthread -std=c++11 obj/PaySystem.o
/usr/bin/ld: obj/PaySystem.o:(.bss+0x40): multiple deFinition of `semmutexPay'; obj/Manager.o:(.bss+0x0): first defined here
/usr/bin/ld: obj/PaySystem.o:(.bss+0x80): multiple deFinition of `waitPayFinish'; obj/Manager.o:(.bss+0x40): first defined here
/usr/bin/ld: obj/PaySystem.o:(.bss+0x210): multiple deFinition of `counter'; obj/Manager.o:(.bss+0x1d0): first defined here
...
std::mutex semmutexPay;
std::mutex waitPayFinish;
int counter;
PaySystem.cpp:
#ifndef __VARS_H__
#define __VARS_H__
#include "GlobalVars.h"
#endif
void PaySystem::operator()() {
while(true) {
std::unique_lock<std::mutex> ulPayQueue(semmutexPay);
if (counter > 5)
doOtherStuff();
}
}
Manager.cpp:
#ifndef __VARS_H__
#define __VARS_H__
#include "GlobalVars.h"
#endif
#ifndef __PAY_SYstem_H__
#define __PAY_SYstem_H__
#include "SistemaPago.h"
#endif
int main(int argc,char *argv[]) {
PaySystem ps;
std::thread paySystemThread(ps);
doSomeStuff();
}
void someMethod() {
counter++;
}
DIROBJ := obj/
DIREXE := exec/
DIRHEA := include/
DirsRC := src/
CFLAGS := -I $(DIRHEA) -c -Wall -ansi
LDLIBS := -pthread -std=c++11
CC := g++
all: dirs PaySystem Manager
dirs:
mkdir -p $(DIROBJ) $(DIREXE)
Manager: $(DIROBJ)Manager.o
$(CC) -o $(DIREXE)$@ $^ $(LDLIBS) $(DIROBJ)PaySystem.o
PaySystem: $(DIROBJ)SistemaPago.o
$(DIROBJ)%.o: $(DirsRC)%.cpp
$(CC) $(CFLAGS) $^ -o $@ $(LDLIBS)
clean:
rm -rf *~ core $(DIROBJ) $(DIREXE) $(DIRHEA)*~ $(DirsRC)*~
我尝试以其他方式进行包含保护,但没有任何效果。 此外,我尝试使用 exter 关键字在 GlobalVars.h 中声明变量,但它给了我同样的错误。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)