Visual Studio 2010 new C++ project from existing code - c++

I want to modify an existing project but put all the source and header files etc. into a new directory to keep them separate. When I use the "create new project from existing code" option, the solution files seem to point back to my original .cpp file which I don't want.
Q - what the best way to do this?

The Visual Studio projects are simple XML files that can be edited. Just bulk replacing the paths in your favorite text editor is probably the easiest way to go (I do that all the time).

create a new project, without opening the existing one.
then copy all the header and the cpp files from the older folder to the new project folder.
'add existing item' will only point to the original file only.

Related

visual studio c++ does it build only files shown in the tab?

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.

folder structure for c++ programming

I am using visual studios. Below is my current project folder structure.
I am actually practicing all codes in a book. So, i want to create a folder for each chapter and write each exercise in a separate .cpp file. But, i am not sure if creating folders under "source files" folder is the correct thing to do. Should i create a folder inside of Source files or outside? what's the standard way of doing it?
Just add them to separate projects. To do that, simply right-click the solution, select Add -> New Project... That will bring up a wizard to make a new project in your solution. Then, you can have another main inside that project.

Using sub folders in the Source folder for Visual Studios 2010

I am currently creating a small project over the summer using c++ and visual studio 2010. I wanted to organize the individual .cpp and .h files into their own folders in the project directory. While I use filters in the actual project, I do understand that these are filters, not folders.
So my question is this. In my source, I currently have a header folder called GameStateManger.h, and a folder called Tank_Headers and Tank_CPPs. I also have Tank.h, and Tank.cpp, in their respective folders. My problem is that in GameStateManager, I have this #include "Tank_Headers\Tank.h", however, in my Tank.h, when I type #include... nothing shows up. I cannot find anything. However, if I make a new header, and put it in just source, I can search for those folders. I want to be able to organize my .h files and my .cpp files into individual folders in my source folder, so it is much more organized, considering the project might get very big very soon.
Thank you!
try to use #include "Tank.h"
That should work as long as you add the file to your project first. Drag it from explorer into whatever filter you want it organized it into.
Sorry I forgot to add:
You need to add the subfolder to your project. Go to the project properties, look under C/C++ and add it to Additional Include Folders.

Copying source files into a Visual Studio project

I've created a new Visual Studio C++ project and I'd like to import copies of a number of C and header files into the project. That is, the files are currently in a folder on my desktop and I'd like to import them such that copies are placed in the newly created project folder.
How would I do this? I've tried using File | New | Project from Existing Code but that just keeps the files in their existing location. I've even tried a simple Ctrl-C and Ctrl-V and again that imported the files but they stayed in the same place on disk.
Copy the files to the new location in Windows Explorer, then start Visual Studio and add them as existing items to the project.
I think the better solution would be to create a new (empty) project and add your h and cpp files per "Add -> Existing item" in the context menu of "Headers" and "Sources"

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

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.