• C

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.
hybrid_skeeterAsked:
Who is Participating?
 
PaulCaswellCommented:
Another possibility would be to set up two projects, one with SerialLayer and the otrher with SocketLayer but sharing everything else. This may end up a bit tidier.

Paul
0
 
PaulCaswellCommented:
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
0
 
hybrid_skeeterAuthor Commented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
PaulCaswellCommented:
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
0
 
hybrid_skeeterAuthor Commented:
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.
0
 
PaulCaswellCommented:
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
0
 
hybrid_skeeterAuthor Commented:
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.
0
 
PaulCaswellCommented:
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
0
 
hybrid_skeeterAuthor Commented:
Thanks Paul,

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

Scott
0
 
PaulCaswellCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.