2

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
ILya
  • 2,670
  • 4
  • 27
  • 40
  • 1
    This is an odd question, considering that your previous question showed how you shot your foot by running mono statically linked. http://stackoverflow.com/questions/10717406/mysterious-behavior-of-dictionarytkey-tsource – Hans Passant May 23 '12 at 13:45

2 Answers2

4

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

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thank you for this excursion into bundles but it's completely different to what i need and to what is in my question. Why you call it XY problem? I've made an attempt. It doesn't worked for me. So i asked for corrections describing steps attempted. I don't need bundling managed assemblies into my executable. I need static linking to libmono. I'm agree to edit my question if it's unclear but now i don't see it is. – ILya May 23 '12 at 13:50
  • @ILya your question is clear. The goal isn't. Note that the resulting executable **is** statically linked to libmono. Note also that you can repeat the exercise **without** `--deps` to compile a single assembly down to a native library instead of a full exe. Does that help? – sehe May 23 '12 at 14:43
  • I don't need to compile any assembly =) Lets say i want mono.exe but winth additional functionality and without separate mono-2.0.dll, i.e. single file executable that i can use in a way "C:\myapp\mymono.exe D:\dot-net-assembly.exe". And i already have one that works except Dictionary<,> classes used in dot-net-assembly.exe randomly throw strange exceptions. It's caused by GC issues. If you are interested please look at my own answer for details. – ILya May 23 '12 at 17:54
  • Aha. That would be like mono embedding but statically linked. Mmm. Thinking about it – sehe May 23 '12 at 18:21
  • Exactly =) Unfortunately, due to GC design it's unachiveable – ILya May 23 '12 at 18:24
2

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

Community
  • 1
  • 1
ILya
  • 2,670
  • 4
  • 27
  • 40
  • It is strange. I would downvote this as not-an-answer. But, as the OP you are 'sort of' entitled to bend the rules and alter your own question. Ok then. – sehe May 23 '12 at 18:23
  • It's your right, but i think it answers all the questions. 1) everithing that was done is correct; 2) I shouldn't add any other flags as everything is correct; 3) The situation that Boehm uses DllMain is still actual; 4) I can't use SGen instead of Boehm on Windows yet. Every question is answered and answers are contained here. In my oppinion of course. – ILya May 23 '12 at 18:29
  • Well, except, maybe, that the question title is "Compiling mono as static library". I humbly offer that 'the only solution on windows is dynamic linking' cannot be considered a real answer. Anyways, no need to defend, [I defended you in my first comment](http://stackoverflow.com/questions/10719828/compilling-mono-as-static-library/10721707#comment13931419_10721707) :) – sehe May 23 '12 at 18:38
  • I didn't 100% understand it because my English is still imperfect) Thank you) – ILya May 23 '12 at 18:42