gpt4 book ai didi

c++ - Qt : multiple defined symbols found

转载 作者:行者123 更新时间:2023-11-28 06:17:32 24 4
gpt4 key购买 nike

这是一个 Qt 项目,一旦构建,就会生成一个 dll AnTS_Core.dll所以我有:

AnTs_Core.cpp

#include <windows.h>
#include "Globals.h" // my global values
extern "C"
{
__declspec(dllexport) void load();
}
void load()
{

mainDispatcher = new Dispatcher();
}

包含所有主要对象的全局头文件作为全局对象(因为我想从其他对象调用对象方法):

全局变量.h:

#ifndef GLOBALS_H
#define GLOBALS_H
#include "AnTS_Types.h"
#include "Dispatcher.h"
#ifdef __cplusplus
extern "C"
{
#endif
Dispatcher *mainDispatcher;
#ifdef __cplusplus
}
#endif
#endif // GLOBALS_H

调度程序:头文件

#ifndef DISPATCHER_H
#define DISPATCHER_H
#include "AnTS_Types.h"
#include "Device.h"
#include <list>
#include <windows.h>
class Dispatcher
{
public:
Dispatcher();
~Dispatcher();
private:
std::list<Device*> _devices;
};
#endif

调度程序.cpp:

#include "Dispatcher.h"
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string.h>
#include <dirent.h>
#include <regex>
#include "Device/DEV_Struct.h"
Dispatcher::Dispatcher()
{
}

和设备(Dispatcher 包含设备列表)

设备.h

#ifndef DEVICE_H
#define DEVICE_H

#include <windows.h>
#include "Device/DEV_Struct.h"
#include "AnTS_Types.h"
#define ANTS_DEVICE_NAME_LENGHT 64
class Device
{
public:
Device(char*);
~Device();
};
#endif // DEVICE_H

设备.cpp

#include "../Includes/Device.h"
#include <string.h>
#include <iostream>
#include <cstdio>
#include "Globals.h"

Device::Device(char* dllPath)
{
}

错误是:

LNK2005 _mainDispatcher already defined in AnTS_Core.cpp.obj

LNK1169 one or more multiply defined symbols found

当我在 Device.cpp 中注释行 #include "Globals.h" 时,错误消失了。但是我想从 device.cpp 文件访问全局变量(例如另一个 Dispatcher 或另一个对象)。

最佳答案

所以,这是一个经典的 declaration vs definition问题 - 您已经在 header 中定义了变量 mainDispatcher ,因此包含此 header 的每个编译单元最终都有一个定义,您想要的是将 header 中的变量声明为 extern (这只会通知包含此类变量存在的 header 的每个编译单元):

#ifndef GLOBALS_H
#define GLOBALS_H
#include "AnTS_Types.h"
#include "Dispatcher.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern Dispatcher *mainDispatcher;
#ifdef __cplusplus
}
#endif
#endif // GLOBALS_H`

并且您应该将实际定义 Dispatcher* mainDispatcher 放在您的 .cpp 文件之一中。

关于c++ - Qt : multiple defined symbols found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29964442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com