views:

45

answers:

2

Hi all,

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.

A: 

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.

James McNellis
Would devenv/msbuild pick up whether a rebuild would be required? Say you build with the default project options then set `CL` and build again. Would it rebuild the entire project? Or do you have to explicitly clean and build?
the_mandrill
@the_mandrill: That's a good question that would probably be better answered by someone more experienced with VCBuild. My guess would be that rebuilds should be handled correctly, but again, I am not sure.
James McNellis
Thanks a lot James.It is working fine for me.
Muthukkumaran
Thanks all, for your valuable inputs. I got it now, and it is working fine for my projects.
Muthukkumaran
A: 

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.

bshields
That solution works better if you don't have many projects to keep in sync and you don't need many different sorts of configurations. Otherwise James's option is more flexible.
the_mandrill
@the_mandrill Yeah you're right you would get into a combinatorial problem if you had a bunch of different flags that you wanted to be able to set independently. You would have to come up with configuration names for each unique combination of flags that you wanted to use. But for anything simpler than that I think the better tool support would make configurations the better choice.
bshields
It would have the benefit though of ensuring that the project would be rebuilt correctly. So I guess the choice of either of these methods will depend on the individual requirements.
the_mandrill
I have 9 projects within the solution. Please let me know how to set up multiple solution configurations..?Are you talikg about user defined build configurations for each projects...?
Muthukkumaran
Right-click on the solution in the IDE and choose Configuration Manager from the menu. You can create a configuration for the solution or for any individual project, the defaults are Debug and Release. For each project configuration you could define different pre-processor flags. So you could make Release_MyModule configuration for one of your projects and have that define MYPROC_ENABLE_MYMODULE. Then you would have a Release_MyModule solution configuration that had all the same project configurations as Release except it would have Release_MyModule for the one project.
bshields
Then from the command line you could do: `devenv my\project\path\myproject.sln /build Release_MyModule > logs\build.log`
bshields
Thanks bshields. It is working fone for me. Also a small change in my command line build script. Thanks again. You guys rocks!
Muthukkumaran