Solved

How can I conditionally link in files using MSVC6

Posted on 2004-08-10
10
233 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
  • 6
  • 4
10 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:hybrid_skeeter
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:hybrid_skeeter
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
Thanks Paul,

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

Scott
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
posix semaphore deadlock 13 108
outside company writing a dll in c/c++, I want to call it from C# 2 63
C++ vs C compilers 13 155
Need example 5 100
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements 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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now