简体   繁体   English

未引用功能的“未解析的外部符号”

[英]“unresolved external symbol” for unreferenced function

I'm in Visual Studio 2003. I have a function in a very common module which requires 3 other modules. 我在Visual Studio 2003中。我在一个非常常见的模块中有一个功能,该模块需要其他3个模块。 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. 我只希望使用新功能的项目必须包括其他3个模块,而那些不引用该功能的模块就不会出现“未解决的外部符号”错误。 I tried function level linking, OPT:REF and every project setting I could think of, but the linker always complains. 我尝试了函数级链接,OPT:REF和我能想到的每个项目设置,但是链接器始终会抱怨。 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. 只要项目从不调用a(),我就需要项目仅使用main.cpp和a.cpp进行编译。 If a() is called in _tmain(), then of course b.cpp would have to be added to the project. 如果在_tmain()中调用了a(),则当然必须将b.cpp添加到项目中。

The linker doesn't seem to apply the OPT:REF until after it is sure EVERY function referenced ANYWHERE is in the project. 直到确定项目中引用了ANYWHERE的每个函数之后,链接器才似乎未应用OPT:REF。 Even if it (b()) is referenced in an unreferenced function (a()). 即使在未引用的函数(a())中引用了(b())。

您是否考虑过建立一个可选函数库,它是三个依赖项?

附带地,用#ifdef包围可疑的a()调用,该调用在b()旁边寻找#define d听起来很有希望。

I have a function in a very common module which requires 3 other modules. 我在一个非常常见的模块中需要一个功能,该模块需要其他3个模块。 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. 我只希望使用新功能的项目必须包括其他3个模块,而那些不引用该功能的模块就不会出现“未解决的外部符号”错误。

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. 否则,您将陷入某种只会导致麻烦的条件编译宏中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM