Making Connections

Creating a .lib file for libclang on Windows

Posted by Terry Moore
Terry Moore
 
User is currently offline
on Thursday, 06 November 2014 in Useful Tips

I'm doing some code refactoring work. For a variety of reasons, I want to try libclang, the C interface for clang/llvm. I want to get to doing my tests immediately, and not waste time building it.

I downloaded the LLVM 3.5 distribution for Visual Studio.

Immediate roadblock: they ship "libclang.dll" (the code I want to use, in compiled form), but no "libclang.lib" (which the Visual Studio linker wants for linking my C apps to libclang).

Some searching found this very useful page: Create .lib file from .dll

However, it was manual; I'll never edit by hand if I can write a script. So here are my steps. Obviously, this can be used for any .dll, if it uses C APIs. (Equally obviously, you'll need a header file to compile your C, but ... I have that.)

  1. Start a command window from Visual C. I do this to get all the paths set up correctly for the VC tools. I don't like having my path set statically -- I use many different compilers, and several different versions of Visual C side by side.

  2. In the VC command window, use dumpbin.exe to generate a listing of the exports.
    dumpbin libclang.dll /exports >clangdefs.txt
  3. Then in a cygwin window:
    awk 'BEGIN { print "EXPORTS" }
        /ordinal hint/,/Summary/ {
            if ( $1 ~ /^[0-9]+$/ ) print $1,$4
        }' clangdefs.txt >libclang.def
  4. Finally, back in the VC window (from steps 1 & 2):
    lib /def:libclang.def /out:libclang.lib
    Microsoft (R) Library Manager Version 10.00.40219.01
    Copyright (C) Microsoft Corporation.  All rights reserved.

    LINK : warning LNK4068: /MACHINE not specified; defaulting to X86
       Creating library liblang.lib and object liblang.exp

 Of course, this can all be combined into a single script.

Thanks to Adrian for posting the instructions. You can find terse instructions in MSDN (http://support.microsoft.com/kb/131313), but I certainly agree with Adrian that his instructions are much more to the point.

 

Tags: Untagged

Comments

Please login first in order for you to submit comments
Legal and Copyright Information