?
Solved

How can I conditionally link in files using MSVC6

Posted on 2004-08-10
10
Medium Priority
?
240 Views
Last Modified: 2010-05-18
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.
0
Comment
Question by:hybrid_skeeter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11761774
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
 

Author Comment

by:hybrid_skeeter
ID: 11761839
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11761910
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:hybrid_skeeter
ID: 11762393
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11762603
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
 

Author Comment

by:hybrid_skeeter
ID: 11762683
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11762861
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
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 2000 total points
ID: 11762895
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
 

Author Comment

by:hybrid_skeeter
ID: 11762929
Thanks Paul,

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

Scott
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11763054
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question