c链接器标志及其文档

时间:2021-12-31 04:51:33

I added the header <math.h> to a project to start to test out the fmod function, which returns a fractional part of float division. I have more questions on that, but i'll save it for another question.

我将标题 添加到项目中以开始测试fmod函数,该函数返回浮点除法的小数部分。我对此有更多的疑问,但我会将其保存为另一个问题。

So after adding the header and using the function, no lint errors went off then I try to build the program.

所以在添加标题并使用该函数后,没有lint错误消失,然后我尝试构建程序。

I get this error:

我收到此错误:

/usr/bin/ld: CMakeFiles/main.dir/main.c.o: undefined reference to symbol 'fmod@@GLIBC_2.2
.5'
//lib/x86_64-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

okay so getting better at reading c errors i noticed ld which means linker, okay fine.

好吧所以在阅读c错误方面越来越好我注意到ld这意味着链接器,好吧。

Next I start googling the error and I find the answer. I need to add the linker flag -lm

接下来我开始谷歌搜索错误,我找到了答案。我需要添加链接器标志-lm

why? I don't know so I start searching google what that linker flag means and I didn't really find any answers. Went back to SO and found this question and answer.

为什么?我不知道所以我开始搜索谷歌链接标志的含义,我没有找到任何答案。回到SO,发现了这个问题和答案。

these were the quotes under the main question.

这些是主要问题下的引用。

Read the library documentation ... – pmg

阅读图书馆文档... - pmg

...and then google. – Ori Pessach

...然后谷歌。 - Ori Pessach

linking semantics vary across operating systems and compilers. We'd need a lot more detail to make any useful contributions. – radical7

链接语义因操作系统和编译器而异。我们需要更多细节才能做出有用的贡献。 - 激进7

pkg-config seems okay if I have a one off file but as a project grows if you get these linker errors it becomes hard to figure out what's wrong.

pkg-config似乎没问题,如果我有一个关闭文件但是如果你得到这些链接器错误项目增长,很难弄清楚什么是错的。

Another answer was read the docs. I went back to google and came across this documentation about math.h

另一个答案是阅读文档。我回到google并看到了关于math.h的文档

but nowhere does it say anything about the actual linker flags to use.

但它没有说明要使用的实际链接器标志。

So my question what documentation was @pmg talking about in his reply? What's a general purpose way to find this documentation on my system just in case I cannot use google for whatever reason? Especially since I have the files on my computer and able to include them.

所以我的问题是@pmg在他的回复中谈到了什么文件?在我的系统上找到这个文档的通用方法是什么,万一我不能出于任何原因使用谷歌?特别是因为我的计算机上有文件并能够包含它们。

2 个解决方案

#1


1  

Basically you always have to link the library when you use a function included in the library. There are some libraries, the most used, that are linked by default (see Why do you have to link the math library in C?) by the compiler.

基本上,当您使用库中包含的功能时,您始终必须链接库。有一些库,最常用的,默认链接(请参阅为什么必须在C中链接数学库?)由编译器。

If you are on a Unix like system you can refer to man pages:

如果您使用的是类Unix系统,可以参考手册页:

man func

or

要么

man -k func

the last gives you all the man pages related to func. There are also general man pages as the one for all the math functions (man math) or the ascii table (man ascii).

最后一个给你所有与func相关的手册页。还有一般手册页作为所有数学函数(人数学)或ascii表(man ascii)。

Unfortunately not all man pages report the needed link to add to the command line. In the man page for math there is no explicit state of -lm however, in the very end, the name of the library is reported:

遗憾的是,并非所有手册页都报告了添加到命令行所需的链接。在数学的手册页中没有明确的-lm状态,但最后,报告了库的名称:

"The libm functions declared in math.h provide mathematical library functions"

“在math.h中声明的libm函数提供了数学库函数”

This is enough because, if the name of the library is libm then you know that to link it you should use:

这就足够了,因为如果库的名称是libm,那么你知道要链接它你应该使用:

-lm

As the general rule is that the linker remove the lib prefix when linking with -l, see for example this http://www.cs.swarthmore.edu/~newhall/unixhelp/howto_C_libraries.html

一般规则是链接器在与-l链接时删除lib前缀,例如参见http://www.cs.swarthmore.edu/~newhall/unixhelp/howto_C_libraries.html

#2


1  

Well, if you type man fmod into Google (man is the UNIX help system) and click the first link you get documentation about the function. This clearly states what headers you must include and what library you must link with:

好吧,如果你输入man fmod到Google(man是UNIX帮助系统)并点击第一个链接就可以获得有关该功能的文档。这清楚地说明了您必须包含哪些标题以及您必须链接的库:

Synopsis

#include <math.h>

double fmod(double x, double y);
float fmodf(float x, float y);
long double fmodl(long double x, long double y);

Link with -lm.

#1


1  

Basically you always have to link the library when you use a function included in the library. There are some libraries, the most used, that are linked by default (see Why do you have to link the math library in C?) by the compiler.

基本上,当您使用库中包含的功能时,您始终必须链接库。有一些库,最常用的,默认链接(请参阅为什么必须在C中链接数学库?)由编译器。

If you are on a Unix like system you can refer to man pages:

如果您使用的是类Unix系统,可以参考手册页:

man func

or

要么

man -k func

the last gives you all the man pages related to func. There are also general man pages as the one for all the math functions (man math) or the ascii table (man ascii).

最后一个给你所有与func相关的手册页。还有一般手册页作为所有数学函数(人数学)或ascii表(man ascii)。

Unfortunately not all man pages report the needed link to add to the command line. In the man page for math there is no explicit state of -lm however, in the very end, the name of the library is reported:

遗憾的是,并非所有手册页都报告了添加到命令行所需的链接。在数学的手册页中没有明确的-lm状态,但最后,报告了库的名称:

"The libm functions declared in math.h provide mathematical library functions"

“在math.h中声明的libm函数提供了数学库函数”

This is enough because, if the name of the library is libm then you know that to link it you should use:

这就足够了,因为如果库的名称是libm,那么你知道要链接它你应该使用:

-lm

As the general rule is that the linker remove the lib prefix when linking with -l, see for example this http://www.cs.swarthmore.edu/~newhall/unixhelp/howto_C_libraries.html

一般规则是链接器在与-l链接时删除lib前缀,例如参见http://www.cs.swarthmore.edu/~newhall/unixhelp/howto_C_libraries.html

#2


1  

Well, if you type man fmod into Google (man is the UNIX help system) and click the first link you get documentation about the function. This clearly states what headers you must include and what library you must link with:

好吧,如果你输入man fmod到Google(man是UNIX帮助系统)并点击第一个链接就可以获得有关该功能的文档。这清楚地说明了您必须包含哪些标题以及您必须链接的库:

Synopsis

#include <math.h>

double fmod(double x, double y);
float fmodf(float x, float y);
long double fmodl(long double x, long double y);

Link with -lm.