如何在Linux或Macos中为Windows编译静态.lib库 [英] How to compile static .lib library for Windows in Linux or Macos
问题描述
我正在寻找在Linux或Macos中为Windows编译静态库的方法,似乎有交叉编译器可以为Windows生成.a库,例如
I am searching way to compile static library for Windows in Linux or Macos, there seems to be cross compiler to generate .a library for Windows like this one, but that is not what I want, what I want is a .lib static library file for Windows, preferably for Visual Studio. I know I can run a Windows virtual machine and using Visual Studio, but that is too heavy, and can't be done in command line.
推荐答案
对于类unix的操作系统(Linux,MacOS等),静态库表示
对象文件的 ar
存档. ar
是GNU通用
目的存档器.不管您将哪种文件粘贴到存档中.它是
仅当它们恰好是目标文件时才将其称为静态库".和
它也是ar
存档的自定义名称,称为*.a
.你可以叫它
*.lib
或其他任何内容.
For unix-like OSes (Linux, MacOS, etc) a static library means
an ar
archive of object files. ar
is the GNU general
purpose archiver. It doesn't care what kind of files you stick into an archive. It's
just the custom to call it "a static library" when they happen to be object files. And
it's also just a custom for an ar
archive to be called *.a
. You can call it
*.lib
, or anything.
对于Visual Studio,静态库表示PE格式目标文件的存档
通常由Microsoft工具 LIB
创建.
For Visual Studio, a static library means an archive of PE-format object files
usually created by the Microsoft tool LIB
.
Microsoft LIB存档的格式实际上与Unix ar
存档的格式相同.微软
很早以前就采用了.
The format of an Microsoft LIB archive is in fact the same as that of a Unix ar
archive. Microsoft
just adopted it, long long ago.
因此,如果您使用发行版的PE交叉编译器在Linux上编译了一些PE对象文件
然后使用ar
将它们归档到*.lib
中,您便拥有了一个静态库,可以在Windows中使用
使用Visual Studio编译器.
So if you compile some PE object files on Linux using your distro's PE cross-compiler
then archive them into a *.lib
with ar
, you've got yourself a static library that's good to go in Windows
with the Visual Studio compiler.
好吧,只要那些目标文件具有C二进制接口即可. 如果它们中的任何一个具有C ++接口,它们都是无用的:Microsoft和GCC C ++编译器使用不同的名称处理协议,否则与ABI不兼容.
Well, you have as long as those object files have C binary interfaces. If any of them have C++ interfaces, they're useless: the Microsoft and GCC C++ compilers use different name-mangling protocols and are otherwise ABI incompatible.
演示
我们从linux开始,提供了一些静态库的源代码:
We start in linux with some source code for the static library:
hello.c
#include <stdio.h>
void hello(void)
{
puts("Hello world");
}
交叉编译:
$ x86_64-w64-mingw32-gcc-win32 -o hello.obj -c hello.c
制作静态库:
$ ar rcs hello.lib hello.obj
然后将要与hello.lib
链接的程序:
Then a program that's going to be linked with hello.lib
:
main.c
extern void hello(void);
int main(void)
{
hello();
return 0;
}
现在,我们跳到Windows 10 VM中,在其中查看已保存的文件 只是通过共享文件夹创建的:
Now we hop into a Windows 10 VM where we're looking at the the files we've just created through a shared folder:
E:\develop\so\xstatlib>dir
Volume in drive E is VBOX_imk
Volume Serial Number is 0000-0804
Directory of E:\develop\so\xstatlib
03/12/2017 18:37 72 main.c
03/12/2017 18:29 978 hello.lib
03/12/2017 18:26 66 hello.c
03/12/2017 18:27 832 hello.obj
4 File(s) 1,948 bytes
0 Dir(s) 153,282,871,296 bytes free
编译并链接我们的程序:
Compile and link our program:
E:\develop\so\xstatlib>cl /Fehello.exe main.c hello.lib
Microsoft (R) C/C++ Optimizing Compiler Version 19.11.25547 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
main.c
Microsoft (R) Incremental Linker Version 14.11.25547.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:hello.exe
main.obj
hello.lib
运行它:
E:\develop\so\xstatlib>hello
Hello world
这篇关于如何在Linux或Macos中为Windows编译静态.lib库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!