我必须从我的 .cpp 和 .h 文件创建一个静态链接的独立 .exe 文件。
我需要克服的唯一障碍是能够m_pListAll()
从两个 .cpp 文件中调用相同的函数,main.cpp
并且window.cpp
(定义一个名为 的类Window
)。
唯一的问题是(由于未知原因)我不能#include
定义 m_peDO() 两次的头文件main.cpp
,window.cpp
我只能做一次,因为头文件设置了一个叫做“动态链接”的东西,有一些奇怪的东西叫做HINSTANCE
(错误:实际原因在答案部分):
//linking.h
// This is an extra header file for dynamic linking of the LabJackUD driver.
// support@labjack.com
#ifdef __cplusplus
extern "C"
{
#endif
//For dynamic linking, we first define structures that have the same format
//as the desired function prototype.
typedef LJ_ERROR (CALLBACK *tListAll) (long, long, long *, long *, long *, double *);
//Define a variable to hold a handle to the loaded DLL.
HINSTANCE hDLLInstance;
//Define variables for the UD functions.
tListAll m_pListAll;
#ifdef __cplusplus
} // extern C
#endif
还有更多功能,但假设我想在 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().
为什么我要使用这个奇怪的 HINSTANCE 东西?与我的 .lib 文件与 minGW 不兼容有关。如果我将库静态添加为编译的一部分,我会得到: :-1: 错误:没有规则可以制作目标 'C:/Users/Joey/Documents/ValvePermissions/libLabJackU.a',这是 'release\ValvePermissions 所需的。可执行程序'。停止。
我应该怎么办?我只是希望该函数在 window.cpp 的范围内,但由于错误,我不想使用该标头两次。