27

I turned my C++ Dynamic link library into Static library just to acquire more knowledge. My question is how can I use the .obj file to compile both projects with C# express/MS visual studio?

5 Answers 5

34

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

Sign up to request clarification or add additional context in comments.

6 Comments

I have to use a DLL,but Jon Skeet doesn't have to,he can compile anything. :) Thanks Jon.
This must be possible with runtime linking to the static library and somehow interpret the static library's binary. I don't know C# but I'm pretty sure one can read binary files with it.
@Radek: And how do you propose to then run the code? There may be ways of doing it if you're willing to use various unmanaged functions, but at that point you're barely using C# any more.
embedded virtual machine. Another way is to translate it into a DLL (probably with reassembling, relinking, whatever) just before using it, but then it's indeed not static anymore. It is difficult, but certainly possible.
@Radek: I think for most practical purposes, saying "you can't do it" is accurate. How far does it have to go before you'd accept "you can't do it" - if you had to build your own CLR with extra hooks, would that count?
|
20

The way to "use" a static library in C# is to first create a Managed C++ wrapper that will provide a facade for the rest of the managed world. As everyone else has already commented, C# only supports DLLs for P/Invoke.

1 Comment

Conceptually this makes sense but assume the static libs were third party and compiled with /MTd, how do you cross into /MDd land required for /CLI?
5

"Static library" means that the library is going to be merged with your final application. This concept doesn't exist in .net. .net supports DLLs only.

Comments

2

To update the outdated answer. If using IL2CPP Scripting backend rather than Mono, it's now possible to link against static libs because IL2CPP uses ahead of time compilation while Mono uses Just in time compilation.

Comments

1

No way to do that.

Only call DLL functions in runtime or create a COM object from your library

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.