Program works in VS 2013 but not the .exe - c++

I have made a test program in Visual Studio 2013 using Direct X 11. It consists of a simple sprite which rotates slowly based on a timer implementation. The program loads and runs fine using F5 or Ctrl-F5, but when I try to open the actual created .exe (in my \Debug folder) it just shows the window then closes instantly.
Most of the answers I have read on this issue correspond to loading the .exe from inside visual studio. I have also tried Release mode but the same thing happens.

Sprite files are kept in your project folder. The default run-location from the Visual Studio IDE is the project folder of the project which you're executing. That is, normally it executes from the directory where your .vcproj or .vcprojx file is kept (and that is often one folder below your solution directory folder, where your .sln file is kept).
If your project runs correctly from the IDE, but fails to run directly from the debug folder, it is highly likely you are relying on project data files that are kept along side your source files in the project folder. When run from the Debug folder, those files are no longer visible because Debug folder is your working directory; not the project folder.
There are a number of ways to solve this problem, each with its own merits. A few options are:
Post Build Step
Make a post-build step for your project that copies your data files to the $(TargetDir) location with your project. These files will then be visible in the same directory as your executable.
Benefit: Its easy.
Drawback: It will always run if you click "build solution" even if the data files are "up-to-date."
Custom Build Targets
Add your data files to the project and write a Custom Build script that performs the same copy, but also establishes an output dependency file(s).
Benefit: Almost as easy as #1, but a little more tedious.
Drawback: You may have a lot of data files and each will require its own custom build step. (Note: you can multi-select all the data files in your project, and if you're creative with the built-in macros you can have them all use the "same" build rules and commands).
Embedded Resources
Add the data files as custom resources to your executable.
Benefit: Your project no longer requires data files side-by-side with the executable since they are embedded in the resource table of your EXE module.
Drawback: Custom code is required to dynamically load the custom resources from your executable's resource table rather than off-disk. It isn't difficult at all to do, but is additional work.
There are other options as well, but I hope this gives you some ideas to start with.

Related

How do I get a simple batch file to run with my project in Visual Studio?

Basically, I have two projects, one which compiles to a dll and the other which consumes that dll. What I want is for the .dll file to be copied to the dependent project's binary folder so that project can use it. I can't just do a simple "copy" script because then my dll won't get recopied upon recompilation. It was suggested that instead I should run a couple commands, one which deletes the old dll if there is one, and then copy the newly compiled dll to the location. It was also recommended to do this with a batch file. I've never written a batch file for visual studio (or anything else for that matter) so my question is how would I:
1.) write a batch file that would say something like:
if dll exists then delete and copy over the new dll
2.) Get that batch file to run with my project in visual studio?
Thanks for any help!
You need to get the results (.dll) of one project into the other project's folder. There are multiple ways to do this, but one way is the following.
Go to the project properties of your DLL project.
Go to Build Events -> Post-Build Event
Enter copy <filename>.dll ..\otherproject\bin /y
This will copy the DLL to the destination folder, overwriting it if it's already there (and if it's not in use). You may need to adjust the directories depending on your project structure.

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.

c++ executable program test

