# \[43998\] How to implement different CMake functions with the same name?

**URL:** <https://discourse.cmake.org/t/43998-how-to-implement-different-cmake-functions-with-the-same-name/13349>\
**Category:** Code\
**Created:** [January 8, 2025, 10:03am UTC](https://discourse.cmake.org/t/43998-how-to-implement-different-cmake-functions-with-the-same-name/13349 "2025-01-08T10:03:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Salomon](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/91b2a8/32.png) [@Salomon](https://discourse.cmake.org/u/Salomon)\
**Post date:** [January 8, 2025, 10:03am UTC](https://discourse.cmake.org/t/43998-how-to-implement-different-cmake-functions-with-the-same-name/13349/1 "2025-01-08T10:03:50Z")

</div>

Hello,

How can I simply & cleanly implement different functions which share the same name?

Here is a first attempt:

- Function Calling: [common.cmake](https://gitlab.com/uranie-cea/root-project/roottest/-/blob/269b2ffe0a4910f9f234cc2da818ab2c934a6687/uranie/tma/cmake/common.cmake):

```auto
get_extension(MACRO_EXTENSION)

```

- Implementation 1: [cpp/CMakeLists.txt](https://gitlab.com/uranie-cea/root-project/roottest/-/blob/269b2ffe0a4910f9f234cc2da818ab2c934a6687/uranie/tma/cmake/cpp/CMakeLists.txt):

```auto
function(get_extension MACRO_EXTENSION)
    set(MACRO_EXTENSION "C" PARENT_SCOPE)
endfunction()

include(${CMAKE_CURRENT_SOURCE_DIR}/../common.cmake)

```

- Implementation 2: python/CMakeLists.txt:

```auto
function(get_extension MACRO_EXTENSION)
    set(MACRO_EXTENSION "py" PARENT_SCOPE)
endfunction()

include(${CMAKE_CURRENT_SOURCE_DIR}/../common.cmake)

```

Best Regards,

Salomon

---

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [January 8, 2025, 10:13am UTC](https://discourse.cmake.org/t/43998-how-to-implement-different-cmake-functions-with-the-same-name/13349/2 "2025-01-08T10:13:42Z")

</div>

You can’t, CMake only has one scope for functions, and no overloading.

If you need a customisation point for a shared piece of CMake code (as in your case), you must make it a variable, not a function. For a simple case like your example, this could work:

- common.cmake:

```auto
set(MACRO_EXTENSION ${INJECTED_MACRO_EXTENSION}) # or just use INJECTED_MACRO_EXTENSION directly

```

- cpp/CMakeLists.txt:

```auto
set(INJECTED_MACRO_EXTENSION C)
include(common.cmake)

```

For something more complicated, you can inject the name of a command to call:

- common.cmake:

```auto
cmake_language(CALL ${INJECTED_COMMAND_NAME})

```

- cpp/CMakeListstxt

```auto
function(cpp_func)
  # whatever
endfunction()
set(INJECTED_COMMAND_NAME cpp_func)
include(common.cmake)

```

- python/CMakeListstxt

```auto
function(py_func)
  # whatever
endfunction()
set(INJECTED_COMMAND_NAME py_func)
include(common.cmake)

```
