Link to home
Start Free TrialLog in
Avatar of hybrid_skeeter
hybrid_skeeter

asked on

How can I conditionally link in files using MSVC6

Hi,

I am using Microsoft Visual C++ v6.

I am writing an application that communicates with a server over a socket. So for testing purposes I am using winsock. However it will be implemented using a serial interface to a GSM card.

Ideally I would like to swap out what files are linked in by using a preprocessor definition, so how can I tell it to link one set of files by specifying one preprocessor definition, or link in another set by specifying another definition.
Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

You have two options.

1. Use the 'Debug' and 'Release' settings as different.

2. Use the Pre-Link step or the Post-Build step to perform the different links.
You may find just a 'Copy' command in the pre-link step will suffice.

Paul
Avatar of hybrid_skeeter
hybrid_skeeter

ASKER

Hi Paul,

I really like the sound of option 2. Can you please give me an example of how to use the pre-link step to do this.

I have never used these options before and could use a firm point in the right Direction.

Cheers

Scott
Scott,

The pre-link step is just a sequence of DOS commands. You can do anything there. I'd recomment 'SET' as a good one to begin with so you can see what environment variables are available.

Paul
Hi Paul,

Sorry, but I still do not understand how I am to use a DOS command to tell MSVC to link in one set of files or another set of files.

I have both sets of files in the project explorer at the moment, and I would like to link just one set based on a preprocessor definition.
I'd copy the library you want to use to a known name.

E.G.
Copy Socket.lib UseThis.Lib
or
Copy GSM.Lib UseThis.lib

Then the link phase can use:

link .... UseThis.lib ...

Note that preprocessor directives are not available in the pre-link or link phase so you will need to use a different method. You could use: 'Copy Socket.lib UseThis.Lib'
in the 'Debug' settings and 'Copy GSM.Lib UseThis.lib' in the 'Release' settings.

Paul
Ah, I see where you are going here!

I am not trying to link in a lib file though. I have the following scenario :

**********Project Explorer
Source Files
   Main.c
   SerialLayer.c
   SocketLayer.c

Header files
   SerialLayer.h
   SocketLayer.h

*****************

SerialLayer and SocketLayerfiles have the same interface and I would like to choose at compile time which ones to link in.
Ahh!

I'd try something like:

In SerialLayer.c, surround the whole code with

#if SERIAL
// ALL the code, and I mean ALL of it, including the header comments and #includes.
#endif

Similarly for SocketLayer.c use

#if !SERIAL
...
#endif

Then, depending on the definition of SERIAL in your settings you will either use the serial interface or the socket interface. No hassle with the linker at all.

Paul
ASKER CERTIFIED SOLUTION
Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Paul,

That makes alot of sense, guess I was trying to over-complicate it.

Scott
Good choice.

Be careful if you use SourceSafe. It has problems with projects that use sources from outside the project path. Tell SourceSafe to create a link between the files if this is the case.

Paul