本文介绍了自动生成C / C ++的可执行对象文件(连接器)的依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个灵活的C / C ++构建框架,我会(希望)开源很快就会。 (请参见)。

I am currently working on a flexible C/C++ build framework that I'll (hopefully) open source fairly soon. (see this question for some background).

我使用下面的命令生成源/头文件中的#include文件的依赖关系。

I am using the below command to generate the #include file dependencies for source/header files.

gcc -M -MM -MF

有巧妙地推断连接(.o文件将)的依赖关系的可执行使用gcc / GNU工具程序以类似的方式上面(单元测试+主要针对目标平台在我的案件的可执行文件)的方式?目前,该框架使得一大堆的假设是pretty在确定这些依赖哑巴。

Is there a way of cleverly inferring linker (.o file) dependencies for executables (unit tests + main executable for target platform in my case) using gcc/GNU utilties in a similar way to above? Currently the framework makes a whole lot of assumptions and is pretty dumb in determining these dependencies.

我听说有个办法,其中的纳米的命令可用于拿出未定义符号在目标文件列表。例如,一个目标文件运行纳米(使用gcc编译-c)想出了这样的事 -

I have heard of one approach where the nm command can be used to come up with a list of undefined symbols in an object file. For example, running nm on an object file (compiled using gcc -c) comes up with something like this -

nm -o module.o

module.o:         U _undefinedSymbol1
module.o:         U _undefinedSymbol2
module.o:0000386f T _definedSymbol

一会再寻找其他目标文件而这些不确定的符号定义要拿出所需的对象文件相关的列表,以成功链接文件。

One would then look for other object files where these undefined symbols are defined to come up with a list of object file dependencies required to successfully link the file.

这是考虑在确定可执行文件的链接器的依赖关系最佳做法?是否有这些推断依赖其他方法吗?假设所有目标文件已经存在,提出解决方案时(即已经使用gcc编译-c)。

Is this considered best practice in determining linker dependencies for executables? Are there any other ways of inferring these dependencies? Assume that all object files already exist (i.e. have already been compiled using gcc -c) when proposing your solution.

推荐答案

如果有需要套不同的依赖性,那么正常的,经典的方式来处理多个可执行文件(或者甚至是一​​个可执行文件)是使用库 - 静态 .A 或共享的.so (或同等学历) - 持有,可以被更多使用的目标文件多个程序,并程序与该库链接。链接器自动拉正确的对象文件从一个静态存档。共享库的过程是一个有点不同,但最终结果是一样的:可执行文件已经向它提供了正确的目标文件在运行时

If there are multiple executables (or even a single executable) that need different sets of dependencies, then the normal, classic way to handle that is to use a library — static .a or shared .so (or equivalent) — to hold the object files that can be used by more than one program, and to link the programs with that library. The linker automatically pulls the correct object files out of a static archive. The shared library process is a little different, but the net result is the same: the executable has the correct object files available to it at runtime.

有关的任​​何程序,存在的至少一个文件所特有的程序(通常,这是一个包含主()程序的文件)。有可能是该程序的几个文件。这些文件可能知道关于并且可以容易地列出。你可能需要根据配置和编译选项的是程序之间共享的可能,并通过库机制很容易​​处理。

For any program, there is at least one file unique to the program (normally, that's the file that contains the main() program). There may be a few files for that program. Those files are probably known about and can be listed easily. The ones that you might need depending on configuration and compilation options are probably shared between programs and are easily handled via the library mechanism.

您必须决定是否要使用静态或共享库。创建共享库以及比创建静态库更难。在另一方面,你可以更新共享库,并立即影响到所有使用它的程序,而静态库是可以改变的,但只有程序,与从变化的新库利益重新连接。

You have to decide whether you want to use static or shared libraries. Creating shared libraries well is harder than creating static libraries. On the other hand, you can update a shared library and immediately affect all the programs that use it, whereas a static library can be changed but only programs that are relinked with the new library benefit from the changes.

这篇关于自动生成C / C ++的可执行对象文件(连接器)的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 19:48
查看更多