cygwin可以编译生成windows下供调用的dll,包括vc可识别的lib。
Cygwin-GCC
Cygwin自带了一个GCC, 用于把C/C++-Source编译成Cygwin平台下的EXE/DLL,
使用Cygwin必须用其自带的GCC, 因为内部会生成一些针对于平台的特定的初始化代码,
如果用其余版本的GCC则很有可能导致程序不能正常运行.
在Cygwin-Shell
中可直接使用gcc, 如下 :
- 编译成DLL 注意Cygwin-GCC 默认导出所有的Function.
gcc -shared xxx.c -o xxx.dll
- 编译成OBJ
gcc -c xxx.c -o xxx.o
- 编译成EXE
gcc xxx.c -o xxx.exe
- 打包多个obj成一个Lib(非链接)
ar r xxx.lib xxx1.o xxx2.o
- 引用外部DLL生成EXE
gcc xxx.c ./L aaa.dll -o xxx.exe
- 链接多个C文件,生成EXE.
gcc xxx.c yyy.c -o zzz.exe
- 引用/bin/include目录下头文件,生成OBJ
gcc -I"/bin/include" -c xxx.c -o xxx.o
- 链接some.a,生成EXE
gcc -L"some.a" xxx.c -o xxx.exe
如果想知道编译时具体的信息, 可以使用--verbose
这个编译选项, 对于了解GCC的工作是很有帮助的.
额外的,也可以写自己的导出Lib文件, 以方便其余程序引用DLL (如Cobol2002编译器就不支持直接引用DLL, 这个时候可以写一个Def, 并导出Lib文件, 便于Cobol2002使用)
具体方法如下:
- 编译生成DLL :
生成MyTest.dll
gcc -shared MyTest.c -o MyTest.dll
- 写一个DEF文件 (这里是MyTest.def), 简单的格式大致如下:
LIBRARY MyTest //这里的MyTest对应于MyTest.dll
EXPORTS
AllocMemory @1 //导出的第一个function : 对应于MyTest.dll里面的AllocMemory(...), 注意, 参数可不用写
ReadMemory @2 //导出的第二个function : 对应于MyTest.dll里面的ReadMemory-Function.
//注意: function可以不用全部导出, 可以只选择你需要的Function.
- 用LIB工具(VC6有提供)生成LIB文件 (这里是MyTest.lib) :
默认生成的名称为MyTest.lib.
lib /def:MyTest.def
- 到此生成完毕, 连接的时候只需要引用MyTest.lib即可, 但要注意XXX.exe 和MyTest.dll要放在同一目录下.
可以用 gcc --help 查看具体的命令 etc...
编辑整理原文: https://www.cnblogs.com/Searchor/p/14473791.html
6.20. How do I link against a .lib file?
If your .lib file is a normal static or import library with C-callable entry points,
you can list foo.lib as an object file for gcc/g++, just like any *.o file.
Otherwise, here are some steps:
Build a C file with a function table
.
Put all functions you intend to use in that table.
This forces the linker to include all the object files from the .lib
.
Maybe there is an option to force LINK.EXE to include an object file.
- Build a dummy 'LibMain'.
- Build a .def with all the exports you need.
- Link with your .lib using link.exe.
or
- Extract all the object files from the .lib using LIB.EXE.
- Build a dummy C file referencing all the functions you need, either with a direct call or through an initialized function pointer.
- Build a dummy LibMain.
- Link all the objects with this file+LibMain.
- Write a .def.
- Link.
You can use these methods to use MSVC (and many other runtime libs) with Cygwin development tools.
Note that this is a lot of work (half a day or so),
but much less than rewriting the runtime library in question from specs...
Thanks to Jacob Navia (root at jacob dot remcomp dot fr) for this explanation.
6.19. How do I use cygwin1.dll with Visual Studio or Mingw-w64?
If you want to load the DLL dynamically, read winsup/cygwin/how-cygtls-works.txt
and the sample code in winsup/testsuite/cygload to understand how this works. The short version is:
-
Make sure you have
4K
of scratch space at the bottom of your stack. -
Invoke
cygwin_dll_init()
:
HMODULE h = LoadLibrary("cygwin1.dll");
void (*init)() = GetProcAddress(h, "cygwin_dll_init");
init();
If you want to link statically from Visual Studio,
to my knowledge none of the Cygwin developers have done this,
but we have this report from the mailing list that it can be done this way:
- Use the
impdef
program to generate a.def
file for the cygwin1.dll (if you build the cygwin dll from source, you will already have a def file)
impdef cygwin1.dll > cygwin1.def
- Use the MS VS linker
lib
to generate an import library
lib /def=cygwin1.def /out=cygwin1.lib
- Create a file "my_crt0.c" with the following contents
#include <sys/cygwin.h>
#include <stdlib.h>
typedef int (*MainFunc) (int argc, char *argv[], char **env);
void
my_crt0 (MainFunc f)
{
cygwin_crt0(f);
}
-
Use
gcc in a Cygwin prompt
to build my_crt0.c into aDLL
(e.g. my_crt0.dll).
Follow steps 1 and 2 to generate.def
and.lib
files for the DLL. -
Download
crt0.c
from the cygwin website and include it in your sources.
Modify it to callmy_crt0()
instead of cygwin_crt0(). -
Build your object files using the MS VC compiler
cl
. -
Link your object files,
cygwin1.lib
, andmy_crt0.lib
(or whatever you called it) into the executable.
Note that if you are using any other Cygwin based libraries that you will probably
need to build them as DLLs using gcc and then generate import libraries for the MS VC linker.
Thanks to Alastair Growcott (alastair dot growcott at bakbone dot co dot uk) for this tip.
编辑整理原文: https://www.cnblogs.com/Searchor/p/14473791.html
Ref
https://cygwin.com/faq.html
http://www.cygwin.com/
失效 http://luckythc.itpub.net/post/31721/458942