How to create a folder in Visual Studio C++ 2012

后端 未结 2 1398
南旧
南旧 2021-02-19 00:42

I have started my first bigger project with Visual Studio 2012 in C++. I will structure my Source-files in folders, but I can not find where I can create real folders, like in t

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-19 01:09

    The IDE has a command for that, "New Folder". It is even present in the Project + Add context menu, something you can see when you look at the context menu with Tools + Customize. It is however hidden in the C++ IDE. Intentionally.

    Its important to understand why it is hidden. It keeps you out of trouble, the kind of trouble you'll get into when you create folders with Explorer.

    At issue is the way C++ files get built. They produce an .obj file when the compiler is done with them. That obj file is stored in a directory whose name is a project setting. You see it with Project + Properties, General, Intermediate Directory setting. For an individual .cpp file, it is C/C++, Output Files, Object File Name. The default for that one is $(IntDir) a macro that tells the compiler to use the Intermediate Directory setting. With the default settings, all .obj files for the Debug build end up in the Debug subdirectory. Release subdirectory for the Release build. Regardless where the .cpp was stored.

    Maybe you see the bear trap by now. If you create a subdirectory with .cpp files then you'll get in trouble when that subdirectory has a .cpp file whose name is identical to another .cpp file in another subdirectory. They produce an .obj file with the same name. One overwrites the other, which ever one was compiled last. That produces very mystifying linker errors. You'll get duplicate symbol errors because the last built .obj file is linked twice and missing symbol errors for the overwritten .obj file.

    So go ahead and create a subdirectory but beware this problem. You have to change the Object File Name setting for the .cpp file if such a collision happens.

提交回复
热议问题