这是一个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();
}
包含所有主要对象的全局头文件(因为我想从另一个对象调用对象方法):
Globals.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()
{
}
和设备(调度程序包含设备列表)
设备.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已在 AnTS_Core.cpp.obj 中定义
LNK1169找到一个或多个多重定义的符号
当我在设备.cpp中注释行#include "Globals.h"
时,错误消失了。但是我想从设备.cpp文件中访问全局变量(例如,访问其他调度程序或其他对象)。
所以,这是一个经典的声明与定义问题 - 你已经在标头中定义了变量mainDispatcher
,因此包含此标头的每个编译单元最终都有一个定义,其中您想要的是将标头中的变量声明为 extern
(这只会通知包含标头的每个编译单元存在此类变量):
#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
文件中。
Globals.h
中Dispatcher *mainDispatcher;
,这样每个包含这个头的编译单元都会创建自己的这个符号的实例。在 Globals.h
中声明extern Dispatcher *mainDispatcher;
,并在AnTs_Core.cpp
中添加Dispatcher *mainDispatcher;
。这样,您将有一个符号用于AnTs_Core.cpp
编译单元,但其他人将通过 extern 声明看到它。