是否可以从 gcc 中的其他目标文件创建目标文件?
我试图在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是:要将多个目标文件合并为一个,请使用
ld -r
或ld -Ur
:来自 Linux 上的“man ld”:
您也可以使用 gcc 执行此
操作 :像这样的目标文件比使用存档库有一些优点:如果合并的文件很少更改(与 main.c 相比),则最终的可执行链接会更快。
OTOH,对于存档库,链接器将仅使用它需要的内容,因此如果最终不需要 window2.c,则您的可执行文件可能会更小。
Yes: to merge several object files into one, use
ld -r
orld -Ur
:From "man ld" on Linux:
You could also do this with gcc:
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.
我一堆目标文件是一个库。 您可以使用 ar 创建库
公用事业。 以下示例创建一个名为 mylib.a 的库,其中包含文件 foo.o 和 bar.o
然后您可以通过在编译器命令行上使用它来链接它:
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
You can then link with it by using it on the compiler command line:
创建库:
链接它:
To create a library:
To link it: