Beyond PREFIX: Alternative Approaches for CMake Target Naming


  • Example
    Imagine you have a library named my_library. By default, CMake might name the library file libmy_library.so (depending on your platform). By setting the PREFIX property to custom_, you can change the filename to custom_my_library.so.
  • Overriding Default Prefix
    The PREFIX property lets you override this default prefix. You can set it to any string you prefer.
  • Target Files
    When you create targets in CMake, like libraries or executables, CMake assigns them names based on their type and a platform-specific prefix (e.g., lib for libraries on Linux).

Here are some additional points to consider:

set_target_properties(my_library PROPERTIES PREFIX custom_)
  • Default PREFIX Value
    The initial value of PREFIX is often influenced by the variable CMAKE_<CONFIG>_POSTFIX (except for executables in older CMake versions).


Changing Library Filename Prefix

This example changes the library filename prefix for a library named my_library to custom_lib_:

set(CMAKE_TARGET_LIBRARY_FILE_DIR libraries)  # Optional: Define library output directory

add_library(my_library my_library.cpp)
set_target_properties(my_library PROPERTIES PREFIX custom_lib_)

# Now, the library will be named something like "custom_lib_my_library.so" depending on your platform.

Setting Different Prefixes for Debug and Release Builds

This example sets different prefixes for debug and release builds of an executable named my_program:

add_executable(my_program main.cpp)

set_target_properties(my_program PROPERTIES PREFIX debug_ FOR CONFIGURATION Debug)
set_target_properties(my_program PROPERTIES PREFIX release_ FOR CONFIGURATION Release)

# Now, the executable will be named "debug_my_program" for Debug configuration and "release_my_program" for Release configuration.

Using a Variable for Dynamic Prefix

This example defines a variable to hold the prefix and uses it for a library:

set(my_lib_prefix custom_)

add_library(my_library my_library.cpp)
set_target_properties(my_library PROPERTIES PREFIX ${my_lib_prefix})

# You can easily change the prefix by modifying the "my_lib_prefix" variable.


  1. OUTPUT_NAME Property

The OUTPUT_NAME property allows you to directly set the entire filename (including prefix and suffix) for your target. This approach offers more control but can be less flexible if you want to maintain a consistent naming scheme across multiple targets.

add_executable(my_program main.cpp)
set_target_properties(my_program PROPERTIES OUTPUT_NAME custom_my_program)

# Now, the executable will be named "custom_my_program".
  1. Environment Variables

For platform-specific adjustments or user-defined prefixes, you can leverage environment variables within your CMakeLists.txt.

set(CUSTOM_LIB_PREFIX "custom_lib_")

add_library(my_library my_library.cpp)
set_target_properties(my_library PROPERTIES PREFIX "${CUSTOM_LIB_PREFIX}")

# Set the environment variable before running CMake (e.g., export CUSTOM_LIB_PREFIX=my_prefix).
  1. Custom Commands

If you need highly customized filenames based on complex logic or external factors, you can utilize custom commands within CMake. These commands can manipulate the generated filenames after the build process. However, this approach can be more complex to manage and maintain.

  • Use custom commands for highly specific or complex filename manipulation (use with caution due to increased complexity).
  • Use environment variables for user-defined or platform-specific prefixes.
  • Use OUTPUT_NAME if you need complete control over the entire filename.
  • Use PREFIX if you want to modify the standard prefix while maintaining a consistent naming scheme.