函数指针多重定义
Function Pointer multiple definition
我有以下代码:
Header.hpp:
#ifndef HEADER_HPP_INCLUDED
#define HEADER_HPP_INCLUDED
#include<Windows.h>
extern HMODULE Module;
extern "C" bool __stdcall Initialized(void);
void (__stdcall *ptr_DetourPoint) (int X, int Y);
#endif //HEADER_HPP_INCLUDED
Header.cpp:
#include "Header.hpp"
HMODULE Module = nullptr;
bool __stdcall Initialize(void)
{
Module = LoadLibrary("Point.dll");
ptr_DetourPoint = reinterpret_cast<(__stdcall *)(int, int)>(GetProcAddress(Module, "PointFunc"));
return ptr_DetourPoint != nullptr;
}
extern "C" __stdcall HookDetourPoint(int X, int Y)
{
//Do stuff here..
ptr_DetourPoint(X, Y);
}
main.cpp:
#include <windows.h>
#include "Header.hpp"
extern "C" bool __stdcall DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
Initialized();
DisableThreadLibraryCalls(hinstDLL);
break;
case DLL_PROCESS_DETACH:
break;
default:
break;
}
return true;
}
在上面,当我使用Mingw 4.8编译它时,我得到:
objReleasemain.o:main.cpp:(.bss+0xb78): multiple definition of `ptr_DetourPoint'
objReleaseImplementationsHeader.o:Header.cpp:(.bss+0xb80): first defined here
知道为什么我得到这个吗?我不想为函数指针定义类型
简短的回答是ptr_DetourPoint
声明了一个全局函数指针,另一个类似Module
的数据块。要修复它,您也可以将其标记为"extern"。但我怀疑你需要在你的标题中暴露它,因为它似乎只是header.cpp
中的实现细节。
该变量在头文件中定义,这意味着任何包含它的源文件都定义了该符号。结果是将main.cpp和Header.cpp链接在一起定义了两次ptr_DetourPoint。您只需要在一个源文件中定义它,如果其他文件需要看到它,则在头文件中将其声明为extern。
相关文章:
- 用户定义函数中的指针和输入
- 使用"Task"函数指针队列定义作业管理器
- 通过函数指针定义类范围之外的方法
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 如何重新定义 C++ 指针函数?
- 返回对未定义指针的引用
- 为什么此自定义指针类会崩溃?
- 如何定义指针铸件操作员
- 定义指针随机崩溃该程序
- 在不同的文件(C++)中定义指针数组长度
- 如何定义指针和动态内存分配的模板
- 具有自定义指针类型的unique_ptr:*get() 和运算符*() 给出不同的输出
- 定义指针时不显示输出;
- 自定义指针类型和容器/分配器类型定义
- 并排定义指针和类型问题
- 具有自定义指针类型的内存分配器
- 正在对unordered_set的自定义指针类型进行哈希处理
- 查找全局定义指针指向的地址,该地址使用了堆分配
- 如何有效地计算已定义指针的数量
- 用户定义指针队列中的分段错误