Run .exe File with Dependencies on Location - c++

I have a Visual Studio project (Project 1) that generates an executable file (a.exe). I have a project (Project 2) that runs the executable from project (Project 1) multiple times with different command line arguments. On the command line I can run:
C:\filepath\Solution\Project1> a.exe //command 1
But I cannot run:
C:\filepath\Solution\Project2> C:\filepath\Solution\Project1\a.exe //command 2
Because the .exe file depends on the surrounding file hierarchy.
In Project 2, I can run a system call to execute command 2, but I hit hierarchy/location dependency issues. The Solution structure is as follows:
Solution
--> Project 1
--> a.exe
--> Project 2
--> main.cpp // this will run system call to command 2
Is there any way I can get around this without changing the a.exe file?

In Project2, how are you executing Project1's .exe? If you set the Current Working Directory (CWD) to the folder where Project1's .exe resides, it should work as expected:
var p = new ProcessStartInfo(#"C:\Project1\bin\project1.exe");
p.WorkingDirectory = #"C:\Project1\bin";
Process.Start(p);
If this doesn't work for whatever reason, you can simply copy the entirety of Project1's binary output and the associated file structure into Project2's output folder. There are two ways to do this:
Option 1 (preferred): Add the binary output of Project1 to Project2. To do this, right-click on the Project2 node in Solution Explorer, select Add | Existing item... In the "Add Existing Item" dialog, change the file type to "All Files". Navigate to Project1's bin folder, select the Project1 .exe and all dependencies, click the "down arrow" on the right side of the "Add" button, and select "Add as link". In Solution Explorer, select the newly-added file links in Project2, and in Properties change "Copy to Output Directory" to "Copy if newer". For each subfolder in Project1's bin folder, you must use Solution Explorer to create a new corresponding folder in Project1, then repeat this process for all files in that subfolder.
Option 2: Create a post-build step to copy Project1's binaries and surrounding folder heirarchy to Project2's bin folder. To do this, right-click on the Project2 node in Solution Explorer and select Properties. Go to Build Events, and enter a Post-build command line such as the following:
xcopy "C:\Project1\bin\" "$(TargetDir)" /S
Option 1 is preferred because with Option 2, external tools are less likely to realize Project 2's dependency on Project1. In both cases, Project2 must be updated to run the Project1 .exe directly from Project2's output folder.
Good luck!

Related

Build specific projects with devenv

I have the following folder structure for a VS2019 solution/project:
Solution_folder\
my_solution.sln
cpp_project\
my_cpp_project.vcproj
ifort_project\
my_ifort_project.vfproj
...
...
and I want to build specific projects from this solution using the command prompt.
Following the answer here, as well as the MS docs guidance
I tried the following:
devenv %path_to_sln_folder%\my_solution.sln /build Release /project .\ifort_project\my_ifort_project.vfproj /projectconfig Release
Also tried other variations according to the documentation (eg reference the name of the project only or pass the absolute path of the project). However, I always get the following error:
The operation could not be completed
Use:
devenv [solutionfile | projectfile | folder | anyfile.ext] [switches]
The first argument for devenv is usually a solution file, project file or a folder.
You can also use any other file as the first argument if you want to have the
file open automatically in an editor. When you enter a project file, the IDE
looks for an .sln file with the same base name as the project file in the
parent directory for the project file. If no such .sln file exists, then the
IDE looks for a single .sln file that references the project. If no such single
.sln file exists, then the IDE creates an unsaved solution with a default .sln
file name that has the same base name as the project file.
Command line builds:
devenv solutionfile.sln /build [ solutionconfig ] [ /project projectnameorfile [
/projectconfig name ] ]
Available command line switches:
/Build Builds the solution or project with the specified solution
configuration. For example "Debug". If multiple platforms
are possible, the configuration name must be enclosed in quotes
and contain platform name. For example: "Debug|Win32".
/Clean Deletes build outputs.
/Command Starts the IDE and executes the command.
/Deploy Builds and then deploys the specified build configuration.
/DoNotLoadProjects Opens the specified solution without loading any projects.
/Edit Opens the specified files in a running instance of this
application. If there are no running instances, it will
start a new instance with a simplified window layout.
/LCID Sets the default language in the IDE for the UI.
/Log Logs IDE activity to the specified file for troubleshooting.
/NoVSIP Disables the VSIP developer's license key for VSIP testing.
/Out Appends the build log to a specified file.
/Project Specifies the project to build, clean, or deploy.
Must be used with /Build, /Rebuild, /Clean, or /Deploy.
/ProjectConfig Overrides the project configuration specified in the solution
configuration. For example "Debug". If multiple platforms are
possible, the configuration name must be enclosed in quotes
and contain platform name. For example: "Debug|Win32".
Must be used with /Project.
/Rebuild Cleans and then builds the solution or project with the
specified configuration.
/ResetSettings Restores the IDE's default settings, optionally resets to
the specified VSSettings file.
/ResetSkipPkgs Clears all SkipLoading tags added to VSPackages.
/Run Compiles and runs the specified solution.
/RunExit Compiles and runs the specified solution then closes the IDE.
/SafeMode Launches the IDE in safe mode loading minimal windows.
/Upgrade Upgrades the project or the solution and all projects in it.
A backup of these files will be created as appropriate. Please
see Help on 'Visual Studio Conversion Wizard' for more
information on the backup process.
Product-specific switches:
/debugexe Open the specified executable to be debugged. The remainder of
the command line is passed to this executable as its arguments.
/diff Compares two files. Takes four parameters:
SourceFile, TargetFile, SourceDisplayName(optional),
TargetDisplayName(optional)
/TfsLink Opens Team Explorer and launches a viewer for the
provided artifact URI if one is registered.
/useenv Use PATH, INCLUDE, LIBPATH, and LIB environment variables
instead of IDE paths for VC++ builds.
To attach the debugger from the command line, use:
VsJITDebugger.exe -p <pid>
I must say that just by removing the part from /project onwards, the build starts ok and finishes without errors, but it is not what I want.
It strikes me as odd, as I believe I'm following the documentation correctly, yet the error message suggests I don't? Also, it doesn't seem to be solution- or project-specific as it's happening with other solutions/projects.
Am I missing anything obvious here?
Thanks

Visual Studio deletes a shared .pch file, and questions about custom build steps

I try to use a shared .pch file, which is compiled in one project and used in others.
However the .pch file is deleted if a .pdb filename of the PCH project differs from .pdb filenames of the other projects.
This page doesn't answer the question: https://devblogs.microsoft.com/cppblog/shared-pch-usage-sample-in-visual-studio/
I don't want to use a same name for all PDBs.
Questions:
1) Why the .pch file is deleted at the start of other projects compilation, which leads to a C1083 error (.pch not found), if PDB names are not equal, not like in that page?
2) I copy pch.pdb and pch.idb files using COPY command, is there a RENAME comand or something, if the copied pch.pdb should be named just like a dependent project's PDB? And where can I find a complete list of Custom Build Step command?
3) I don't understand the purpose of "Additional dependencies" and "Outputs" in Custom Build Step. Can I enter the .pch filename into the dependency list, so it won't be deleted? Does the output list need to contain the dependent project's PDB name, or the pch.pdb, or both?
For some reason (I did this or not) the generated by compiler .pdb file was not $(PlatformToolsetVersion).pdb, but $(ProjectName).pdb . So the copied into other project folders shared .pdb file was pch.pdb in my case, while other projects were expecting different names. And that was triggering a DELETE task in Microsoft.CppCommon.targets , ("Delete the pch file if the pdb file has been deleted."). Instead of changing the output .pdb name I just looked into XCOPY command and made it to change the copied filename to an expected by a specific project (actually then I just added a custom Target with a renaming Copy task right into the project file instead of using the CustomBuildStep calling a xcopy OS's command, as now I learned more about MSBuild).
Then I also changed the generated by Linker output .pdb, just added "Linked" suffix to the name, so there are no conflicts between Compiler's and Linker's PDBs. Not sure if that is a good idea to change the default settings without a big reason.
I guess it's better just to change the Compiler's output PDB to $(PlatformToolsetVersion).pdb , so all projects will use the same name.
That was the first time I had looked into MSBuild and advanced project settings, now it seems to be obvious, that a project using a shared .pdb wants some familiar .pdb name, not a random pch.pdb
Here is my custom Target imported into project files copying the shared .pdb only if it was rebuilt (.idb is not generated in my case):
<Target Name="CopyFreshPchPdb" BeforeTargets="ClCompile"
Inputs="$(PchDir)\pch.pdb"
Outputs="$(IntDir)\$(ProjectName).pdb">
<Message Importance="High" Text="Copying shared pch.pdb" />
<Copy
SourceFiles="$(PchDir)\pch.pdb"
DestinationFiles="$(IntDir)\$(ProjectName).pdb">
</Copy>
</Target>
Did you use the sample code under the github link.
If so, you should download and then use that sample and if you create your own project, you should check your projects carefully.
Borrowing from this tutorial to your project, I think you need to pay attention to whether you have any additional custom targets in your xxx.vcxproj file to delete the PCH file. Therefore, you need to check each xxx.vcxproj file carefully. In vs, there will be no deletion of certain files due to the different .pdb file names of the PCH project and other projects, so check whether there are additional operations of your own.
1) Why the .pch file is deleted at the start of other projects
compilation, which leads to a C1083 error (.pch not found), if PDB
names are not equal, not like in that page?
First of All, make sure that there are no other option to delete PCH file in your projects.
The PCH project is to create a PCH file and the other two projects are to use such file. And every time when you build the two projects(reference the PCH project), and always execute the build of PCH project and then build the two project. So the PCH is always created and later be used in two projects.
Based on it, you should ensure that all three projects create and use the same address for the file.
SharedPCH project
ConsoleApplication1
ConsoleApplication2
In the sample code,the PCH file exists under SharedPchSample\Outputs\Intermediate\Shared\Win32\Debug.
2) I copy pch.pdb and pch.idb files using COPY command, is there a
RENAME comand or something, if the copied pch.pdb should be named just
like a dependent project's PDB? And where can I find a complete list
of Custom Build Step command?
Custom Build Step is under every project-->Properties-->Custom Build Step-->Command Line, and then you can find it.That custom step is just CMD command. And you can execute CMD in that to do extra opertion.
Besides, I guess you want to make those xxx.pdb and xxx.idb be the same name of the project name in order to distinguish one from another. You can right-click on every project-->Properties-->C/C++-->Output Files-->Program Database File Name-->change it and to use $(IntDir)$(ProjectName).pdb. More about Custom Build Steps, you can refer to this link.
I don't understand the purpose of "Additional dependencies" and
"Outputs" in Custom Build Step. Can I enter the .pch filename into the
dependency list, so it won't be deleted? Does the output list need to
contain the dependent project's PDB name, or the pch.pdb, or both?
Additional dependencies is set to use the PCH file's content in the project 1 and 2 which is similar to configuring the address of a reference class library in a c++ project. And I think it might be redundant and since the author has add it which implies that it is well-founded.
And Outputs is the author customized output path, the author changed the output address of the project, and started a new custom output path and a temporary output path.
Actually, the xxx.pch and its pdb and idb file will not be copied into outputpath. So the custom build step is to copied the files into temporary output path. And if you want to copied them into the final outputpath, you can also use these in CustomBuildStep.targets file:
<CustomBuildStep>
<Command>
if EXIST "$(SharedPdb)" xcopy /Y /F "$(SharedPdb)" "$(IntDir)"
if EXIST "$(SharedIdb)" xcopy /Y /F "$(SharedIdb)" "$(IntDir)"
if EXIST "$(SharedPdb)" xcopy /Y /F "$(SharedPdb)" "$(OutDir)"
if EXIST "$(SharedIdb)" xcopy /Y /F "$(SharedIdb)" "$(OutDir)"
</Command>
<Outputs>$(IntDir)vc$(PlatformToolsetVersion).pdb;</Outputs>
<Inputs>$(SharedPdb)</Inputs>
</CustomBuildStep>
And in fact, one project references another project, and the output files of the referenced project are automatically copied to the main project. Perhaps this is because the author's SharePCH project does not generate the pdb and idb files, so those files of the dependent project will not be found in the main project.
I'd like to add that I had a similar situation where a shared .pch file was being deleted but for a different reason related to the .pdb file.
The reason was that the pdb formats were different across the exe and the PCH.lib. The PCH library project has its 'Project Properties -> C/C++ -> General -> Debug Information Format' set to 'C7 compatible (/Z7)'.
When I added a new project exe that depended on the PCH library I had forgotten that new projects default to using 'Program Database (/Zi)' for its 'Debug Information Format'.
So now when the main project is being built and linked with the PCH it would delete the PCH.pch file and complain that the .PCH is missing.
Having all projects with the same matching Debug Information Format was the fix to prevent the PCH from being deleted.

