Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
Hi,

We have unmanaged c++ static library which I wanted to use in C# code. But when I tried it wasn't working. Then I try to search on Google and came to know it is not possible to directly use static library with C#, but I did not find reason behind it, can you please suggest why I cannot use static lib with C#.

Regards
Posted

No, you can't access static libraries directly from C#. You have to use a DLL. 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

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.

Hope, it clears your query.
 
Share this answer
 
Comments
[no name] 1-Jul-12 12:56pm    
Thanks 5!
Sergey Alexandrovich Kryukov 1-Jul-12 18:29pm    
Correct, my 5.

I added some brief speculations explaining why not .LIB files, even though it is intuitively pretty much obvious for those who know how linking and loading works. Please see.
--SA
In addition to the correct answer by GanesanSenthilvel.

He did not explain why .LIB modules are not imported by .NET, only DLLs. Essentially, the .LIB files are just collections of the contents of .OBJ file, something to keep the object files in a single "library". The answer is: .LIB or .OBJ file do not exist during run-time, only DLL modules do. Besides, due to historical reasons, .LIB files are not well standardized, which is explainable, because they serve as intermediate files, and their details are not critical for compatibility of executable modules, legacy or contemporary ones (DLL is one kind of executable modules) with the platform.

—SA
 
Share this answer
 
v2
Comments
[no name] 2-Jul-12 0:34am    
It helps thanks. 5!
Sergey Alexandrovich Kryukov 2-Jul-12 1:24am    
You are very welcome.
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900