views:

242

answers:

3

I'm in Visual Studio 2003. I have a function in a very common module which requires 3 other modules. I want only projects using the new function to have to include the 3 other modules, and those that don't reference the function to link without "unresolved external symbol" errors. I tried function level linking, OPT:REF and every project setting I could think of, but the linker always complains. I made a simple example for testing. Any ideas would be awesome...

//main.cpp
//#include "a.h"
int _tmain(int argc, _TCHAR* argv[])
{
  //a();
  return 0;
}

//a.h
#include "b.h"
void a();

//a.cpp
#include "a.h"
#include "b.h"
void a()
{
  b();
}

//b.h
void b();

//b.cpp
#include "b.h"
void b()
{
}

I need for the project to compile fine with only main.cpp and a.cpp in the project as long as a() is never called. If a() is called in _tmain(), then of course b.cpp would have to be added to the project.

The linker doesn't seem to apply the OPT:REF until after it is sure EVERY function referenced ANYWHERE is in the project. Even if it (b()) is referenced in an unreferenced function (a()).

A: 

Offhand, surrounding the questionable a() call with an #ifdef that looks for something #defined alongside b() sounds promising.

VoteyDisciple
I thought about that, but I like to keep the predefine list to a minimum. I guess I was hoping there was some VS setting or feature of c++ I was missing.
Cowtung
A: 

I have a function in a very common module which requires 3 other modules. I want only projects using the new function to have to include the 3 other modules, and those that don't reference the function to link without "unresolved external symbol" errors.

It sounds to me that you should be separating this new function out into a different module. (Don't put it in the common module). That way, whoever needs it can include it, whoever doesn't, won't. Otherwise, you'll be stuck with some kind of conditional compilation macros that can only lead to trouble.

zdan
I added encrypt/decrypt functions to a string_utilities module. I guess monolithic modules that do everything aren't ideal.
Cowtung
Also, I agree about the conditional compilation macros not being ideal.
Cowtung
+1  A: 

Did you consider making a library of the optional function and it's three dependencies?

Jay
That is ultimately what we'll be doing later. The common module I'm talking about is called string_utilities.h, which gets used all over the place. I guess I was hoping there was an easy fix that didn't require modifying a ton of projects. The thing I don't like about libs is that you can get into other trouble sometimes with differing project settings and junk. string_utilities.h/cpp was just supposed to be an easy quick thing included everywhere.
Cowtung