Adding Preprocessor directive dynamically from commandline build VS2008 - visual-studio

I am using VS2008, and developing C/C++ projects. I am using .bat file to build my projects from commandline (VC2k8 command prompt). I need a way to include preprossor directive dynamically at build time.
I am using devenv to build from command line.
>devenv my\project\path\myproject.sln /build release > logs\build.log
Actually I want to set a macro definition based on a command line parameter to the batch file. I can keep two different .vcproj files, but that gives problem in keeping multiple project/sln files.
My batch file would something like this...
if (condition)
#define MYPROC_ENABLE_MYMODULE "yes" // To be included in the project.
else
#define MYPROC_ENABLE_MYMODULE "no"
Any help would be really appreciated.
Thanks.

One option would be to set the CL environment variable, using something like:
set CL=/DMYPROC_ENABLE_MYMODULE
The C++ compiler (cl.exe) will add the contents of the CL environment variable to its command line when it runs.
I know you can define macros if you build using msbuild, but I'm not sure you can do the same when using devenv directly.

You can make different configurations for your solution and define different preprocessor flags for the different configurations. Then you would just need to select the configuration at the command line and no need for multiple solution or project files.

Related

How to call a custom build tool through a batch file

While trying to setup the Cap'n Proto compiler as a custom build tool in Visual Studio 2017, I came across a curious behavior, it only seems to work when called directly and not through a batch file.
What I first tried was, for each .capnp file, set the following Custom Build Tool settings:
Command line: "$(SolutionDir)run_capnpn.bat" compile -oc++ "%(FullPath)"
Description: Executing capnp.exe on %(Identity)...
Outputs: %(Identity).c++;%(Identity).h
I made the batch file because I wanted to avoid polluting my %PATH% with the capnp folder, this is what it contains:
#echo on
echo %*
SET PATH=%PATH%;C:\GitHub\capnproto\bin\c++\src\capnp\Debug
start /wait "" capnp.exe %*
exit /b %errorlevel%
However, with this setup the Custom Build Tool was only called on 1 of the 5 capnp files in my solution (all 5 files had exactly the same settings). I know this because only one pair of generated files appeared and only one message appeared in my build log.
Even weirder, if I compiled again it would do the next file, and on the following compile it would do another file. In all, it would take 5 compiles (one per file) before it considered everything to be fully built and stop calling the custom build tool.
After much trial and error and some help from other programmers on Discord, I tried adding capnp.exe to my path and call it directly (instead of going through the batch file) so for each capnp fil I changed the command line setting to:
capnp.exe compile -oc++ "%(FullPath)"
and now it all builds correctly. Is it possible to call a custom build tool through a batch file? and if so how?

How to make MSBuild print output from a custom tool?

I have a project, which use a .rules file to build some non-c++ sources. How to make MSBuild print diagnostic output from this tool? -- Precisely cerr stream.
(I silently presume, the M$ team have not reached that level of insanity, to give the custom building tools functionality without possibility to print errors from them.)
I've actually noticed you marked question with label visual-studio-2008. I guess it means your custom build tool processes non cpp files in vc2008 project? If so, I think it is not built by msbuild but with vcbuild.
In this case try to add echo on in front of command executed by custom tool.
Vcbuild creates batch files in %temp% that first line contains #echo off and then it is executed. So if you add echo on you should see complete output in output window including command line used for command execution.

Project path or other macro in command-line parameters for console project in VS2010