I have been following some tutorials for c++ game programing. I am kind of new to c++ and I'm using Microsoft Visual C++ 2010 Express IDE. I'm working on creating a game, and when I run the program through the IDE, it shows the grass sprites as expected. But when I run the .exe file from the Release folder, it shows weird images. and when I run the .exe file from the debug folder I get a grey screen. Can anybody tell me why this is happening?
I hazard to guess that your sprite images are kept as data files in your project folder. With that I offer the following premise:
The default run-location from the Visual Studio IDE is the project folder of the project which you're executing. That is, normally it executes from the directory where your .vcproj or .vcprojx file is kept (and that is often one folder below your solution directory folder, where your .sln file is kept).
If your project runs correctly from the IDE, but fails to run directly from the release folder, it is highly likely you are relying on project data files (images in your case) that are kept along side your source files in the project folder. When run from the Release folder, those files are no longer visible because your the Release folder is your working directory; not the project folder.
There are a number of ways to solve this problem, each with its own merits. A few options are:
Post Build Step
Make a post-build step for your project that copies your data files to the $(TargetDir) location with your project. These files will then be visible in the same directory as your executable.
Benefit: Its easy.
Drawback: It will always run if you click "build solution" even if the data files are "up-to-date."
Custom Build Targets
Add your data files to the project and write a Custom Build script that performs the same copy, but also establishes an output dependency file(s).
Benefit: Almost as easy as #1, but a little more tedious.
Drawback: You may have a lot of data files and each will require its own custom build step. (Note: you can multi-select all the data files in your project, and if you're creative with the built-in macros you can have them all use the "same" build rules and commands).
Embedded Resources
Add the data files as custom resources to your executable.
Benefit: Your project no longer requires data files side-by-side with the executable since they are embedded in the resource table of your EXE module.
Drawback: Custom code is required to dynamically load the custom resources from your executable's resource table rather than off-disk. It isn't difficult at all to do, but is additional work.
There are other options as well, but I hope this gives you some ideas to start with.
I use VS2008 and try to answer your question. Right click on the project and select properties on the bottom of popup, then go to Debugging under Configuration properties. You can see command you run and arguments you pass in IDE. I guess you miss some parameters.

Handling images and sound files for a simple C++ project in Visual Studio

I am new to Visual Studio, and I am trying to figure out the best way to organize my projects.
I am writing an application using the sfml library, and I have various resources (images/sounds) that I am using. I dropped these into the project folder, and everything works fine when I launch my application from Visual Studio.
I am wondering though, how does this translate to when a program is deployed? If I go into my solution's debug folder, and try launching the exe, it is unable to locate any of the resource files. Am I suppose to tell Visual Studio to copy files to an appropriate directory, and if so how?
Thanks for any advice or links.
For slightly more complicated "deployment" scenario, you can use post-build scripts to copy the correct files into the output directory and even package it into a zip file, for example.
If you find yourself writing more than one page of batch you may want to consider the options below, because batch is a PITA to debug.
Recent MSVS project files are actually MSBuild files (just open the .vcxproj file in Notepad or Vim). For instance you can use the Copy task, invoke arbitrary programs using the Exec task, etc. It can be a bit more sophisticated than the batch script in post-build scripts. MSBuild 4 can use Property Functions making it quite expressive. Useful reference if you do this
For a "full blown" project, you'll want to roll a dedicated build system using a dedicated MSBuild file, NAnt or even higher level wrappers like Rake.
As a less popular alternative, in a previous project I built a small dedicated "builder" .exe project in the solution and have other projects depend on it. Then in the post-build scripts of the other projects I just invoke the builder projects with arguments to make it perform certain tasks. The advantage is that you can write C# (or F# or VB.NET) and not have to fight the build system (as much) and I think it works quite well for small-mid sized projects.
for my project, I direct everything into one directory.
Go to ur project configuration, change General->Output directory, General->intermediate directory, and Debugging->Working directory to one directory. The reason you cannot locate the resource files is because the debug directory is not the same as the output directory.

Visual Studio C++ project management. How do I handle non-code files in a project?

I have a project in a c++ solution. For that project, I have some config files that I would like to manage from within the project and when I build the project, have those config files added to the executable build path in the proper directory structure.
Example:
test.exe references config/myconfig.txt
Is there a way to setup myconfig.txt and my project so when I build test.exe, I get
/
as well as
//config/config.txt
so when I run test, all paths stay in the proper order without me having to go in and manually create those directories.
I'm not sure I'm making sense here, but maybe one of you will understand where I'm going.
You could use pre-build events to create the directories and copy the files.
In Visual Studio's Solution Explorer, you can right-click on the non-code files, select "Properties" and set the "Copy To Output Directory" property.
This creates a rule in the build file to (1) include the file, and (2) to copy that file as part of the build process. In other words, it's possible to get this behavior without Visual Studio, but a little more work.