-lm在命令末尾不起作用 [英] -lm doesnt work unless it's at the end of the command

查看:89
本文介绍了-lm在命令末尾不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个用于单评估的程序,并且他们有一条设定好的行来编译它,因此,如果该程序不能正常工作,则将不被接受.他们命令使用的是

Im currently writing a program for a uni asssessment and they have a set line to compile it, so if it doesn't work with that it won't be accepted. They command they use is

gcc -Wall -ansi -lm program.c -o program.out

我的程序不会那样编译,并且会给我一个未定义的引用错误(使用math.h库引用我的log10)如果我使用:

My program will not compile that way, and it'll give me a undefined referance error (Referring to my log10 using math.h library) if i use:

gcc -Wall -ansi program.c -o program.out -lm

有效

可能是我的问题吗?

我正在使用Windows 10 64bit,并已安装Windows bash和gcc.

Im using windows 10 64bit and have windows bash installed and gcc.

推荐答案

如果您的讲师使用的是金牌,而您使用的是GNU ld,则可以对此进行解释.这是两个链接器,它们都是GNU项目的一部分,并且都与GCC共同使用.

This would be explained if your instructors are using gold and you are using GNU ld. These are two linkers, both are part of the GNU project, and both are commonly used with GCC.

如果使用的是GNU ld,则会出现传统"行为:

If you are using GNU ld, you get the "traditional" behavior:

相对于路径名操作数,指定-L和-l选项的顺序以及指定-l选项的顺序都是重要的.

The order of specifying the -L and -l options, and the order of specifying -l options with respect to pathname operands is significant.

这意味着必须将 -lm 放在之后的所有依赖于它的目标文件和库中.

This means that you have to put -lm after any object files and libraries that depend on it.

但是,如果您使用的是黄金,则 -l <​​/code>选项可能会首先出现.

However, if you are using gold, the -l options may appear first.

如果您的系统上安装了黄金,则可以自己进行测试.

If you have gold installed on your system, you can test it yourself.

这就是我得到的:

$ gcc -lm program.c 
/tmp/ccJmBjmd.o: In function `main':
program.c:(.text+0x15): undefined reference to `sin'
collect2: error: ld returned 1 exit status

但是,如果我使用黄金,它就可以正常工作:

But if I use gold, it works fine:

$ gcc -lm program.c -fuse-ld=gold

这篇关于-lm在命令末尾不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