Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass a preprocessor definition to the resource compiler through the command line?

I'm currently trying to switch between a few different default Icons in a Visual C++ .rc file using #ifdef tags.

The builds switching the #define value are being created through command line using MSBuild.

The difficulty I have been running into is that using Visual Studio 2010, in order to pass a preprocessor definition to the resource compiler you must define it in the project settings (Config Properties -> Resources -> General).

This makes it difficult to use an #ifdef tag because using this method it will always be defined in the resource compiler.

I would love to define it to a value, so that I might use a preprocessor #if SOMEVALUE == 4 might work, but Cannot seem to find a way to pass a Preprocessor definition + value to MSBuild via the command line.

Does anyone know a way to pass a preprocessor definition directly through to the resource compiler or a way to define a value for a preprocessor definition via commandline for msbuild?

like image 413
Maixy Avatar asked Aug 22 '11 15:08

Maixy


People also ask

Which option shows the valid pre processor definition?

The correct option is (c). Explanation: During preprocessing stage the line #include<stdio. h> with the system header file of that name gets replaced by the contents of file stdio.

What is the gcc option that runs only the preprocessor?

The -E option causes gcc to run the preprocessor, display the expanded output, and then exit without compiling the resulting source code.

What is preprocessor directive in Makefile?

The syntax is compiler specific, for gcc use the -D option like so: -Dcpp_variable . Show activity on this post. Take a variable in Makefile and whatever you need to define in it just add -DXXX. Where XXX in you case is cpp_variable.

Which gcc option Undefine a preprocessor macro?

4. Which gcc option undefines a preprocessor macro? Explanation: None.


2 Answers

Yes, this can be done.

Try using environment variables to pass values into your build process.

In your project properties add ;$(CMDLINE_DEFINES) to the end of your resource preprocessor definitions. (Be sure to pick the right configuration.)

Then when you use MSBuild from the command line type (or add to a batch file)...

C:\Projects\SomeProject> set CMDLINE_DEFINES=SOMETEST=42
C:\Projects\SomeProject> MSBuild SomeProject.vcproj

A batch file may look like:

@echo off
SET CMDLINE_DEFINES=%1
MSBUILD SomeProject.vcproj

Using this batch file, whatever you pass on the commandline will be passed on to the build process as your preprocessor macro(s).

like image 182
Kevin Williams Avatar answered Sep 27 '22 21:09

Kevin Williams


See the answer to this, with the additional step of setting up ResourceCompile options, i.e. edit your project file in a text editor to include elements like this:

<ItemDefinitionGroup>
    <ClCompile>
        <AdditionalOptions>/DERROR_LOG_LEVEL=5 %(AdditionalOptions)</AdditionalOptions>
    </ClCompile>
    <ResourceCompile>
        <AdditionalOptions>/DERROR_LOG_LEVEL=5 %(AdditionalOptions)</AdditionalOptions>
    </ResourceCompile>
</ItemDefinitionGroup>
like image 27
fakeleft Avatar answered Sep 27 '22 22:09

fakeleft