调试链接错误的最佳实践

时间:2022-03-13 06:10:22

When building projects in C++, I've found debugging linking errors to be tricky, especially when picking up other people's code. What strategies do people use for debugging and fixing linking errors?

在使用c++构建项目时,我发现调试链接错误非常困难,尤其是在获取其他人的代码时。人们使用什么策略来调试和修复链接错误?

3 个解决方案

#1


22  

Not sure what your level of expertise is, but here are the basics.

不确定你的专业水平是多少,但是这里有一些基本的东西。

Below is a linker error from VS 2005 - yes, it's a giant mess if you're not familiar with it.

下面是来自VS 2005的链接错误——是的,如果你不熟悉的话,这是一个巨大的混乱。

ByteComparator.obj : error LNK2019: unresolved external symbol "int __cdecl does_not_exist(void)" (?does_not_exist@@YAHXZ) referenced in function "void __cdecl TextScan(struct FileTextStats &,char const *,char const *,bool,bool,__int64)" (?TextScan@@YAXAAUFileTextStats@@PBD1_N2_J@Z)

There are a couple of points to focus on:

有几个要点值得关注:

  • "ByteComparator.obj" - Look for a ByteComparator.cpp file, this is the source of the linker problem
  • “ByteComparator。寻找一个ByteComparator。cpp文件,这是链接器问题的根源。
  • "int __cdecl does_not_exist(void)" - This is the symbol it couldn't find, in this case a function named does_not_exist()
  • “int __cdecl does_not_exist(空白)”-这是它找不到的符号,在本例中,一个名为do _not_exist()的函数

At this point, in many cases the fastest way to resolution is to search the code base for this function and find where the implementation is. Once you know where the function is implemented you just have to make sure the two places get linked together.

此时,在许多情况下,解决问题的最快方法是搜索这个函数的代码库,并找到实现的位置。一旦你知道函数在哪里实现,你只需要确保这两个地方连接在一起。

If you're using VS2005, you would use the "Project Dependencies..." right-click menu. If you're using gcc, you would look in your makefiles for the executable generation step (gcc called with a bunch of .o files) and add the missing .o file.

如果您正在使用VS2005,您将使用“项目依赖项…”右键单击菜单。如果您正在使用gcc,那么您将在makefile中查找可执行生成步骤(gcc调用了一堆.o文件)并添加缺失的.o文件。


In a second scenario, you may be missing an "external" dependency, which you don't have code for. The Win32 libraries are often times implemented in static libraries that you have to link to. In this case, go to MSDN or "Microsoft Google" and search for the API. At the bottom of the API description the library name is given. Add this to your project properties "Configuration Properties->Linker->Input->Additional Dependencies" list. For example, the function timeGetTime()'s page on MSDN tells you to use Winmm.lib at the bottom of the page.

在第二个场景中,您可能会丢失一个“外部”依赖项,而您没有代码来支持这个依赖项。Win32库通常是在必须链接的静态库中实现的。在这种情况下,访问MSDN或“Microsoft谷歌”并搜索API。在API描述的底部给出库名。将此添加到项目属性“配置属性—>链接器—>输入—>附加依赖项”列表中。例如,MSDN上的函数timeGetTime()页面告诉您使用Winmm。lib在页面底部。

#2


3  

The C-runtime libraries are often the biggest culprit. Making sure all your projects have the same settings wrt single vs multi-threading and static vs dll.

c运行时库通常是最大的罪魁祸首。确保你所有的项目都有相同的设置wrt单线程vs多线程和静态vs dll。

The MSDN documentation is good for pointing out which lib a particular Win32 API call requires if it comes up as missing.

MSDN文档可以很好地指出,如果某个特定的Win32 API调用出现缺失时需要哪个库。

Other than that it usually comes down to turning on the verbose flag and wading through the output looking for clues.

除此之外,它通常归结为打开冗长的标志并在输出中寻找线索。

#3


2  

One of the common linking errors I've run into is when a function is used differently from how it's defined. If you see such an error you should make sure that every function you use is properly declared in some .h file.
You should also make sure that all the relevant source files are compiled into the same lib file. An error I've run into is when I have two sets of files compiled into two separate libraries, and I cross-call between libraries.

我遇到的一个常见的链接错误是,当一个函数与它的定义方式不同时。如果您看到这样的错误,您应该确保在一些.h文件中正确地声明了您使用的每个函数。您还应该确保所有相关的源文件都被编译成相同的库文件。我遇到的一个错误是,当我将两组文件编译成两个独立的库时,我在库之间进行交叉调用。

