Change Target Output Name in CMake

Change Target Output Name in CMake

By default, when you define a target in CMake using commands like add_executable or add_library, the name of the generated output file matches the target name. However, there are situations where you might want the final binary or library to have a different name - for example, to follow a specific naming convention or to differentiate between variants of a build. This tutorial explains how to change target output name in CMake.

CMake provides the OUTPUT_NAME property, which allows you to specify a custom name for the generated output file without changing the target name itself.

Here's a minimal CMakeLists.txt example how to use this property:

cmake_minimum_required(VERSION 3.27)
project(myapp)

set(CMAKE_CXX_STANDARD 17)

add_executable(${PROJECT_NAME} main.cpp)

set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME app)

Run the command to generate the build files:

cmake -S . -B build

Build the project:

cmake --build build

After building, instead of generating myapp, CMake will output an executable named app. You can run it as follows:

./build/app

Leave a Comment

Cancel reply

Your email address will not be published.