how to set MSBuild VC++ Directories - visual-studio-2005

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

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.

Specify path to rule file using environment variable or macro?

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?

How do I set a path in Visual Studio?

How do I set a path for DLL files to be searched in Visual Studio for a particular project alone?
Now I am setting it in the environment path variable, but I would like better control over this.
Search MSDN for "How to: Set Environment Variables for Projects". (It's Project>Properties>Configuration Properties>Debugging "Environment" and "Merge Environment" properties for those who are in a rush.)
The syntax is NAME=VALUE and macros can be used (for example, $(OutDir)).
For example, to prepend C:\Windows\Temp to the PATH:
PATH=C:\WINDOWS\Temp;%PATH%
Similarly, to append $(TargetDir)\DLLS to the PATH:
PATH=%PATH%;$(TargetDir)\DLLS
You have a couple of options:
You can add the path to the DLLs to the Executable files settings under Tools > Options > Projects and Solutions > VC++ Directories (but only for building, for executing or debugging here)
You can add them in your global PATH environment variable
You can start Visual Studio using a batch file as I described here and manipulate the path in that one
You can copy the DLLs into the executable file's directory :-)
If you only need to add one path per configuration (debug/release), you could set the debug command working directory:
Project | Properties | Select Configuration | Configuration Properties
| Debugging | Working directory
Repeat for each project configuration.
Set the PATH variable, like you're doing. If you're running the program from the IDE, you can modify environment variables by adjusting the Debugging options in the project properties.
If the DLLs are named such that you don't need different paths for the different configuration types, you can add the path to the system PATH variable or to Visual Studio's global one in Tools | Options.
None of the answers solved exactly my problem (the solution file I was running was trying to find xcopy to copy a dll after generation).
What solved it for me was going into menu "Project -> Properties"
Then in the window that opens choosing on the left pane: "Configuration Properties -> VC++ Directories
On the right pane under "General" choosing "Executable Directories "
And then adding:
$(SystemRoot)\system32;$(SystemRoot);$(SystemRoot)\System32\Wbem;$(SystemRoot)\System32\WindowsPowerShell\v1.0\;$(ExecutablePath)

Resources