Visual Studio Run specific source C++ File - c++

I want to know how to run a specific files in source files in my project. For example, I have 2 files in my source file, Exer1, Exer2. Now, the Exer1 file is already selected as the default startup file. I want to run the Exer2, however I don't know how to do it. Or much better I could run a selected file. Is this possible? Or do I need to create a new project? Thanks.

You cannot "run a source file". Source files are compiled and linked together to make an executable. In Visual Studio, this is what a project does. Building a project will give you an executable that will run when you press F5.
Now, you can have multiple projects in a solution and they can all generate a different executable. Which one gets started when you press F5 depends on the current StartUp project. To change it, you can right-click on a project and select "Set as StartUp Project".

Related

How to run a single c++ file in Visual Studio not in Visual Studio Code

I am able to run the cpp file if create a project and build the solution but when i add another file cpp there is a error as there can be only one main function in a project.
I don't want to create a project just execute singel individual files in Visual Studio;
I can executehi individual files in Visual Studio Code.. How do I do this Visual Studio.(IDE)
You don't run source "C++ files"; you run executable files. These are built from source files through the compilation and linking process. A VS project represents the sequence of steps needed to build a single compilation product (executable, library, etc).
So if you have two source files, each of which separately builds two executables, that means you want two projects.
If you wish to compile individual files on VS you can simply click "terminal" at the top of the application and then "Run Build Task".
After running the build task with your selected compiler it will create an executable that you can test/run.
for example
I have a file open called main.cpp
After selecting "Terminal" and then "Run Build Task" and selecting the compiler "gcc, g++"
the compiler will produce an executable called main.exe

Is it possible to only link a project without compiling when using visual studio 2019?

In my project, there is a common header file shared by many source files. Usually, a modification of the header only affects several source files. I want to recompile those files manually and then let vs to link the project.
Sure, compile your C++ files only (ctrl-F7). Then link the project (right click project, then "Project only" -> Link), then run. In Options => Build And Run, make sure you specify "Prompt to build" when projects are out of date, and then choose to run instead of build. Good luck!

How do I debug existing C++ source code in Visual Studio 2015?

This may have a very simple solution, but being new to Visual Studio and C++ programming, I'm having a hard time with this.
I downloaded an SDK written in C++ which contains an executable file and also the source and header files. The executable file accepts some command line arguments. So far I've been running the executable file from the windows command prompt (like C:\path\filename.exe -argument), but now I want to be able to enter these command line arguments and then place breakpoints in the source code for debugging the source code.
I don't know how I can open the source files in Visual Studio and debug it. If I just open the source file with the main function, the debug button says 'Attach' on it instead of debug.
I see another similar question here, but that question is for a project developed using Visual Studio whereas the source code I have does not have any Visual Studio project/solution files. The only files I have are the executable, the source and header files (.cpp, .h, .hpp), and CMakeLists.txt files.
You can "open" the exe as a project (you can achieve the same if you drag and drop the exe into VS icon). Then you can add command line parameters at Project Properties. You will need to have debug symbol information (usually a .pdb file), if you want source code level debugging (values of variables, etc.). If you don't have that, you can only debug at the disassembly level.
You may want to create a proper project for the source files - it is an easy task, if the project is simple - so you can rebuild the exe.

VS2010: Set what directory an executable looks for files in

I have set my project to output my .exe file into a specific directory, and if I run this compiled executable outside of Visual Studio, it can find and use the files around it using cstdio. However, if I run it within Visual Studio, those files are suddenly gone, as if it's in the wrong directory.
What is causing this, and how might I fix it?
Visual Studio will run the program that's "over there" in its own project folder, so the program's working directory, where it creates and looks for files etc..., will be the project directory and not where the executable is stored.
You can do this yourself. Open a command prompt and type the full path to the executable. It will run, write, and look for files in the prompt's current folder.
Do not use argv[0]. this will give you the command line, and might not include the actual location of the executable.
If you want to keep your files with the executable no matter where the program is run from, GetModuleFileName will tell you where the executable is. You can then strip off the executable to get the path and concatenate that with the names of other files you want to stay in the same folder as, or relative to, the executable. With this approach you can run the program from anywhere, including Visual Studio.
If you want to know where the executable is being run from and make your own way, use _getcwd.
If all you care about is Visual Studio, navigate to Project->Properties->Configuration Properties->Debugging and set the Working Directory to the target directory
I will assume that what you really want to do is to copy the executable elsewhere after the build. That way you don't have to have anything special in the executable that is only for debugging (development). You can have Visual Studio do the copy for you automatically using a Custom Build Step. The build should create the executable in the same project directory that it normally does, then the copy will be done after that (like a mini deployment).
In the project's properties:
Select "Build Events" | "Post-Build Step"
Enter a post build event command to do the copy, for example:
copy "$(TargetDir)$(TargetFileName)" "G:\Temporary"
Where "G:\Temporary" is wherever you want the file to be copied to.
Be mindful of the configuration. You can choose to create the Custom Build Step for both debug and release configurations, but then the executable will be copied to the same place for both configurations. You probably will want a different copy command for each configuration.
While in the property page for entering the Custom Build Step, you can click the down-arrow and select "Edit..." (inside the less than and greater than symbols) to get help creating the command. While doing that, click on "Macros>>" to see the big list of available macros.

visual studio 2013 deploy a project

I crack my head already, I need to deploy a project. I tried to use InstallShield, it create an msi file, and local installation has no errors, but then when I tried to launch the program it output "debug error". In my settings code generator -> runtime library set for Multi-threaded Debug DLL(/MDd). If I'll set it to Multi-threaded (/MT) it would not compile: "Please use the /MD switch for _AFXDLL builds"
Then I tried to use VS Installer project extension. It creates the msi and setup file, but after I install it, I can't find the .exe file and launch program. What I am doing wrong?
Folder with project files
![enter image description here][3]
Folder with exe file
![enter image description here][4]
At the stage of creating the installer for your program, you have to put all the external files, libraries and resources, that your program needs in order to run correctly, in your program's working directory. When you run the program via Visual Studio, everything works fine since it can find the files, because, as you've mentioned, they are in the project folder, and the project folder is where Visual Studio is looking for them.
When you launch your program outside VS, the program can't find these files since it by default tries to locate them in the folder in which the .exe file being run is located. So, if you wan't to run program outside VS, you need to put all the files needed to your .exe's directory.
For the same reason, while creating your installer, you need to include all the files necessary in the installation target directory together with your .exe. Every installer creator lets you do that.
In general, if you cannot run your program outside any IDE, and you can run it within that IDE, it is a rule of thumb that you should first check if you have included all the necessary files/libraries/dll's/etc. in your program's working directory.
I solved it!!!!!!!!
1)Help to run .exe file, helped updating .uld file in the same directory as a exe file.
2)I used Dependency Walker (http://www.dependencywalker.com/) to find all dll that it's need. And then I create a Setup Project using Wizard at the "Choose file to include" step I add every file and every library that it's depend on! Build->Install-> Then magic, and my application installed and running! Thank you all for your help