@einpoklum 将我的评论转换为答案:@n.18e9 是正确的,因为您必须使用不带任何 -L 或 -l 选项的 lib 文件的完整路径名。
g++ -o foo.exe foo.o c:\something\somethingelse\some.lib。您也可以直接链接到 Windows DLL 文件g++ -o foo.exe foo.o c:\something\somethingelse\some.dll。
重要提示 - 确保您链接到为 64 位平台(在 MSVC 目标 X64,而不是 Win32)生成的 lib 文件(和关联的 dll)。
好的,你想要一个例子,我们走吧。
下面是两个使用 gcc/g++ 链接到导出纯 C 函数的 Windows 原生 DLL 的示例(在 Windows 10 上使用 x86_64-w64-mingw32/8.3.0)。
我以我自己的免费 xmlsq 库为例 https://www.cryptosys.net/xmlsq。
您可以下载核心原生 DLL 和下面引用的所有源代码。确保使用 64 位 DLL。
本机 Windows DLL diXmlsq.dll 完全用纯 C 代码编写,并导出简单的 C 函数(外部“C”)。
特别是,对于本示例,它导出了一个返回整数值的XMLSQ_Gen_Version 函数。
DLL 是使用面向 X64 平台的 MSVC 12.0 编译的。 MSVC生成的关联库文件为diXmlsq.lib。
我应该补充一点,这个 DLL 的工作方式与 Windows“Win32 API”DLL 完全相同,所以这里的说明应该适用于Windows\System32 中的标准 Windows 库(再次确保链接到 64 位版本) .
示例 1。一个普通的 C 接口。
这两个命令在我的系统上编译时没有警告:
> gcc -o test-ver test-ver.c "C:\fullpath\to\x64\diXmlsq.lib"
> gcc -o test-ver test-ver.c "C:\fullpath\to\x64\diXmlsq.dll"
diXmlsq.dll 使用以下定义文件编译。
(您也可以使用__declspec(dllexport))
参考:https://docs.microsoft.com/en-us/cpp/build/exporting-from-a-dll?view=msvc-160
diXmlsq.def
LIBRARY "diXmlsq"
EXPORTS
XMLSQ_Gen_Version
diXmlsq.h - diXmlsq.dll 的 C 接口
#ifdef __cplusplus
extern "C" {
#endif
long __stdcall XMLSQ_Gen_Version(void);
#ifdef __cplusplus
}
#endif
在纯 C 程序中调用核心函数:
test-ver.c
#include <stdio.h>
#include "diXmlsq.h"
int main(void)
{
long n;
n = XMLSQ_Gen_Version();
printf("Version = %ld\n", n);
return 0;
}
示例 2。一个 C++ 接口。
这两个命令都使用 g++ 编译,没有警告。
> g++ -o test-simple test-simple.cpp xmlsq.cpp "C:\fullpath\to\x64\diXmlsq.lib"
> g++ -o test-simple test-simple.cpp xmlsq.cpp "C:\fullpath\to\x64\diXmlsq.dll"
C++ 接口的想法是成为普通 C 库的接口,使用更方便的 STL 类型,如 std::string 和 std::vector。
为简单起见,我们将仅演示 Gen::Version 方法。
C++ 代码摘录如下:
test-simple.cpp - 一个测试 C++ 程序。
#include <iostream>
#include "xmlsq.hpp"
int main()
{
std::cout << "xmlsq::Gen::Version=" << xmlsq::Gen::Version() << std::endl;
}
xmlsq.hpp - C++ 接口
namespace xmlsq
{
class Gen {
private:
Gen() {} // Static methods only, so hide constructor.
public:
/** Get version number of core diXmlsq DLL. */
static int Version();
};
}
xmlsq.cpp - C++ 实现。
#include "diXmlsq.h"
#include "xmlsq.hpp"
namespace xmlsq
{
int Gen::Version() {
int n = XMLSQ_Gen_Version();
return n;
}
}
示例 3。尝试错误地链接到 32 位库。
> gcc -o test-ver test-ver.c "C:\fullpath\to\Win32\diXmlsq.lib"
C:/Strawberry/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
C:\Users\user\AppData\Local\Temp\cce27Dhl.o:test-ver.c:(.text+0xe):
undefined reference to `XMLSQ_Gen_Version'
collect2.exe: error: ld returned 1 exit status