简体   繁体   English

static 库中的外部变量链接失败

[英]extern variable linking failure inside a static library

I have to use an ugly C-library inside my c++ application.我必须在我的 c++ 应用程序中使用一个丑陋的 C 库。 In the following explanation I will call it UglyLib.在下面的解释中,我将其称为 UglyLib。 I successfully compiled UglyLib as a statically linked library.我成功地将 UglyLib 编译为静态链接库。 In the file ugly.h UglyLib uses an extern variable:在文件 ugly.h 中,UglyLib 使用了一个外部变量:

file ugly.h:文件 ugly.h:

extern SomeStruct_type somestruct;

the variable is defined (and also used) in another file.该变量在另一个文件中定义(并使用)。 I will call it anotherugly.c.我将其称为另一个丑陋的东西。c。

file anotherugly.c:文件另一个丑陋的.c:

SomeStruct_type somestruct;

My c++ application is based on a generic template library (TemplateLib) and the application itself is composed by the main window GUI code and on an application library statically linked with the main window code.我的 c++ 应用程序基于通用模板库 (TemplateLib),应用程序本身由主要 window GUI 代码和与主要 window 代码静态链接的应用程序库组成。 I will call this statically linked library ApplicationLib.我将这个静态链接库称为 ApplicationLib。

TemplateLib contains a templatelibfile.h, that exports a function foo using somestruct, the extern variable exposed by UglyLib. TemplateLib 包含一个 templatelibfile.h,它使用 somestruct 导出一个 function foo,这是 UglyLib 公开的外部变量。

file templatelibfile.h:文件模板库文件.h:

#include<ugly.h>
...
void foo()
{
...
do something with somestruct
...
}

foo function is used by appllibfile.h contained in the statically linked ApplicationLib. foo function 由包含在静态链接的 ApplicationLib 中的 appllibfile.h 使用。

file appllibfile.h:文件 appllibfile.h:

#include<templatelibfile.h>
...
void applfoo()
{
...
foo();
...
}

Main Window application includes appllibfile.h主要 Window 应用程序包括 appllibfile.h

file main.cpp:文件 main.cpp:

#include<appllibfile.h>
...
int main()
{
...
applfoo();
...
return 0;
}

In VS2008 when I try to compile the main window application, the microsoft linker give me this error在 VS2008 中,当我尝试编译主要的 window 应用程序时,微软 linker 给我这个错误

error LNK2001: unresolved external symbol "struct SomeStruct_type somestruct" (?somestruct@@3USomeStruct_type@@A) error LNK2001: 未解析的外部符号“struct SomeStruct_type somestruct” (?somestruct@@3USomeStruct_type@@A)

If I add in templatefile.ha new definition of the extern variable the compiler stops to complain.如果我在 templatefile.ha 中添加外部变量的新定义,编译器将停止抱怨。

file templatelibfile.h:文件模板库文件.h:

#include<ugly.h>
SomeStruct_type somestruct
...
void foo()
{
...
do something with somestruct;
...
}

BUT I would wish to avoid it because I don't know if it is the correct thing to do (I don't want to risk to alter the semantic of UglyLib redefining a different instance of somestruct variable).我希望避免它,因为我不知道这样做是否正确(我不想冒险改变 UglyLib 的语义重新定义 somestruct 变量的不同实例)。 Have you some suggestions in order to avoid the linking problem without redefining the somestruct extern variable?您是否有一些建议可以在不重新定义 somestruct 外部变量的情况下避免链接问题? Thanks a lot!非常感谢!

It's probably because of the name-mangling that the C++ compiler does.这可能是因为 C++ 编译器进行了名称修改。 Since anotherugly.c is a C source (presumably compiled with a C compiler), the symbol somestruct will be exposed without being mangled.由于anotherugly.c是一个 C 源代码(大概是用 C 编译器编译的),符号somestruct将被暴露而不会被破坏。 When you compile the rest of the files with a C++ compiler, the linker then looks for a mangled name which does not exist.当您使用 C++ 编译器编译文件的 rest 时,linker 会查找不存在的错位名称。

I think surrounding the declaration in ugly.h in extern "C" might solve the problem.我认为围绕extern "C"ugly.h中的声明可能会解决问题。

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

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