310

I created a new project in Visual Studio 2010 and noticed that there are now two new folders named obj and bin in my project directory.

A similar pair of folders are created when building and debugging - what are these folders for?

Justin
  • 84,773
  • 49
  • 224
  • 367
yesraaj
  • 46,370
  • 69
  • 194
  • 251

6 Answers6

322

The obj folder holds object, or intermediate, files, which are compiled binary files that haven't been linked yet. They're essentially fragments that will be combined to produce the final executable. The compiler generates one object file for each source file, and those files are placed into the obj folder.

The bin folder holds binary files, which are the actual executable code for your application or library.

Each of these folders are further subdivided into Debug and Release folders, which simply correspond to the project's build configurations. The two types of files discussed above are placed into the appropriate folder, depending on which type of build you perform. This makes it easy for you to determine which executables are built with debugging symbols, and which were built with optimizations enabled and ready for release.

Note that you can change where Visual Studio outputs your executable files during a compile in your project's Properties. You can also change the names and selected options for your build configurations.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 4
    For whatever reason, my project doesn't have a debug or release sub-folder for the obj and bin folders. If I edit my project settings to build to the debug/release sub-folder depending on the currently selected configuration, i get an error that the data type couldn't be created when I debug my application. My app only ever looks in the bin folder for the dlls and never knows to look in the debug or release folder. How do I correct this? – Anil Aug 21 '13 at 20:54
  • Just to clarify a typo, I do have a debug and release folder in my obj folder, but no debug or release folder in the bin folder. – Anil Aug 21 '13 at 21:06
  • 3
    @Sly I don't understand what you're asking. A project builds either an application (EXE) or a library (DLL). It cannot build both. So if your project creates an application, it will not place any DLLs into your bin folder. And if your project builds a library, you'll get an error when you try to debug it because you cannot execute a DLL. Anyway, it sounds like you've changed the default output path in your project's properties. If you need more help, ask a new question. Be sure to document the problem well, including screenshots of your project configuration if necessary. – Cody Gray - on strike Aug 22 '13 at 09:14
  • 2
    I wonder if I can force visual studio delete obj folder after build is compelted? – Johnny_D Oct 27 '13 at 20:01
  • I didn't see any harm in deleting obj folder after build – Interstellar Mar 04 '14 at 12:38
  • 2
    @SlyRaskal One reason why you might not have Debug/Release folders is if you're developing a web project? – Tim Iles Jan 02 '15 at 14:30
  • @CodyGray, So basically you are saying that **both** `bin` and `obj` folders can be deleted anytime anywhere anyhow? – Pacerier Jan 03 '15 at 01:19
  • @Pacerier Yes, but with potential impacts. VS can reuse obj files during incremental builds, so your builds may be a bit slower if you delete the obj folder. If you have project references to binaries under the bin folder created by VS (unless it's a setup project making the reference this is a bad practice IMHO... much better to have the build copy binaries to a shared folder) then depending on build order you may break these references. – BitMask777 Jan 15 '15 at 20:00
  • 1
    @BitMask777 Yeah, might be a slower, but if you've got a problem that you **CANNOT** find the answer to, methodically delete all the bin + obj folders. It's "fixed" a problem for me more than once. – FuriousFolder Dec 17 '15 at 22:02
  • 3
    Does `obj` really only contain _unlinked_ object code files? From my experience, it contains _all_ of them, and at the final build step, the files that are part of the "final set" are _copied_ into `bin`. – ivan_pozdeev Jan 06 '16 at 03:24
  • 1
    @ivan The C++ toolchain doesn't behave that way. It might generate the binaries temporarily in `obj` and *move* them into `bin` (I haven't verified), but there certainly are no binary files left in my `obj` folder after a compile. Not sure what C# does anymore, been too long. – Cody Gray - on strike Jan 27 '16 at 05:56
  • @CodyGray Would I ever want to use or link the `obj` folder? Based on your answer it seems like it's only used by VS and has no practical usage for programmers? – Alexander Derck Aug 21 '18 at 07:58
  • 1
    Kind of a loaded question, @Alexander, as it depends on what you mean by "practical usage for programmers". If your build process consists of clicking the toolbar button in Visual Studio, then no: you don't ever need `obj`. But if your build process is more complicated, like you have multiple dependencies, then `obj` is very useful. – Cody Gray - on strike Feb 02 '19 at 06:55
  • @CodyGray I was facing an issue with one project thus I decide to delete the contect of both folders (bin & obj). as soon as the project run the deleted files recreated and problem solved. do you know why this happened? – Error 1004 Jan 27 '23 at 12:36
  • @Error1004 No, there isn't enough information/details there for me to know what might have happened. Deleting the "bin" and "obj" folders is essentially equivalent to performing a "clean" of your project/solution in VS: it causes everything to be re-built from scratch. It's possible that there was something stale or corrupted in the "obj" folder from a previous build, but for some reason the build tools thought it was good/up-to-date so wasn't rebuilding it. After you removed it, it had no choice but to rebuild it. That's just a wild guess; I really don't know. Glad you got it working, though! – Cody Gray - on strike Jan 28 '23 at 00:23
