Specify path to rule file using environment variable or macro? - visual-studio

In some of my Visual C++ project files I would like to specify the path to a rule file using an environment variable or macro.
I have tried editing the ToolFile element with a text editor:
<ToolFile
RelativePath="$(SolutionDir)\build\tao_idl.rules"
/>
But Visual Studio complains that it can't find C:\Path\to\ProjectFile\$(SolutionDir)\build\tao_idl.rules so it looks like it's not expanding the macro.
The same thing happens if I try an environment variable, using either the $(VAR) or %VAR% syntax.
NOTE: The project file is pulled into several other SVN repositories by using the svn:externals property, and not all the repositories that pull this project in use a standard directory structure.
Is this possible?

Related

Where does VS save an environment variable like $(outdir)?

Where does Visual Studio save an environment variable like "$(OutDir)"?
I don't see anything resembling "OutDir" in the command line.
Assuming you changed the "Output Directory" setting of your project, it is stored in the project's .vcxproj file. Just try it, change the setting, save the project and have a look-see with a text editor:
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>Example</OutDir>
</PropertyGroup>
I changed the setting to "Example".
If you did not change the setting then MSBuild computes the value of the property (aka macro). Pretty visible from the IDE, it glues the values of $(SolutionDir) and $(Configuration) together.
Those properties in turn are set by the IDE, based on the platform target and the configuration you have selected in the Build > Configuration Manager dialog. If you build from the command line then you use MSBuild's /property option to select what you want to build.
Note how the environment plays no role at all. If you need an environment variable because you are running some other kind of build tool then you'd do so by using the SET command in a pre- or post-build event. Like:
SET outdir="$(OutDir)"
Using double-quotes to avoid trouble with pathnames that contain a space.
Why are you assuming they're saved? They're not; they exist solely within the build process
these ar basically macros ; you can find the output directory in : Properties->General->Output Directory
For more info check this

Setting Environment variable in Visual studio C++project

I realize that similar questions were posted earlier but I need slightly different solution.
I have VS2012 C++ project A.vcxproj,that has debug1 configuration( platform x64) , and it builds corresponding .exe. It also uses a dll from other VS2012 C project B.vcxproj from a path that must be added to the environment variables. A.vcxproj.user file has following text
<LocalDebuggerEnvironment>PATH=%PATH%;C:\Program Files\libsndfile\bin;..\..\lib\simulink\;$(LocalDebuggerEnvironment) </LocalDebuggerEnvironment>
I need to add this setting automatically to the "A"project with following constraints
I cannot export user file as it as "per user", so cannot upload to our SCM system where other users can download it.
I tried adding code in main function, something like
_putenv("PATH = ....\lib\simulink");
but this does not work, as before the main file is compiled, it needs to search for the dll from specified path, which it dosn't .
Can anyone suggest a easy, portable fix , that i could distribute to all users through SCM, along with the project file.
I have also tried following:
--Created batch file setpath.bat with following content
#ECHO %PATH% set PATH = %PATH%;C:\Program Files\libsndfile\bin;C:\dev\lib\simulink
-- added to A.vcxproj settings->build event->Pre-build->Command line
call C:\setpath.bat
and I don't see the added paths under vS op window. neither does the VS User file gets the change, and running the project complains for missing dll error.
--I tried to execute the batch file in
A.vcxproj settings->
Custom build step->Execute before "Run"
and still no result.
I guess the solution needs to add needed path to current environment variable for the time VS project is "run".
Thanks
sedy
added to A.vcxproj settings-> Build Events ->Pre-Build event
call setdllpath.bat
where the file contains the following:
#ECHO %PATH%
set COMSPEC = "%VCINSTALLDIR%\vcvarsall.bat" amd64
setx PATH "C:\Program Files\libsndfile\bin;..\..\lib\simulink"
#ECHO %PATH%
So, once I build the Project, close Visual studio and open it again, and run the files wiithin project, it picks up dll correctly.
Contents in *.vcxproj.user, *.vcxproj.user or *.props use the same xml schema so can be easily exchanged or included.
First if usefull you can add UserMacros to define the path to your libraries.
Like bellow for the following two variable
PYTHONHOME=$(USERPROFILE)\AppData\Local\Programs\Python\Python37
PYTHONPATH=$(PYTHONHOME)\DLLs;$(PYTHONHOME)\Lib;$(PYTHONHOME)\Lib\site-packages
Edit the .vcxproj adding inside <Project>:
<Project .... >
...
<PropertyGroup Label="UserMacros">
<PYTHONHOME>$(USERPROFILE)\AppData\Local\Programs\Python\Python37</PYTHONHOME>
<PYTHONPATH>$(PYTHONHOME)\DLLs;$(PYTHONHOME)\Lib;$(PYTHONHOME)\Lib\site-packages;</PYTHONPATH> </PropertyGroup>
...
</Project>
After you can add inside your build configuration, the following to set the $(Path) variable.
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<LocalDebuggerEnvironment>Path=$(Path);$(PYTHONHOME);$(PYTHONHOME)\DLLs;$(PYTHONHOME)\Lib;$(PYTHONHOME)\Lib\site-packages;$(PYTHONHOME)\Scripts;$(PYTHONHOME);</LocalDebuggerEnvironment>

