Reputation: 67
This is a Qt project which, once built, results in a dll AnTS_Core.dll so I have:
AnTs_Core.cpp
#include <windows.h>
#include "Globals.h" // my global values
extern "C"
{
__declspec(dllexport) void load();
}
void load()
{
mainDispatcher = new Dispatcher();
}
The Global header file which contains all main objects as global(because I want to call an object method from an other object):
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
The Dispatcher : header file
#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
Dispatcher.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()
{
}
and the device (Dispatcher contains a list of devices)
Device.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
Device.cpp
#include "../Includes/Device.h"
#include <string.h>
#include <iostream>
#include <cstdio>
#include "Globals.h"
Device::Device(char* dllPath)
{
}
The errors are:
LNK2005 _mainDispatcher already defined in AnTS_Core.cpp.obj
LNK1169 one or more multiply defined symbols found
The errors disappear when I comment the line #include "Globals.h"
in Device.cpp. But I want to access to global variables (to an other Dispatcher for example or to another object) from the device.cpp file.
Upvotes: 0
Views: 277
Reputation: 11944
So, this is a classical declaration vs definition issue - you have defined the variable mainDispatcher
in the header and thus every compilation unit that includes this header winds up with a definition, where what you want is to declare the variable in the header as extern
(this will only inform every compilation unit that includes the header that such variable exists):
#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`
And you should place the actual definition Dispatcher* mainDispatcher
in one of your .cpp
files.
Upvotes: 1
Reputation: 986
You have Dispatcher *mainDispatcher;
in your Globals.h
, this way every compilation unit which includes this header creates its own instance of this symbol. Declare extern Dispatcher *mainDispatcher;
in Globals.h
, and add Dispatcher *mainDispatcher;
in AnTs_Core.cpp
. This way you will have one symbol for AnTs_Core.cpp
compilation unit, but others will see it through extern declaration.
Upvotes: 0