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 the windows explorer. So here is my question.
How can I create real folders in my project?
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.
This is mildly annoying, however, here is what can be done:
Create the new folder in Visual Studio. This does not create a new folder in the file system. Add a new item to the folder. Choose the proper directory for the item.
For example, if your project is at %Documents%\Project, and your new folder name is Folder, then you add a new item to that folder at %Documents%\Project\Folder.
Visual Studio 2012 will put the item in the folder where you want it. If you add a new item, it will default to the same folder. That is where the annoying part comes in. If you create 3 folders for all your project items and try to add a new item to each folder, Visual Studio will try to put all 3 items in the same place in the file system (the last folder you added a new item to), while putting items into the correct Visual Studio folder.
It is possible there is a setting for this in Visual Studio. I haven't found it. I also haven't looked that hard.
Related
I just tried to open my project in the VS 2017 and suddenly i cant see one of my classes files.
The header and the cpp are missing.
The files are not in the folder and i cant even see them in the Cmake.
When i start the VS and trying to open the project it says:
ex01_files (the name of the folder) could not be opened. Do you want to remove the reference(s) to it from the Recent list(s)?
when i open the project, i get an error at the top that says:
C++ IntelliSense information may be out of date, generate the CMake cache to refresh.
Im new at the VS so im afraid to take action by myself.
If you just removed the header and cpp in VS, you could find these files in the corresponding folder.
If you deleted these files, you could try to find them in the Recycle Bin.
If you deleted these files completely, I suggested that you could use Windows File Recovery to recover lost files.
I'm new to C++ and was following the instruction steps here
The situation was I have math.cpp file where contains two functions.
One of those function was already in the other cpp file in the same project so I have removed it.
The build worked however, when I tried to recreate the removed cpp with the same name, it said this already exsits. In the file explorer, the file was there not deleted completely.
Now I'm confused how this visual studio building works.
Does it only builds the cpp files which is shown in the tab even though some files not included may exist in the same folder?
I was wondering wouldn't this cause a problem in the future when you are programming and some of the files not shown in the tab?
Removing the file does not delete it from the directory, it removes it from the project. So it's still there in the directory. When you right-click on the file and attempt to remove it, it should give you the option to delete the file or remove it. Now that you have removed it right click on the source files folder and add then existing item. Now navigate to where the cpp file is and add it back.
I have been working on a project for a while now (C++), in Visual Studio 2019, but I've suddenly run into several problems that send me in circles between them.
I loaded it one morning, and found that it couldn't load any of the files.
So thinking I must have accidentally moved files or something, I just copied my files again from my Github repository, but then it started having problems with every include file for the headers.
Manager.h is in a different folder in the project, which makes this more confusing to me because if the source files can't open a header file in the same solution, the headers should have problems opening a different header in a different part of the project.
I've tried deleting the project settings and reloading it, but VS won't reload them, and Github desktop is freaking out with any action with the project.
Cloning my repository again and loading that just sends me back to the first problem...
Anyone have any other ideas for what I can do?
Check to see if the files are in the folder present in the error message. If they are that can be a problem with the config files of the project/solution.
Possible solutions:
Since you have the project in the repository, a quick way to solve it would be to delete your local project and clone the repository project. (Or clone it to a different location)
You can try to repace the <Subscriber.h> by "Subscriber.h".
Create a new empty project and add all the project files through the Solution explorer:
right-click on the folder where you want to add the project (I recommend Source for the .cpp and Headers for the .h);
Select Add -> Existing item -> select the files.
I have a VS2012 solution. One of the Project A generates a .h file at runtime. This header file is used by another Project B. Project A and Project B are in the same directory.
However when Project B starts rebuild, C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\Microsoft.CppClean.targets deletes the files generated by Project A. Due to this Project B cannot find the file anymore and build fails.
How can I avoid the files being deleted during a clean build? Should the two projects be moved to a different directory?
Note that this problem does not occur in Visual Studio 2008.
Have you set the build order in the solution? It has to be explicitly set that project B depends on A, unless you use the project referencing functionality built into VS to directly set the dependency between the two projects.
Is this header generated in the temp ("Intermediate") directory by a "Build Event"? If so, then Project B assumes it's a temp file and deletes it. This is because project B's cleanup finds the header in the temp directory but doesn't know that it's an output of Project A. Possible solutions:
Separate the output folder from the temp folder and generate the header in the output folder.
Keep the output and temp folders together but separate the output/temp folder of Project A from that of Project B.
Generate the header in a Custom Build Step instead of a Build Event, and specify the header as the Output of the step.
I faced same issue while migrating to VS 2015. The solution is to set the "Configuration Property->General->Intermediate Directory" to ..\\$(ProjectName)\ so that Build.CppClean doesn't clean up previous build dlls from other unrelated projects. This worked for me.
I would like to have a the following structure of folders:
Myproj\src
Myproj\include
Myproj\lib
Myproj\src should include all sources and project files etc.
EDIT: also, $(projectDir) should equal Myproj\src
The problem is with Myproj\src, when I create a new project in VS2010 a folder named \Myproj is created and in it all sources are placed. How can I break this connection and have my desired structure made?
A TFS friendly solution would be nice.
After you create a project, you can change the folders where certain files are stored. When you right-click the project in solution browser, there will be option for VC++ Directories - Include Directories.
If you add your $(ProjectDir)\src and $(ProjectDir)\include to this field, you will be able to put your files in these places. However, default VS creators, such as "Add Class" will still add your files to Project folder.
As for "lib" - I am guessing you are trying to create a library project. For that, you must find the "General->Output directory", and change it accordingly.