How to Compile C# Application with C++ Static Library

How to compile C# application with C++ static library?

No, you can't access static libraries directly from C#. You have to use a DLL.

C# Static Libraries

The closest you can come is ILMerge - but do you really need this? Unless you have an application which you can deploy as a single executable, it doesn't much matter whether you have one DLL or ten. I would suggest that for most cases, you should just keep them as normal class libraries.

Note that ILMerge isn't quite the same as "static linking" - it still does the job of bundling all the code into a single file, but the linking process in .NET is fundamentally somewhat different to the one for native code.

Static Linking of libraries created on C# .NET

ILMerge is what you're after.

I'm not sure I'd really call this "static linking" - it's just merging several assemblies into one. (In particular, please don't get the impression that this is building a native, unmanaged executable.) However, I think it's what you're after :)

Update: ILMerge is now open source and is also available as a NuGet package:

Install-Package ilmerge 

Statically linking a C++ library to a C# process using CLI or any other way

Unique among other managed languages, C++ allows for mixed mode - a combination of managed (C++/CLI) and native C++ within the same compilation unit (dll/exe/lib), with calls back and forth. Maybe you can leverage that, create a kind of a glue layer. I've never tried though.

The key is the /clr compiler switch - you apply it to some files in the project but not others. Then you create some classes/functions as managed. The unmanaged bits can see them and call them, and vice versa. Passing primitive types around is done transparently, for strings there's some marshaling trickery. I'll be able to post more on Monday.

EDIT: seems like some deep magic would be required. Not on the mixed C++ side - on the linking the result to C# side. The Visual Studio IDE does not readily support the scenario, you see. Chances are, the regular build process won't be of any use.

EDIT2: you can compile your C++ bits to a .netmodule by specifying a /LN command line option to the compiler and /NOASSEMBLY to a linker. Now, to link that to the C# exe...

The .NET equivalent of static libraries?

If you are annoyed at all the DLLs, download ILMerge. I use this to bundle together multiple DLL's into an easy-to-use .EXE for my clients.

Pinvoking functions from a static C library

A static library is used by linking it into a larger module. In Windows that means a DLL or an executable.

They do not stand alone and only make sense when you link them into a larger module. So, that would imply that, in order to use pinvoke you need to build a DLL which includes the library, and pinvoke to that DLL.

As an alternative to pinvoke, you could make a mixed mode C++/CLI assembly. Link the static library to that C++/CLI assembly and expose the functionality via a C++ managed class. That managed class can then be consumed by your C# code.

Should we place C code in Static library or Runtime component?

There isn't any real difference between the two approaches. A static library is nothing but a collection of .obj files, the exact same kind of .obj files that you'll get from approach #2. After the linker is done, there won't be any difference in the result.

That's when everything is perfect, an ideal that can be very difficult to achieve when you use open source C code. An advantage of a static .lib is that it improves build time, not having to re-generate the .obj files. But that's also their disadvantage, you'll shoot your foot if you use a .lib that was created by somebody else and he didn't use the same compiler version or compile options. The simplest example of such a trap is building your Debug version and the .lib was built for Release. Or if it uses winapi functions that are verboten in a Phone app, pretty common. So #3 is the best way to avoid problems, build the .lib yourself so you can control all the compile and link settings. Do beware however that it can be very difficult to get open source C code to build, it often comes with a very extensive configuration script, designed to deal with the differences between the many architecture and Unix variants.



Related Topics



Leave a reply



Submit