How does cmake set the file name of the dynamic library to be built?

I want to add a LuaJIT wrapper to libgit2 so that it can be used in neovim.

The cmake configuration is as follows:

cmake_minimum_required(VERSION 3.22.2)
project("git2-neovim")

message(STATUS "cmake binary directory: ${CMAKE_BINARY_DIR}")

# Compile commands are output to "compile_commands.json", so that tools such as "ccls" can provide assistance.
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Set gcc compile options.
set(CMAKE_C_FLAGS_DEBUG "$ENV{CFLAGS} -Wall -g3 -ggdb")
set(CMAKE_C_FLAGS_RELEASE "$ENV{CFLAGS} -O3 -Wall")

set(
    SRC_FILES
    src/libgit2.c
)

add_library(${PROJECT_NAME} SHARED ${SRC_FILES})

find_package(PkgConfig REQUIRED)
if (PKG_CONFIG_FOUND)
    pkg_check_modules(LIBGIT2 REQUIRED libgit2)
    include_directories(${LIBGIT2_INCLUDE_DIRS})
    target_link_libraries(${PROJECT_NAME} ${LIBGIT2_LIBRARIES})
endif(PKG_CONFIG_FOUND)

This generates a "libgit2-neovim.so" file, but I want to generate a "libgit2.so" file.

Since I only use it in neovim, there is no conflict with the real libgit2.

environmental information:

operating system: Archlinux

cmake version: 3.25.1

============================== replenish ============================

When LuaJIT is looking for a shared library, it will only automatically add the file of the shared library according to the operating system, and will not add a prefix. The example is as follows:

require("demo")

The lua code above loads the "demo.so" file but not the "libdemo.so" file.

Therefore, I want to set in cmake, compile in any operating system, the output shared library must have "lib" prefix.

>Solution :

How does cmake set the file name of the dynamic library to be built?

The output filename is controlled by target properties https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#library-output-artifacts , and the default is composed of https://cmake.org/cmake/help/latest/variable/CMAKE_SHARED_LIBRARY_PREFIX.html followed by library name followed by https://cmake.org/cmake/help/latest/variable/CMAKE_SHARED_LIBRARY_SUFFIX.html .

I want to generate a "libgit2.so" file

So name your library git2 not git2-neovim.

add_library(git2

or set LIBRARY_OUTPUT_NAME target property of the target.

Leave a Reply