Automatically add .cpp files to Visual Studio 2019 project - c++

I wrote a small code generator but I dont know how to automagically include the generated files in the project automatically.
My first try was to write into the .vcxproj file, but this prompted a windows saying that the project file was modified and I want to auto-reload (there is an option to auto-reload changed files, but this only works for existing files in the solution )
My second attempt was to add a wildcard to the .vcxproj file to compile all .cpp files in a given folder. This worked but as soon as I tried to add a new .cpp normally from within VS I got an internal error.
Any ideas?

Related

How do I run .cpp files in Visual Studio?

I have been following tutorials which have me download and unzip projects which contain .sln files for me to open. Following them this way is pretty easy. However, I want to be able to download a single .cpp file and run it without creating a project. I just want to get straight to into it. In Code::Blocks, setting this up is easy to figure out. For some reason, I can figure it out in Visual Studio.
I want to be able to download a single .cpp file and run it without creating a project.
You cannot. Visual Studio does not support this. A project is always required, even if it only contains a single source code (.cpp) file.
You can, however, run a single .cpp source file through Microsoft's C++ compiler on the command line (cl.exe), and then execute it. But this doesn't involve anything about the Visual Studio IDE.
if you have the source code, you could make a .cpp file by right-clicking and adding one and then ctrl+s then f5 and it might run... Idk.

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.

How to make Visual Studio automatically detect a .cpp file in a subfolder?

As it's described in the title, I would like to make Visual Studio detect a .cpp file which is in a subfolder. (It's a C++ project.)
My program automatically creates new .cpp files and .h files, but when it creates a new .cpp, it doesn't detect the .cpp file in the subfolder of the project. Even if I restart the project/program.
What could I do to make it automatic?
Does an instruction exist to tell Visual Studio/or the program to compile this .cpp file ?
Does every .cpp file have to be in the same folder? I could do it that way, but it would be more organized if the different files are in different folders.
So I didn't find any solutions, but I've ask severals persons, and they told me that I might have to change by hand some files automactly created by Visual studio to tell them to include a specific .cpp file. But to identify which file I've to change and if I can open them as a txt file ect.. It was too much work, then I've decided to change my project.
Thanks for your help ! And I hope this will give you a little taste of what you have to do if you have the same problem

How to Run a OpenCL program from Command Line using Visual Studio?

I'm creating a Build button in my application which can take two source code files .cpp and .cl
It should make use of Command line to set the header files, libraries and OpenCL.lib which i usually set manually through VS Configuration Settings.
Hope there is some way to pass these Additional Libraries and Header files and compile the code.
Thanks.
I'm not entirely clear on what you're doing, and it really depends on how you want your application to work.
The VS configuration settings live in a .vcxproj file. Does this live with the input .cpp/.ci files? If so, you would be better off compiling the source files by calling "msbuild.exe" on the project file. Instead of providing the .cpp/.ci files as input, it would be the .vcxproj file.
If there isn't any associated .vcxproj with the .cpp/.ci files, but it instead lives with your application, then you'll have to parse the .vcxproj directly and search for the XML tags that contain this information (eg: ). Obviously, this will be a little more work. Alternatively, this could live in a completely separate text file that lists the library paths, header paths, etc. that you want to build the source files with.
Again, without fully understanding what you're trying to do, it's difficult to give a clear answer.

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.