Qt:找到多个定义的符号
Qt : multiple defined symbols found
这是一个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 声明看到它。
相关文章:
- vscode g++链路故障:体系结构x86_64的未定义符号
- 为函数定义符号不明确的指针参数
- 体系结构x86_64的未定义符号:std:terminate(),typeinfo,运算符delete[],运算符new
- C++ 中 dlsym 的未定义符号
- 仅在 MacOS 上析构函数的未定义符号
- OSX clang++:用于 cpp 文件中显式实例化模板的体系结构x86_64的未定义符号
- PCL 出错:体系结构x86_64 @pcl的未定义符号
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 体系结构x86_64的未定义符号:链接器错误
- C++ std::vector<int> 体系结构的未定义符号 x86_64:
- 生成文件:体系结构x86_64的未定义符号
- libc++abi未定义符号:_ZTVN10__cxxabif120__si-class_type_infoE
- Xcode"体系结构x86_64的未定义符号"
- 体系结构x86_64的未定义符号:找不到原因
- SWIG链接器:未定义符号:_ZN2cv8fastFreeEPv(cv::fastFree(void*))
- 如何修复架构x86_64的未定义符号,Boost Asio 1.58
- 在Qt5上去静态库:架构的未定义符号x86_64:
- 体系结构x86_64的未定义符号:c++/ h 文件和 cpp 文件
- 为什么链接器报告全局函数的乘法定义符号,而不是类静态方法
- 监视 SDK 下的未定义符号"__Unwind_SjLj_Unregister"