是否可以从 gcc 中的其他目标文件创建目标文件?

发布于 2024-07-24 05:49:41 字数 662 浏览 6 评论 0原文

我试图在 makefile 中做类似的事情:

program.exe: ui.o main.o
   gcc ......etc
ui.o: window1.o window2.o
   gcc -c window1.o window2.o -o ui.o #this doesn't want to work
window1.o: window1.c window1.h window1_events.c window1_controls.c ...
   gcc -c window1.c window1_events.c window1_controls.c... -o window1.o
window2.o: ...
   gcc ...
main.o: ...
   gcc ...

但是当我像这样编译时,它会给出错误“输入文件未使用,因为链接未完成”,然后我得到一堆未解决的外部文件等 - 已解决的问题通过更改

program.exe: ui.o main.o
   gcc ...

program.exe: window1.o window2.o main.o
   gcc ...

so 是否可以将目标文件链接在一起,以避免 makefile 中出现一英里长的行并进一步分解构建过程?

I was trying to do something like this in a makefile:

program.exe: ui.o main.o
   gcc ......etc
ui.o: window1.o window2.o
   gcc -c window1.o window2.o -o ui.o #this doesn't want to work
window1.o: window1.c window1.h window1_events.c window1_controls.c ...
   gcc -c window1.c window1_events.c window1_controls.c... -o window1.o
window2.o: ...
   gcc ...
main.o: ...
   gcc ...

but when I compile like this, it gives the error "input file unused because linking not done," and then I get a bunch of unresolved externs, etc--problems which are resolved by changing

program.exe: ui.o main.o
   gcc ...

to

program.exe: window1.o window2.o main.o
   gcc ...

so is it possible to just link object files together, to avoid having mile-long lines in a makefile and break down the build process a little more?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

几度春秋 2024-07-31 05:49:41

是:要将多个目标文件合并为一个,请使用 ld -rld -Ur

来自 Linux 上的“man ld”:

   -r
   --relocatable
      Generate  relocatable  output---i.e.,  generate  an output file that can
      in turn serve as input to ld.  This is often called partial linking.
      As a side effect, in environments that support standard Unix magic
      numbers, this option also sets the output file’s magic number to
      "OMAGIC".
      If this option is not specified, an absolute file is produced.
      When linking C++ programs, this option will not resolve references to
      constructors;  to do that, use -Ur.

您也可以使用 gcc 执行此

gcc -Wl,-r foo.o bar.o -o foobar.o -nostdlib

操作 :像这样的目标文件比使用存档库有一些优点:如果合并的文件很少更改(与 main.c 相比),则最终的可执行链接会更快。

OTOH,对于存档库,链接器将仅使用它需要的内容,因此如果最终不需要 window2.c,则您的可执行文件可能会更小。

Yes: to merge several object files into one, use ld -r or ld -Ur:

From "man ld" on Linux:

   -r
   --relocatable
      Generate  relocatable  output---i.e.,  generate  an output file that can
      in turn serve as input to ld.  This is often called partial linking.
      As a side effect, in environments that support standard Unix magic
      numbers, this option also sets the output file’s magic number to
      "OMAGIC".
      If this option is not specified, an absolute file is produced.
      When linking C++ programs, this option will not resolve references to
      constructors;  to do that, use -Ur.

You could also do this with gcc:

gcc -Wl,-r foo.o bar.o -o foobar.o -nostdlib

Merging object files like this has some advantages over using an archive library: if merged files change very infrequently (compared to say main.c), your final executable links will be faster.

OTOH, with archived library, the linker will only use what it needs, so your executable may end up being smaller if e.g. window2.c ends up not being necessary.

唯憾梦倾城 2024-07-31 05:49:41

我一堆目标文件是一个库。 您可以使用 ar 创建库
公用事业。 以下示例创建一个名为 mylib.a 的库,其中包含文件 foo.o 和 bar.o

ar rvs mylib.a foo.o bar.o

然后您可以通过在编译器命令行上使用它来链接它:

gcc -o myexe main.c mylib.a

I bunch of object files is a library. You can create a library with the ar
utility. The following example creates a library called mylib.a containing the files foo.o and bar.o

ar rvs mylib.a foo.o bar.o

You can then link with it by using it on the compiler command line:

gcc -o myexe main.c mylib.a
空城之時有危險 2024-07-31 05:49:41

创建库:

ar rvs somelib.a file1.o file2.o file3.o

链接它:

gcc -o program.exe file4.o somelib.a

To create a library:

ar rvs somelib.a file1.o file2.o file3.o

To link it:

gcc -o program.exe file4.o somelib.a
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文