7

How can I extract all .obj files from a .lib file? The only way I found is to extract one at once. Is there a way to automate it?

LIB.EXE /EXTRACT:member.obj Library.LIB 
John Lewis
  • 337
  • 3
  • 12

2 Answers2

7

This is the batch file that I ended up using with usage extract.bat mylib.lib or extract.bat *.lib even

@ECHO OFF
SETLOCAL EnableDelayedExpansion

:Loop
IF "%1"=="" GOTO Continue

FOR %%F in (%1) DO (

    SET LIBFILE=%%F
    SHIFT

    @ECHO !LIBFILE!

    FOR /F %%O IN ('lib.exe /LIST !LIBFILE! /NOLOGO') DO (
        @SET OBJFILE=%%O
        @ECHO !OBJFILE!
        
        SET OBJPATH=%%~dO%%~pO
        SET OBJNAME=%%~nO

        IF NOT EXIST "!OBJPATH!" md !OBJPATH!

        IF EXIST "!OBJFILE!" ECHO !OBJFILE! exists, skipping...
        IF NOT EXIST "!OBJFILE!" lib.exe /NOLOGO !LIBFILE! "/EXTRACT:!OBJFILE!" "/OUT:!OBJFILE!"
    )
)
GOTO Loop
:Continue
GaspardP
  • 4,217
  • 2
  • 20
  • 33
  • 3
    This should be the accepted answer. Worked like a charm! – Shrukul Habib Oct 01 '20 at 19:09
  • 1
    The original OBJPATH in my LIBFILE was mapped to a "G:" drive that did not exist on my system. So when it tried to output the obj file to the original path, it complained of invalid location. So I did the following: SET OBJNAME=%%~nxO and lib.exe /NOLOGO !LIBFILE! "/EXTRACT:!OBJFILE!" "/OUT:!OBJNAME!" This would put all objs in the same folder and loose directory structure, but that was fine with my usage. – balajimc55 Nov 02 '20 at 22:31
  • 1
    This might be the best solution. But besides this there is another way. The .lib file is an archive this can be extracted with 7z or other archive tool. – Alexander Samoylov Mar 16 '21 at 10:53
-1

You could write a short program to do the calls for you using system(...)-Function.

That still leaves you with the problem of generating a list of member object files, though.

sum1stolemyname
  • 4,506
  • 3
  • 26
  • 44