Xcode Project with multiple cpp files

I have a project directory which has multiple cpp files. Each of these files has a main function tied to the respective files. Can i execute each of these files individually or do i have to modify some configurations in Xcode to target each of the files individually. If so, what are steps that i have to take?
This is what the structure looks like in Xcode
Project1
Project1
main1.cpp
main2.cpp
main3.cpp
It's relatively straight forward to have multiple targets under the same project:
Click on your project in the project navigator:
Go to your target, here;
Go to "add target":
Select "macOS" and scroll down to "command line tool":
Give it a name, and finish:
You'll see both target in the same project:
Next to the 'run' and 'stop' buttons, set your active scheme to the desired main file:

Where should i save codeblocks projects?

so i downloaded codeblocks codeblocks-13.12mingw-setup-TDM-GCC-481.exe
when i try to run main.cpp i get this error message:
Failed to open 'C:\Users\$imba\Documents\Codeblocks projects\test\main.cpp'.
this is a detailed summary of what i did prior to opening main.cpp
i selected the defaults all the way through the end. I created a seperate folder (called Codeblocks projects) in my documents to save the projects in.
now when i went to create a new folder, i selected console application, C++ and then i named my project test. i decided to create the project in the Codeblocks projects folder that i created. the resulting filename comes out to be C:\Users\$imba\Documents\Codeblocks projects\test\test.cbd
i then selected
GNU GCC Compiler,
'Create Debug Configuration: Debug'
output dir: bin\Debug\
object dir.: obj\Debug\
i selected create Release configuration,: 'Release'
output dir.: bin\release\
objects output dir.: obj\ Release
Finish
when i double click on main.cpp is when i get the error message
Failed to open 'C:\Users\$imba\Documents\Codeblocks projects\test\main.cpp'.
please help, i need this program for my course.
Try saving it in a folder in C:\, like C:\Cpp\. Code::Blocks may be having problems with the space or the $ in the file path.
I just created a new folder CB-Project under my D: drive where I have codeblocks installed.
You may want to look in the directory that you have CB installed in first, you may also find this in program file (x86) or the codeblocks sub directory. Look for a file named that may have proj or project in it's name. If you don't find it you can just create a new folder. You will have to put the new folder name in the CB path so it will find it.

Eclipse CDT "New Class" default paths for created source files

How can I configure Eclipse 4.2 to put the test file in tests/ when using the New Class wizard?
I'm looking for the same thing... well, I was looking to automate it. In the remote case you didn't figure out any solution at all, here's my solution that is a 2 extra clicks and some typing (steps 3 and 4).
Right click folder you want your new source files (.h and .cpp) to be in, and choose create new class
Type the 'class name' as usual, check 'unit test'
Click Browse next to unit test and click ok, without selecting a file (but don't hit cancel!).
choosing a path doesn't do anything. It would be nice if Eclipse would append the path to the file name, but it doesn't.
What it does do is change the source folder to the project root, and fills in a relative path for the other 2 files, src/class.h and src/class.cpp.
Type test/ in front of the test file name to specify the path.