Mingw 奇怪的标题可以';不能同时包含在main.cpp和window.cpp(a类)中

Mingw 奇怪的标题可以';不能同时包含在main.cpp和window.cpp(a类)中,mingw,static-linking,redefinition,.lib,hinstance,Mingw,Static Linking,Redefinition,.lib,Hinstance,我必须从.cpp和.h文件创建一个静态链接独立.exe文件 我需要克服的唯一障碍是能够从两个.cpp文件main.cpp和window.cpp(定义一个名为window)调用相同的函数mplistall()) 唯一的问题是(出于未知原因)我不能#包含头文件,该头文件在main.cpp和window.cpp中定义了两次m#u peDO(),我只能执行一次,因为头文件设置了一个称为“动态链接”的东西,其中有一个奇怪的东西叫做HINSTANCE(错误:实际原因在回答部分): 还有更多函数,但让我们假设

我必须从.cpp和.h文件创建一个静态链接独立.exe文件

我需要克服的唯一障碍是能够从两个.cpp文件
main.cpp
window.cpp
(定义一个名为
window
)调用相同的函数
mplistall()

唯一的问题是(出于未知原因)我不能
#包含
头文件,该头文件在
main.cpp
window.cpp
中定义了两次m#u peDO(),我只能执行一次,因为头文件设置了一个称为“动态链接”的东西,其中有一个奇怪的东西叫做
HINSTANCE
(错误:实际原因在回答部分):

还有更多函数,但让我们假设我想在main.cpp和window.cpp中使用tListAll m_pListAll。我的Qt项目包含以下文件:

main.cpp //main code
window.h //header defining a class used in main
window.cpp //cpp thing defining that class's methods
peripheral.h //header file to control my peripheral device
peripheral.lib //library file to control my peripheral device (VC6 only not minGW?!)
linking.h //thing that solves(?) the minGW/VC6 library incompatibility (read on)

Scenario 1) #include <linking.h> in **only** main.cpp
Outcome: m_pListAll() only in scope of main.cpp, out of scope for window.cpp
Scenario 2) #include <linking.h> in **both** main.cpp AND window.cpp
Outcome: Redefinition errors for redefining hDLLInstance and m_pListAll().
main.cpp//main代码
window.h//定义在main中使用的类的头
window.cpp//cpp定义该类方法的东西
peripheral.h//头文件,用于控制我的外围设备
peripheral.lib//用于控制我的外围设备的库文件(仅限VC6而非minGW?!)
链接.h//解决(?)minGW/VC6库不兼容问题的方法(请继续阅读)
场景1)#仅包含在**main.cpp中
结果:m_pListAll()仅在main.cpp的范围内,不在window.cpp的范围内
场景2)#包括在**main.cpp和window.cpp中
结果:重新定义hDLLInstance和m_pListAll()的重新定义错误。
我为什么要用这玩意儿?我的.lib文件与minGW不兼容。如果我将库添加为静态编译的一部分,我会得到: :-1:错误:没有规则使“release\ValvePermissions.exe”需要目标“C:/Users/Joey/Documents/ValvePermissions/libLabJackU.a”。停下来


我该怎么办?我只希望该函数在window.cpp的范围内,但由于错误,我不想使用该标题两次。

您的问题是您在标题中定义了
hDLLInstance
m_pListAll
,而不是简单地声明它们;因此,包含此头的每个翻译单元都将创建这些变量的公共实现,所有这些变量在链接时发生冲突

您需要将
extern
关键字添加到标题中的每个定义中,从而将它们转换为声明,并将原始定义(即不带
extern
关键字)复制到仅一个翻译单元中(
main.cpp
),因此,在链接时只定义了一个实现。因此,在标题中,您有:

//Declare a variable to hold a handle to the loaded DLL.
extern HINSTANCE hDLLInstance;

//Declare variables for the UD functions.
extern tListAll m_pListAll;
main.cpp
中(例如):

//Declare a variable to hold a handle to the loaded DLL.
extern HINSTANCE hDLLInstance;

//Declare variables for the UD functions.
extern tListAll m_pListAll;
//Define a variable to hold a handle to the loaded DLL.
HINSTANCE hDLLInstance;

//Define variables for the UD functions.
tListAll m_pListAll;