90

I would encourage you to see this youtube video which demonstrates the difference between C# bin and obj folders and also explains how we get the benefit of incremental/conditional compilation.

C# compilation is a two-step process, see the below diagram for more details:

  1. Compiling: In compiling phase individual C# code files are compiled into individual compiled units. These individual compiled code files go in the OBJ directory.
  2. Linking: In the linking phase these individual compiled code files are linked to create single unit DLL and EXE. This goes in the BIN directory.

C# bin vs obj folders

If you compare both bin and obj directory you will find greater number of files in the "obj" directory as it has individual compiled code files while "bin" has a single unit.

bin vs obj

Meraj al Maksud
  • 1,528
  • 2
  • 22
  • 36
Shivprasad Koirala
  • 27,644
  • 7
  • 84
  • 73
  • 1
    I tried to compile both a C# console application and a WPF application with VS 2017 and .net 4.7. In both cases I couldn't find "individual compiled units", that is objects that have a 1-1 relationship with source files. Only xaml files seem to produce intermediate files for each source files. Maybe things are changed in newer version of .net? Maybe I'm missing something? Maybe this is how c++ works and nobody acutally checks those files out and see that it doesn't work that way? – AgostinoX Sep 19 '20 at 10:30
27

The obj directory is for intermediate object files and other transient data files that are generated by the compiler or build system during a build. The bin directory is the directory that final output binaries (and any dependencies or other deployable files) will be written to.

You can change the actual directories used for both purposes within the project settings, if you like.

David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
8

One interesting fact about the obj directory: If you have publishing set up in a web project, the files that will be published are staged to obj\Release\Package\PackageTmp. If you want to publish the files yourself rather than use the integrated VS feature, you can grab the files that you actually need to deploy here, rather than pick through all the digital debris in the bin directory.

Roger Hill
  • 3,677
  • 1
  • 34
  • 38
6

Be careful with setup projects if you're using them; Visual Studio setup projects Primary Output pulls from the obj folder rather than the bin.

I was releasing applications I thought were obfuscated and signed in msi setups for quite a while before I discovered that the deployed application files were actually neither obfuscated nor signed as I was performing the post-build procedure on the bin folder assemblies and should have been targeting the obj folder assemblies instead.

This is far from intuitive imho, but the general setup approach is to use the Primary Output of the project and this is the obj folder. I'd love it if someone could shed some light on this btw.

SteveCinq
  • 1,920
  • 1
  • 17
  • 22
0

It's better to understand this issue from a higher level: compile and link, which applies not only c# language, but many other languages.

Compiling - The modified source code is compiled into binary object code. This code is not yet executable.

Linking - The object code is combined with required supporting code to make an executable program. This step typically involves adding in any libraries that are required.

Specifically, for .net case the obj(object) folder contains the output of compiling stage. And bin(binary) folder contains the output of linker.

Chris Bao
  • 2,418
  • 8
  • 35
  • 62