Compiling mono as static library

2019-05-26 04:21发布

I want to compile libmono as static library at Windows.

Target platform is Windows x86. Build environment: Windows 7 64-bit, VC++ Express 2010

What i have already done.

1) Downloaded mono 2.10.8 sources.
2) Opened mono.sln from msvc folder and ensured that everything is compilling.
3) Then i've made some changes:
3.1) General->Project Defaults->Configuration Type: Static library (.lib)
3.2) General->Project Defaults->Use of MFC: Use Standard Windows Libraries
3.3) C/C++->Code Generation->Runtime Library: Multi-threaded (/MT)
4) Built it and VC++ 2010 successfully created mono-2.0.lib
5) Added it in linker inputs of my own project (that i want to embed mono in) with:
5.1) General->Project Defaults->Configuration Type: Application (.exe)
5.2) General->Project Defaults->Use of MFC: Use MFC in a Ststic Library
5.3) C/C++->Code Generation->Runtime Library: Multi-threaded (/MT)

It seems to work near perfect but with some terrible issues: Mysterious behavior of Dictionary<TKey, TSource>

Is everything done correct? Should i specify any other compiler options or preprocessor directives?

P.S.: libmono command line is:

/I"..\libgc\include" /I"..\" /I"..\mono\" /I"..\mono\jit" /I"..\mono\eglib\src" /I"....\mono\eglib\src" /I"..\eglib\src" /Zi /nologo /W1 /WX- /O1 /Ob1 /Oi /Oy- /D "NDEBUG" /D "i386" /D "TARGET_X86" /D "i386" /D "WIN32" /D "_WIN32" /D "WIN32" /D "_WINDOWS" /D "WINDOWS" /D "HOST_WIN32" /D "TARGET_WIN32" /D "_CRT_SECURE_NO_DEPRECATE" /D "GC_NOT_DLL" /D "HAVE_CONFIG_H" /D "WINVER=0x0500" /D "_WIN32_WINNT=0x0500" /D "_WIN32_IE=0x0501" /D "WIN32_THREADS" /D "FD_SETSIZE=1024" /D "default_codegen" /D "MONO_ASSEMBLIES=0" /D "_UNICODE" /D "UNICODE" /GF /Gm- /EHsc /MT /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Fp".\Release/libmono.pch" /Fa"Win32\obj\libmono\" /Fo"Win32\obj\libmono\" /Fd"Win32\obj\libmono\vc100.pdb" /Gd /TC /analyze- /errorReport:queue

UPD:

I've found this discussion which is related to my question http://mono.1490590.n4.nabble.com/Mono-static-library-td3546774.html

Is it still actual? Can i use SGen instead of Boehm? If yes, any tip is very appreciated. And if yes, can i then use mono as a static library with use of sgen?

2条回答
不美不萌又怎样
2楼-- · 2019-05-26 04:23

All is clear for me now.

Hans Passant gave an answer to Mysterious behavior of Dictionary<TKey, TSource> that shows that static linking won't work.

Answer to this question shows that there are no possibilities to choose another GC yet: Compiling Mono from Visual Studio with sgen support

Summarizing, it's understood that nowdays the only solution on windows is dynamic linking

查看更多
爷的心禁止访问
3楼-- · 2019-05-26 04:38

I'm going to skip the details of your question because I suspect an XY problem.

If you want to create an application that is statically linked to the mono runtime, just use mkbundle.exe:

 mcs Main.cs
 mkbundle --static --deps -z Main.exe -o Main
 ldd Main

results in

sehe@mint12:~/Projects/SODemo/SODemo$ mkbundle --static --deps -z Main.exe -o Main
OS is: Linux
Note that statically linking the LGPL Mono runtime has more licensing restrictions than dynamically linking.
See http://www.mono-project.com/Licensing for details on licensing.
Sources: 1 Auto-dependencies: True
   embedding: /home/sehe/Projects/SODemo/SODemo/Main.exe
   compression ratio: 44,62%
   embedding: /usr/lib/mono/4.0/mscorlib.dll
   compression ratio: 34,99%
   embedding: /usr/lib/mono/gac/System/4.0.0.0__b77a5c561934e089/System.dll
   compression ratio: 37,49%
   embedding: /usr/lib/mono/gac/Mono.Security/4.0.0.0__0738eb9f132ed756/Mono.Security.dll
   compression ratio: 40,12%
   embedding: /usr/lib/mono/gac/System.Configuration/4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
   compression ratio: 40,12%
   embedding: /usr/lib/mono/gac/System.Xml/4.0.0.0__b77a5c561934e089/System.Xml.dll
   compression ratio: 34,06%
   embedding: /usr/lib/mono/gac/System.Security/4.0.0.0__b03f5f7f11d50a3a/System.Security.dll
   compression ratio: 39,32%
   embedding: /usr/lib/mono/gac/System.Core/4.0.0.0__b77a5c561934e089/System.Core.dll
   compression ratio: 34,16%
   embedding: /usr/lib/mono/gac/Mono.Posix/4.0.0.0__0738eb9f132ed756/Mono.Posix.dll
   compression ratio: 40,01%
Compiling:
as -o temp.o temp.s 
cc -o Main -Wall `pkg-config --cflags mono-2` temp.c -lz `pkg-config --libs-only-L mono-2` -Wl,-Bstatic -lmono-2.0 -Wl,-Bdynamic `pkg-config --libs-only-l mono-2 | sed -e "s/\-lmono-2.0 //"` temp.o
Done
sehe@mint12:~/Projects/SODemo/SODemo$ ldd Main
    linux-vdso.so.1 =>  (0x00007fff7b1ff000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007ffe95d0f000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007ffe95a8b000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007ffe95882000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ffe9567e000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ffe95461000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ffe950bf000)
    /lib64/ld-linux-x86-64.so.2 (0x00007ffe95f52000)

Note the resulting executable size is: 5.8Mb for a trivial program. But it is completely independent.

See also

查看更多
登录 后发表回答