5

For example, there is three object files a.obj b.obj c.obj just compiled out with cl, and it is desired to combine them into one combined.obj.

A comment of an SO question points out that on *nix it's possible to do this kind of thing with ld. However, cl and link all seems only support .exe, .dll and .lib as output.


The whole procedure of what I want to do with the combined object file as follows:

  1. a.obj b.obj c.obj -> combined.obj
  2. combined.obj d.obj e.obj -> executable.exe

My problem is solved. a.obj b.obj c.obj use some variables and functions yet to be linked, and I thought that .lib can't tolerant missing functions since it is a library, but in fact it is OK. I can just merge them into an .lib file:

lib *.obj /OUT:combined.lib
user26742873
  • 919
  • 6
  • 21
  • 2
    Something wrong with putting them in a .lib file ? That is, after all, what they're for: collecting object code into a single library. – WhozCraig Aug 18 '20 at 14:04
  • 1
    There may be a tool available for merging object files. But, generally, this is what an object *library* is for. – Adrian Mole Aug 18 '20 at 14:04
  • @WhozCraig Yes, they lacks some unlinked functions, so it's not OK to put them in a library. – user26742873 Aug 18 '20 at 14:05
  • 1
    @Aoyama You can stuff *any* object code into a .lib file. external dependencies make no difference. Ultimately the consumer of the lib will have to finish that off by providing anything still missing. A DLL, different story, but a regular archive lib is literally just a package of object code. – WhozCraig Aug 18 '20 at 14:07
  • 2
    @Aoyama If your last comment is about the ability to do link-time code generation and the like - MSVC *can* do that with object libraries, if you tell it to build those libraries with that option in mind. – Adrian Mole Aug 18 '20 at 14:07
  • @WhozCraig Thank you, I didn't know that `.lib` can tolerant missing stuff! Now my problem is solved, I can compiled them into one `.lib`. – user26742873 Aug 18 '20 at 14:11
  • 1
    Take note to what Adrian said. If you're looking to build with LTCG for release builds, you need to make sure the objs you're stuffing into your .lib were adequately configured to do so. – WhozCraig Aug 18 '20 at 14:12
  • Please write an answer and mark it. This is not a forum, and others seeking for help with similar issues will not bother to come here if there is no accepted answer. – the busybee Aug 19 '20 at 07:04
  • @thebusybee The answer hasn't been found yet, however there is a workaround and I've edited it into the question description. I have benefited from many "unanswered" questions like this, where there are great stuff in the comments and the question description, and I think if anyone with similar issues came up with this in Google they would came in to see what happens. – user26742873 Aug 19 '20 at 09:51
  • Another tool to be aware of would be `objconv` by Agner Fog of general performance optimization fame ;) (it currently doesn't seem to implement this exact feature, though, it appears) – 0xC0000022L Jan 20 '23 at 12:04

2 Answers2

0

You can apply the method employed here also to the COFF files created by cl.exe, provided that your build of ld supports the respective input and output formats and those formats lend themselves to the process.

What you can do in such a case is this (and yes $INPUTS means you can give multiple object files as you wanted):

ld --oformat pe-x86-64 -r $INPUTS -o $OUTPUT

The --oformat pe-x86-64 (aka AMD64, x64 on Windows) is necessary whenever the ld has been built with a different default output format.

If that's the case and you didn't give --oformat you will get something like:

ld: relocatable linking with relocations from format pe-x86-64 (input.obj) to format elf64-x86-64 (output.obj) is not supported

However the process doesn't work for all input/output format combinations, as I learned with ld 2.34 on Ubuntu 20.04:

ld: relocatable linking with relocations from format pe-i386 (input.obj) to format pe-i386 (output.obj) is not supported

NB: At this point I had no luck to get this to work with lld-link or ld.lld (both available through modern VS versions), though.

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
-1

Seems not, but it is convenient to merge them into an .lib:

lib *.obj /OUT:combined.lib
user26742873
  • 919
  • 6
  • 21
  • This is not a merge, this is bundling. The object files are simply placed into an archive, much as with `ar` in GCC-based toolchains. You can even unpack the `.lib` files MSVC generates using tools like 7-Zip and `ar`. – 0xC0000022L May 18 '22 at 07:55