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

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.

Related

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.

Adding my DLL into a Visual Studio project in C++

I am working on a project that involves making a dynamic link library, so I want to test it in a console app in Visual Studio.
The DLL is also made in Visual Studio, it doesn't have much, just a few functions in it. I'm not sure if I'm just supposed to include the librarys header in the include directories panel in Properties, or do something else
A lot of people say I'm supposed to add its corresponding .lib file in the Library or Reference directory, but I'm not sure that VS generates a .lib file alongside the DLL. I'm using VS 2015.
I don't have VS in front of me this very moment, but these should be the general steps to set it up:
Properties->Linker->Input: your.lib
Properties->Linker->Additional Library Directories: ../your/bin
Properties->General->Compiler->Additional Include Directories: ../your/include
To build your app, the DLL's API headers must be in the include for the compile-time, it's LIB files in the bin for the link-time. Once you have your app EXE, all you need is the DLL to be in the same folder as your EXE when it executes.
You might also want to add the dll project and the app project into a common solution in VS and add (right click) Project Dependency from the app to the dll. This ensures correct order of building, assuming you are going to build the dll at all.
You can also do what I did.
You can create a Libs directory inside of your Solution directory.
You can then place your .DLL files inside of the Libs directory or some sub-directory inside of Libs
In my case, I added the entire SFML-2.3.2 directory in there, which included the source-code, .lib files, and .dll files.
I did link up what I could in the project properties, but I used Visual Studio's macros to fill in the path name to the Solution directory. Just in case I wanted to put this in version control and work on it from multiple machines.
Then I opened up the Project's Property Page.
Within the property page, I went to Build Events -> Post-Build Event -> Command Line
Within the Command Line, you can add a copy command that will copy any needed files into the same directory as the executable that will need them.
In my case I used: copy "$(SolutionDir)Libs\SFML-2.3.2\bin\*" "$(TargetDir)"
I could have written multiple commands to copy just the individual files that I needed, but I had spent a good three hours trying to get SFML to work without actually installing it.

Program works in VS 2013 but not the .exe

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.

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.

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.