Is there a failure you have in mind?

你有失败的想法吗?

#1


22  

Not sure what your level of expertise is, but here are the basics.

不确定你的专业水平是多少,但是这里有一些基本的东西。

Below is a linker error from VS 2005 - yes, it's a giant mess if you're not familiar with it.

下面是来自VS 2005的链接错误——是的,如果你不熟悉的话,这是一个巨大的混乱。

ByteComparator.obj : error LNK2019: unresolved external symbol "int __cdecl does_not_exist(void)" (?does_not_exist@@YAHXZ) referenced in function "void __cdecl TextScan(struct FileTextStats &,char const *,char const *,bool,bool,__int64)" (?TextScan@@YAXAAUFileTextStats@@PBD1_N2_J@Z)

There are a couple of points to focus on:

有几个要点值得关注:

  • "ByteComparator.obj" - Look for a ByteComparator.cpp file, this is the source of the linker problem
  • “ByteComparator。寻找一个ByteComparator。cpp文件,这是链接器问题的根源。
  • "int __cdecl does_not_exist(void)" - This is the symbol it couldn't find, in this case a function named does_not_exist()
  • “int __cdecl does_not_exist(空白)”-这是它找不到的符号,在本例中,一个名为do _not_exist()的函数

At this point, in many cases the fastest way to resolution is to search the code base for this function and find where the implementation is. Once you know where the function is implemented you just have to make sure the two places get linked together.

此时,在许多情况下,解决问题的最快方法是搜索这个函数的代码库,并找到实现的位置。一旦你知道函数在哪里实现,你只需要确保这两个地方连接在一起。

If you're using VS2005, you would use the "Project Dependencies..." right-click menu. If you're using gcc, you would look in your makefiles for the executable generation step (gcc called with a bunch of .o files) and add the missing .o file.

如果您正在使用VS2005,您将使用“项目依赖项…”右键单击菜单。如果您正在使用gcc,那么您将在makefile中查找可执行生成步骤(gcc调用了一堆.o文件)并添加缺失的.o文件。


In a second scenario, you may be missing an "external" dependency, which you don't have code for. The Win32 libraries are often times implemented in static libraries that you have to link to. In this case, go to MSDN or "Microsoft Google" and search for the API. At the bottom of the API description the library name is given. Add this to your project properties "Configuration Properties->Linker->Input->Additional Dependencies" list. For example, the function timeGetTime()'s page on MSDN tells you to use Winmm.lib at the bottom of the page.

在第二个场景中,您可能会丢失一个“外部”依赖项,而您没有代码来支持这个依赖项。Win32库通常是在必须链接的静态库中实现的。在这种情况下,访问MSDN或“Microsoft谷歌”并搜索API。在API描述的底部给出库名。将此添加到项目属性“配置属性—>链接器—>输入—>附加依赖项”列表中。例如,MSDN上的函数timeGetTime()页面告诉您使用Winmm。lib在页面底部。

#2


3  

The C-runtime libraries are often the biggest culprit. Making sure all your projects have the same settings wrt single vs multi-threading and static vs dll.

c运行时库通常是最大的罪魁祸首。确保你所有的项目都有相同的设置wrt单线程vs多线程和静态vs dll。

The MSDN documentation is good for pointing out which lib a particular Win32 API call requires if it comes up as missing.

MSDN文档可以很好地指出,如果某个特定的Win32 API调用出现缺失时需要哪个库。

Other than that it usually comes down to turning on the verbose flag and wading through the output looking for clues.

除此之外,它通常归结为打开冗长的标志并在输出中寻找线索。

#3


2  

One of the common linking errors I've run into is when a function is used differently from how it's defined. If you see such an error you should make sure that every function you use is properly declared in some .h file.
You should also make sure that all the relevant source files are compiled into the same lib file. An error I've run into is when I have two sets of files compiled into two separate libraries, and I cross-call between libraries.

我遇到的一个常见的链接错误是,当一个函数与它的定义方式不同时。如果您看到这样的错误,您应该确保在一些.h文件中正确地声明了您使用的每个函数。您还应该确保所有相关的源文件都被编译成相同的库文件。我遇到的一个错误是,当我将两组文件编译成两个独立的库时,我在库之间进行交叉调用。

Is there a failure you have in mind?

你有失败的想法吗?