libversion icon indicating copy to clipboard operation
libversion copied to clipboard

Errors LNK2001 on linking with libversion on Windows [bug]

Open AndreyMZ opened this issue 4 years ago • 0 comments

Problem description

The C library libversion is installed as the following:

  1. C:\Program Files\libversion\lib\libversion.lib - the import library for load-time dynamic linking of the DLL. However, the DLL itself is not installed to the system.
  2. C:\Program Files\libversion\lib\version.lib - the static library.

The pkg-config file C:\Program Files\libversion\lib\pkgconfig\libversion.pc points to the second one (the static library version.lib). But the pkg-config does not contains the compiler option to define the macro LIBVERSION_STATIC_DEFINE which is required for linking with the static library.

This causes, in particular, the bug https://github.com/repology/py-libversion/issues/8 (Build errors LNK2001 on Windows).

Steps to reproduce

  1. Build and install the C library libversion on Windows:

    set LIBVERSION_REF=3.0.1
    choco install -y git cmake visualstudio2017buildtools pkgconfiglite
    git clone https://github.com/repology/libversion.git --branch="%LIBVERSION_REF%"
    cd libversion
    cmake . -G "Visual Studio 15 2017 Win64"
    cmake --build . --config Release
    cmake --install .
    setx PKG_CONFIG_PATH "C:/Program Files/libversion/lib/pkgconfig"
    
  2. Check that it is discoverable by pkg-config:

    pkg-config --libs --cflags libversion
    

Actual result

C:\libversion>pkg-config --libs --cflags libversion
-IC:/Program\ Files/libversion/include -LC:/Program\ Files/libversion/lib -lversion

Expected result

C:\libversion>pkg-config --libs --cflags libversion
-DLIBVERSION_STATIC_DEFINE -IC:/Program\ Files/libversion/include -LC:/Program\ Files/libversion/lib -lversion

or

C:\libversion>pkg-config --libs --cflags libversion
-IC:/Program\ Files/libversion/include -LC:/Program\ Files/libversion/lib -llibversion

Possible solution 1

Add the definition on the macro LIBVERSION_STATIC_DEFINE to the pkg-config file:

Libs: -L${libdir} -lversion
Cflags: -I${includedir} -DLIBVERSION_STATIC_DEFINE

Possible solution 2

Properly install the DLL and make the pkg-config file point to the import library (libversion.lib):

Libs: -L${libdir} -llibversion
Cflags: -I${includedir}

AndreyMZ avatar Mar 06 '21 09:03 AndreyMZ