如何在lib错误中得到未解析的外部?

时间:2022-09-27 04:54:42

yes I actually want to get that error. I am using MSVC (the command prompt). I would like to have .lib that would require definition of external symbol that gets linked to it. I must understand something wrong about static linking, because to me my approach seems legit:

是的,我真的想得到那个错误。我正在使用MSVC(命令提示符)。我想要.lib,它需要定义与之链接的外部符号。我必须理解静态链接的错误,因为对我而言,我的方法似乎是合法的:

I have a file that looks roughly like this:

我有一个看起来大致如下的文件:

extern INFO_BLOCK user_setup;

int
crtInit()
{
    SetInfoBlock(&user_setup);
    return 0;
}

when I try to use the .obj of this file in compilation with the main module cl main.c file.obj it says unresolved externals. That is desired behaviour. Nevertheless, once I pack the file.obj with lib file.obj even using /include:user_data (which frankly I don't trust as being of any use in this case)

当我尝试在主模块cl main.c file.obj的编译中使用此文件的.obj时,它表示未解析的外部。这是理想的行为。然而,一旦我使用/ include:user_data将file.obj与lib file.obj打包在一起(坦率地说,我不相信在这种情况下有任何用处)

using the .lib with cl main.c /link file.lib does not generate missing externals and that is the problem. I need the programmer to define that symbol. Does extern get stripped away once you put your .obj in .lib? Where am I wrong?

使用带有cl main.c / link file.lib的.lib不会生成缺少的外部,这就是问题所在。我需要程序员来定义该符号。将.obj放入.lib后,extern会被删除吗?我哪里错了?

1 个解决方案

#1


2  

If main.c does not contain any reference to crtInit there is no reason for the linker to pull that function into the generated binary - Thus it will not "see" the unresolved reference to user_setup at all.

如果main.c不包含对crtInit的任何引用,则链接器没有理由将该函数拉入生成的二进制文件中 - 因此它根本不会“看到”对user_setup的未解析引用。

When you mention an object file to the linker, you force it to include the object file into the binary, regardless of whether it is needed by the program or not.

当您向链接器提及目标文件时,强制它将目标文件包含到二进制文件中,而不管程序是否需要它。

Opposed to that, when you mention a library to the linker, it will only use the library to resolve unresolved references you already have at that point with object files that it pulls in from this library. In case nothing is unresolved up to this point (or not satisfied by any symbol in the library), the linker will not use anything from the library at all.

与此相反,当您向链接器提及库时,它将仅使用该库来解析此时已经存在的未解析引用,其中包含从此库中引入的对象文件。如果到目前为止还没有解决任何问题(或者库中的任何符号都不满意),链接器根本不会使用库中的任何内容。

The above is also the reason why many linkers are a bit picky about the order of libraries when linking (typically from specific to generic - or "user" to "system"), because linkers are normally single pass and will only pull in what they "see" needed at this specific point in the linking process.

以上也是为什么许多链接器在链接时(通常从特定到通用 - 或“用户”到“系统”)对库的顺序有点挑剔的原因,因为链接器通常是单通道,只会拉入它们的内容在链接过程中的这个特定点“需要”。

#1


2  

If main.c does not contain any reference to crtInit there is no reason for the linker to pull that function into the generated binary - Thus it will not "see" the unresolved reference to user_setup at all.

如果main.c不包含对crtInit的任何引用,则链接器没有理由将该函数拉入生成的二进制文件中 - 因此它根本不会“看到”对user_setup的未解析引用。

When you mention an object file to the linker, you force it to include the object file into the binary, regardless of whether it is needed by the program or not.

当您向链接器提及目标文件时,强制它将目标文件包含到二进制文件中,而不管程序是否需要它。

Opposed to that, when you mention a library to the linker, it will only use the library to resolve unresolved references you already have at that point with object files that it pulls in from this library. In case nothing is unresolved up to this point (or not satisfied by any symbol in the library), the linker will not use anything from the library at all.

与此相反,当您向链接器提及库时,它将仅使用该库来解析此时已经存在的未解析引用,其中包含从此库中引入的对象文件。如果到目前为止还没有解决任何问题(或者库中的任何符号都不满意),链接器根本不会使用库中的任何内容。

The above is also the reason why many linkers are a bit picky about the order of libraries when linking (typically from specific to generic - or "user" to "system"), because linkers are normally single pass and will only pull in what they "see" needed at this specific point in the linking process.

以上也是为什么许多链接器在链接时(通常从特定到通用 - 或“用户”到“系统”)对库的顺序有点挑剔的原因,因为链接器通常是单通道,只会拉入它们的内容在链接过程中的这个特定点“需要”。