engf-010
engf-010

Reputation: 3929

One source with multiple objects

One source with multiple objects I am using MSVS 2010 and I have a C++ source-file which must compile into 2 object-files. The diiference between those compilations is a "#define UNICODE" for one of them but not for the other. I can't (and don't want to) use templates for a this.

Currently ,I use 3 source files for this in my project. The actual source is excluded from build ,while the other 2 are wrappers around it. Like this :

file = wrap-UNICODE.cpp

#ifndef UNICODE
#define UNICODE
#endif

#include "actual-source.cpp"
// eof

file = wrap-ANSI.cpp

#ifdef UNICODE
#undef UNICODE
#endif

#include "actual-source.cpp"
// eof

When using makefiles i can easily avoid the use of wrapper soucrces ,using different output switches. My question is ,I would like to know if (and how) i can do this directly in a MSVS project.

Upvotes: 3

Views: 340

Answers (4)

anatolyg
anatolyg

Reputation: 28300

If i correctly understand what you want to do, this is possible. I have MS Visual Studio 2005 Standard Edition; here is how i can do this (you might have to adjust this if you have a different version, or possibly it might even not work in your version; i hope your computer doesn't explode :) ).

The first step requires manual editing of the project file. Open the project file (it is called stuff.vcproj on my machine) and replicate the lines that mention your file:

<File
    RelativePath=".\actual-source.cpp"
    >
</File>
<File
    RelativePath=".\actual-source.cpp"
    >
</File>

Then, load the project into MSVS. Go to the Solution Explorer (Ctrl+Alt+L on my machine); the project will show two files with identical name. Open the Property Pages of each one (Alt+F7 on my machine) and add any differences you want (e.g. Preprocessor Definitions).

You must also set different names for object files: choose Output Files, Object File Name in the same window (Property Pages), and add different names (e.g. actual-source-unicode; MSVS will add the .obj extension when compiling). If you don't do that, the two obj-files will have the same name, and one will overwrite the other.

Upvotes: 1

Jon
Jon

Reputation: 437734

Why don't you simply use different configurations for your two builds (as Luca Martini mentions) and then use Batch Build? You can then compare the compiled outputs any way you want.

Upvotes: 0

J&#246;rgen Sigvardsson
J&#246;rgen Sigvardsson

Reputation: 4897

Visual Studio defines _UNICODE for you if you intend to build unicode apps.

Upvotes: 0

Luca Martini
Luca Martini

Reputation: 1474

AFAIK you can create multiple builds in your solution. Just go in the configuration manager of the solution (should be accessible from contextual menu on the solution).

In this way you can also avoid to have two versions of your file. It is sufficient to set the define options differently in the two configurations.

Upvotes: 0

Related Questions