“未解析的外部符号”对于未引用的函数
我使用的是 Visual Studio 2003。我在一个非常常见的模块中有一个函数,它需要 3 个其他模块。我只希望使用新函数的项目必须包含其他 3 个模块,以及那些不引用该函数的项目,以在没有“无法解析的外部符号”错误的情况下进行链接。我尝试了功能级链接、OPT:REF 和我能想到的每个项目设置,但链接器总是抱怨。我做了一个简单的例子来测试。任何想法都很棒...
//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()
{
}
我需要项目中只有 main.cpp 和 a.cpp 才能正常编译,只要 a() 从未被调用。如果在_tmain()中调用a(),那么当然必须将b.cpp添加到项目中。
链接器似乎不会应用 OPT:REF,直到确定项目中任何地方引用的每个函数都在之后。即使它 (b()) 在未引用的函数 (a()) 中被引用。
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()).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否考虑过制作一个可选函数及其三个依赖项的库?
Did you consider making a library of the optional function and it's three dependencies?
在我看来,您应该将这个新功能分离到不同的模块中。 (不要将其放在公共模块中)。这样,谁需要它就可以包含它,谁不需要就不会包含它。否则,您将陷入某种只会导致麻烦的条件编译宏中。
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.
顺便说一句,用
#ifdef
围绕有问题的 a() 调用,在b()
旁边查找#define
d ,听起来很有希望。Offhand, surrounding the questionable a() call with an
#ifdef
that looks for something#define
d alongsideb()
sounds promising.