Problem with duplicate library names under MS-Windows

Under MS-Windows, if two different shared libraries have the same name, their produced binaries will be the same, even if their output directories differ.

A minimal example:

cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(TestDuplicateNamesMSVC LANGUAGES C)

set(libname prime)
set(libdir1 ${CMAKE_BINARY_DIR}/lib1)
set(libdir2 ${CMAKE_BINARY_DIR}/lib2)

# NOTE: The output names of both libraries are the _same_,
# yet they are built in _different_ directories

# shared library 1
add_library(prime1 SHARED prime1.c)
set_target_properties(prime1 PROPERTIES
    POSITION_INDEPENDENT_CODE ON
    RUNTIME_OUTPUT_NAME ${libname}
    RUNTIME_OUTPUT_DIRECTORY_RELEASE ${libdir1}
    OUTPUT_NAME ${libname}
    LIBRARY_OUTPUT_DIRECTORY ${libdir1}
    )

# shared library 2
add_library(prime2 SHARED prime2.c)
set_target_properties(prime2 PROPERTIES
    POSITION_INDEPENDENT_CODE ON
    RUNTIME_OUTPUT_NAME ${libname}
    RUNTIME_OUTPUT_DIRECTORY_RELEASE ${libdir2}
    OUTPUT_NAME ${libname}
    LIBRARY_OUTPUT_DIRECTORY ${libdir2}
    )
  • Build
cmake -B build
cmake --build build --config Release
  • Compare binaries under MS-Windows
fc /b .\build\lib1\prime.dll .\build\lib2\prime.dll

yields FC: no differences encountered.

  • Compare binaries under Linux
diff ./build/lib1/libprime.so ./build/lib2/libprime.so

yields Binary files build/lib1/libprime.so and build/lib2/libprime.so differ.

Tested with MSVC 19.29 under Windows 10 and GCC 10.2 under Linux, both with x64 architecture.

What about the .lib files? Are they identical?

Do these libraries actually export symbols? If they don’t, then the .dll files are basically empty shells, so it would make sense that they are identical.

Thank you for the reply. The problem was that I had forgotten to export the symbols in the source files; that is, __declspec(dllexport) was not added.