How do I compile a single source file within an MSVC project from the command line?

I'm about to start doing some benchmarking/testing of our builds, and I'd like to drive the whole thing from a command line. I am aware of DevEnv but am not convinced it can do what I want.
If I could have a single file built within a single project, I'd be happy.
Can this be done?
The magical incantation is as follows. Note that this has only been tested with VS 2010 - I have heard this is the first version of Visual Studio with this capability:
The Incantation
<msbuild> <project> <settings> <file>
Where
msbuild is a path to MSBuild.exe. Usually this should be set up for you by the VS2010 bat file so the right one will end up in your PATH, but if not I found one at C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe
project is the path to the vcxproj file within which your source file resides.
settings include the following:
/p:Configuration="Debug" // or whatever your Configuration is
/p:Platform=x64 // or x86
/t:ClCompile // to specify specifically you're looking to compile the file
file is actually another setting:
/p:SelectedFiles="path_to_file"
Notes
For <project> I had to specify a project (vcxproj) file instead of a solution (sln) file. The sln I would have used has multiple projects within it, so there would have been extra work to go that route anyhow (if it can even be done).
For the /p:Platform=x64 setting, there are several environment variables that pivot on what platform you are targeting (x64 v. x86) so make sure you set those up properly via Visual Studio's vcvarsall.bat.
Regarding path_to_file in the SelectedFiles parameter, this path must be the path as specified in the project file. If the path does not match the path used in the project file to reference the source, it doesn't seem to work.

Force VS2010 to keep the build output path absolute

We want to have our build server send the output of each build to
C:\Projects\{project name}\build\{build configuration}\.
To accomplish this, I set the Build Output path property for my projects to that in Visual Studio 2010, and build locally to make sure everything works. When I do so, visual studio changes the path to a relative one, for example ..\..\build\Debug.
Since the project directory is not the same on the build server (we use TeamCity, so the project url there is something like C:\BuildAgent\work\9358A92GF92), the output doesn't end up where we want it.
How do I make Visual Studio not change the build output paths to relative paths?
You cannot convince the IDE to make it absolute. Possible solutions:
Use a post-build event to xcopy the build output
It won't make the path relative if the output directory is on another drive. You could use the SUBST command to map a drive letter like z: to a folder on c: and fool the IDE that way
Run msbuild.exe directly on the build machine. Use the /p command line option to force the OutDir property to another value.
Solution 3 is probably best since you can make it specific to the build machine without tinkering with the project files and make the devs' lives difficult. The command should look similar to this:
msbuild /p:outdir=c:\mumble\ projectOrSolutionFileName
Beware that the output directory name must end with a trailing backslash
You can not convince IDE to use absolute path, but you can change the Project file:
<OutputPath>C:\Dir1\Dir2</OutputPath>
Your IDE will still show relative path but it will store the absolute path.
Works for me with Visual studio 2010 Sp1

how to set MSBuild VC++ Directories

I have a solution of mixed VB.NET and C++ projects. When using Visual Studio 2005 you can set the "Tools->Option->Projects and Solutions->VC++ Directories" to help the compiler find your include files. When building the same solution with MSBuild I don't see how to pass these settings. The C++ won't compile without this path specified. When building this solution form Visual Studio it build perfectly.
What is the way to pass this path for MSBUild?
Edit: Looks like MSBuild doesn't pass the path (or the /u switch) to vcbuild. Starting from VCBuild instead fails on dependency.
To set the include directories, you can add them into your INCLUDE environment variable. You use the same format as in PATH env. variable - you separate paths with semicolons.
To set the library directories - you can do it in similar way, by putting them into your LIB environment variable.
To set environment variables, you simply right-click "My Computer", choose "Properties". Then you go to "Advanced" tab, and there's a button called "Environment Variables".
Or, you can run MSBuild from a BATCH script, in which case, before calling MSBuild, you can set the INCLUDE and LIB variables, like so:
set INCLUDE=C:\Libraries\LibA\Include
set LIB=C:\Libraries\LibA\Lib\x86
...and also you may like to append %INCLUDE% and %LIB% to your variables to avoid overwriting of them
set INCLUDE=C:\Libraries\LibA\Include;%INCLUDE%
set LIB=C:\Libraries\LibA\Lib\x86;%LIB%
You can use /p[roperty]:useenv=true switch to forward environment variables in newer versions of MSBuild. See full article here http://blogs.msdn.com/b/vcblog/archive/2010/01/11/vcbuild-vs-c-msbuild-on-the-command-line.aspx

Resources