The debug tab of the project properties for a console application in VS2010 allows me to set command-line parameters to pass to the project whilst debugging.
I would like to set a parameter which is a path and the path is specific to each developer/machine, as it is a path which resides in the solution folder and each environment is different.
For pre- and post-build events, I can use macros such as $(ProjectDir), but I can't find a way to do this for command-line parameters - is there a way? A hack is fine, as long as it's not too awful!
Thanks
I haven't found a way to use $(ProjectDir) in the command line arguments, but you can access files contained within the project by:
Tell Visual Studio to copy specific files to the output directory by changing their "Copy to Output Directory" property.
Change your command line arguments from $(ProjectDir)/FileNeededDuringRuntime to FileNeededDuringRuntime.
This is more of a hack since it probably doesn't cover all the cases of using the variable, but it may get you by if you're just referencing a few files.
Macros can be used in command line arguments for C++ projects, see:
How to pass solution folder as parameter in command line arguments (for debug)?
You could have an empty C++ project "Set as StartUp Project" and change its "Configuration Properties -> Debugging -> Command" from "$(TargetPath)" (default for new projects) to "$(ProjectDir)..\OtherProjectRelativeDebugFolder\OtherProjectsOutputFileName.exe".
Since OtherProjectRelativeDebugFolder and OtherProjectsOutputFileName are relative and thus location independent you should be fine with that.
You said:
A hack is fine, as long as it's not too awful!
Is an empty project that produces an empty dll (unless you find a way to stop it, e.g. delete on post-build) too awful?
BTW. Environment variables aren't resolved in "Debug -> command line arguments" for C# either. I'll be experimenting on setting an environment variable, passing its name (because it isn't resolved) and reading it in the program. Passing the name is intended to show where the environment variable comes from, i.e. project settings.
Edit:
I hoped to find a way to set the environment variable to the value of a macro, e.g. in a build event. A simple shell "set" command is not persistent, so it didn't work out. Instead I was able to use a relative path as working folder to get things work for me. I also found a workaround that uses a file for persistent storage:
VS2010 - Project Macro Variables in Start Options Command Line Arguments

Finding build output in Visual Studio 6 (visual basic)

I realize this is going to be an exotic question, but I just can't find the answer.
I'm trying to fix up and enhance an old visual basic-based application.
My problem is: where is the output directory with the compiled binaries?
Thanks.
If some clarification is needed, please ask.
In the .VBP, if there is a line specifying the path like this
Path32="C:\"
Then the resulting EXE will be built at that location. Otherwise, it will be built in the same directory as the .VBP file. The path can be relational as well and may not be a fully qualified path.
I think you want the /outdir switch. This overrides the Path32 setting in the Project file.
VB6[.EXE] [[{/run | /r}] | [/runexit] | [{/make | /m}] projectname]
[/out filename] [/outdir path] [/d const=value{[:constN=valueN]}]
[/mdi | /sdi] [{/cmd argument | /c argument}]
Options:
/run or /r projectname Tells Visual Basic to compile projectname and run it,
using the arguments stored in the Command Line
Arguments field of the Make tab of the Project
Properties dialog box.
/runexit projectname Tells Visual Basic to compile projectname and run it.
Visual Basic will exit when the project returns to
design mode.
/make or /m projectname Tells Visual Basic to compile projectname and make an
executable file from it, using the existing settings
stored in the project file.
/out filename Specifies a file to receive errors when you build using
/m or /runexit. If you do not use /out, command line
bild errors are displayed in a message box.
/outdir path Specifies a directory path to place all output files in
when using /make. This path must already exist.
/d or /D const=value... Tells Visual Basic which values to use for conditional
compilation constants when making an .EXE or ActiveX
component with the /make switch. Separate multiple
constants with colons.
/cmd or /c argument Specifies a command string to be passed to the Command$
function. When used, it must be the last switch on the
command line.
/mdi or /sdi Changes the Visual Basic environment to either Single
Document Interface (SDI) or Multiple Document Interface
(MDI) mode. Visual Basic remains in this mode until
you change it.
/? Displays a list of valid command line switches.
You may use a group name in place of projectname in any of the above switches.
Works fine here.
There is no equivalent 'bin/lib' output directory for VB6. When you compile a VB6 project, the default is to compile to the same folder as the project (vbp file). It is also possible to compile to any other folder available to the user.

How to pass the assembly name as a command-line argument when debugging

I have an NUnit test assembly (a .NET DLL). When I click Run in Visual Studio I want it to launch NUnit and run the tests in this assembly. I can do all of that.
Instead of specifying the full assembly name and path in the command line arguments, does Visual Studio support some sort of macro that expands into that for the Command Line Arguments box? Most other development tools I have used support this, but I cannot find anything in the documentation about this.
I was expecting something like: %assembly_full_path%
The reason I want to do this is so if the assembly name or build location changes, then I don't have to update the command line arguments as well.
This doesn't work as far as I can tell. Macros in the Command Line arguments box do not get expanded. Not even environment variables. Bummer.
A workaround is to create a custom tool. Tools + External Tools, Add. Title = Run tests, Command = nunit.exe, Arguments = $(TargetPath), Initial Directory = $(TargetDir). Tweak as needed. You could assign a keystroke to this new tool command, even F5.
Using VS2005, the only item I need to provide in the Command Line Arguments is the name of the dll. I suspect VS sets the default working directory to the project's output directory, as I've never specified the path and yet the tests always load correctly.

Resources