'On Windows MSVC, is it possible to merge some .obj into one .obj? If yes, how should I do that?
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:
a.obj b.obj c.obj
->
combined.obj
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
Solution 1:[1]
Seems not, but it is convenient to merge them into an .lib
:
lib *.obj /OUT:combined.lib
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user26742873 |
Solution 2 | 0xC0000022L |