为什么将参数传递给g ++的顺序很重要 [英] Why does the order of passing parameters to g++ matter

查看:59
本文介绍了为什么将参数传递给g ++的顺序很重要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我试图构建一个使用一些库的应用程序,这些库以共享对象文件的形式提供.我在编译CPP代码上浪费了很多时间,但没有用.

Recently, I was trying to build an application, which uses some libraries, available in form of shared object files. I wasted lot of time in compiling the CPP code and it didn't work.

下面是命令,以前我试图编译代码-

Below is the command, previously I was trying to compile the code-

g++ -I/opt/ros/indigo/include/ -I/usr/include/eigen3/ -L/opt/ros/indigo/lib/ -lorocos-kdl -lkdl_parser test.cpp -o test

上面的命令总是显示许多 undefined reference 错误.出于好奇,我更改了参数的顺序.下面是正在运行的命令-

The above command always shows many undefined references errors. Just for the curiosity, I changed the order of parameters. Below is the command, which is working-

g++ -L/opt/ros/indigo/lib -I/opt/ros/indigo/include -I/usr/include/eigen3 test.cpp -lorocos-kdl -lkdl_parser -o test

我在此处发布了完整的代码和解决方案.

I posted the complete code and solution here.

我的问题是,为什么将参数传递给g ++的顺序很重要?有什么其他办法可以避免将来发生此类问题?

My question is why does the order of passing parameters to g++ matter? Is there any alternative to avoid such problems in future?

推荐答案

通常,参数的顺序无关紧要,但是当然也有例外.例如,如果您提供多个 -O 标志,它将是最后一个被使用的标志,与其他标志相同.

Generally the order of arguments doesn't matter, but there are of course exceptions. For example if you provide multiple -O flags it will be the last one that is used, the same for other flags.

但是,图书馆有所不同,因为对于它们而言,顺序是很重要的.如果目标文件或库 A 依赖于库 B ,则 A 必须在命令行中的 B 之前.这是因为链接器如何扫描符号:当您使用库时,链接器将检查是否存在任何可以解析的符号.扫描结束后,该库将被丢弃,不会再次搜索.

Libraries are a little different though, because for them the order is significant. If object file or library A depends on library B, then A must come before B on the command line. This is because of how the linker scans for symbols: When you use a library the linker will check if there are any symbols that could be resolved. Once this scan is over the library is discarded and will not be searched again.

这意味着当您具有 -lorocos-kdl -lkdl_parser test.cpp 时,链接程序将首先扫描库 orocos-kdl kdl_parser ,请注意,这些库之间没有依赖关系,不需要库中的符号,而是继续由源文件生成的目标文件.

This means when you have -lorocos-kdl -lkdl_parser test.cpp the linker will scan the libraries orocos-kdl and kdl_parser first, notice that there aren't dependencies on these library, no symbols from the libraries are needed, and continue with the object file generated by the source file.

将顺序更改为 test.cpp -lorocos-kdl -lkdl_parser 时,链接程序将能够解析 test.cpp 引用的未定义符号到图书馆.

When you change the order to test.cpp -lorocos-kdl -lkdl_parser the linker will be able to resolve the undefined symbols referenced by test.cpp when it comes to the libraries.

这篇关于为什么将参数传递给g ++的顺序很重要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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