Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compile C# code as a library instead of an executable?

I have a C# console application in Visual Studio 2010. It has a Main() method as well as a bunch of utility classes. I'd like those utility classes to be available to other solutions. From reading online it seems that I need to compile it as a Class Library (DLL). So here's what I did:

  • Went in Visual Studio to "Project > [ProjectName] Properties > Application" and changed "Output type" from "Console Application" to "Class Library"
  • Rebuilt; ProjectName.dll was created in bin/Debug.
  • Created a new Console Application
  • Solution Explorer > Add Reference > browse to ProjectName.DLL, select it.

However, neither IntelliSense nor the Object Browser could find the classes inside that DLL.

I tried recompiling several different Console Applications as Class Libraries and got the same result. I also noticed that it works if I initially create the solution as a Class Library, but not if I convert it to one later.

Any tips?

like image 895
RexE Avatar asked Jan 22 '11 22:01

RexE


People also ask

What is the command to compile in C?

The Unix command for compiling C code is gcc. This is a compiler from Gnu for Linux. If you are using a Unix machine like Solaris you may need to use the command cc.) When you compile your program the compiler produces a file containing binary code which is directly readable by the machine you are on.

How do I run a C program file?

Opening a file is performed using the fopen() function defined in the stdio.h header file. The syntax for opening a file in standard I/O is: ptr = fopen("fileopen","mode");

Do you need a compiler to run C?

C is a mid-level language and it needs a compiler to convert it into an executable code so that the program can be run on our machine.


1 Answers

You do not need to build it as a dll. VS 2010 (and IIRC 2008) allow referencing exe assemblies. All you need is for they relevant types to be declared public - top-level classes defualt to internal if you don't add a specifier.

like image 191
Marc Gravell Avatar answered Oct 21 '22 10:10

Marc Gravell