# Linking Ada objects into a C++ shared library

**URL:** https://discourse.cmake.org/t/linking-ada-objects-into-a-c-shared-library/2011
**Category:** Code
**Created:** [October 16, 2020, 2:56pm UTC](https://discourse.cmake.org/t/linking-ada-objects-into-a-c-shared-library/2011 "2020-10-16T14:56:44Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [October 16, 2020, 3:42pm UTC](https://discourse.cmake.org/t/linking-ada-objects-into-a-c-shared-library/2011/2 "2020-10-16T15:42:33Z")

</div>

You cannot mix custom commands and libraries. `add_library()` is only able to handle sources for supported languages. So, `somefile.adb` is simply ignored.

To work-around this, manage all ada source files compilation by custom commands (what you already did) and a custom target (see [add\_custom\_target()](https://cmake.org/cmake/help/latest/command/add_custom_target.html)). And add explicitly the dependency between targets ([add\_dependencies()](https://cmake.org/cmake/help/latest/command/add_dependencies.html)) to ensure compilation.

And to finish, compute the list of ada objects and add them explicitly to the link step of you library.

Something like that:

```cmake
set (ADA_OUTPUTS)
foreach (...)
   ...
  add_custom_command(OUTPUT ${WORK_DIR}/${target}.o ...)
  list(APPEND ADA_OUTPUTS ${WORK_DIR}/${target}.o)
endforeach()
add_custom_target(ada_objects
   DEPENDS ${ADA_OUTPUTS}
   ...
)

add_library(main SHARED ...)
add_dependencies(main ada_objects)
target_link_libraries(main PRIVATE ${ADA_OUTPUTS})

```

And FYI, I am currently evaluating the integration of Ada language support in `CMake` (`gnat` specifically). I think it is feasible but many changes are required in the code of `CMake`.  
For now, I am just able to pass the initialization step and compile a simple ada executable.

For the fun:

```cmake
cmake_minimum_required(VERSION 3.19)

project(Tests LANGUAGES Ada)

set(CMAKE_Ada_STANDARD 2012)

add_executable(ada_test hello.adb)
set_property(TARGET ada_test PROPERTY MAIN_NAME hello)

```

And lauching `cmake` gives:

```auto
-- The Ada compiler identification is GNU 2020
-- Checking whether Ada compiler has -isysroot
-- Checking whether Ada compiler has -isysroot - yes
-- Checking whether Ada compiler supports OSX deployment target flag
-- Checking whether Ada compiler supports OSX deployment target flag - yes
-- Detecting Ada compiler ABI info
-- Detecting Ada compiler ABI info - done
-- Check for working Ada compiler: /usr/local/opt/GNAT/2020/bin/gnat - skipped
-- Detecting Ada compile features
-- Detecting Ada compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: .../build

```

and the compilation:

```auto
[50%] Building Ada object CMakeFiles/ada_test.dir/hello.o
/usr/local/opt/GNAT/2020/bin/gnat make -c -u -D CMakeFiles/ada_test.dir .../hello.adb -cargs -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -gnat2012
gcc -c -I/Users/marc/tests/cmake/ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -gnat2012 -I- -o .../build/CMakeFiles/ada_test.dir/hello.o .../hello.adb
[100%] Linking Ada executable ada_test
/usr/local/opt/GNAT/2020/bin/gnat make -b -l -o ada_test hello -aOCMakeFiles/ada_test.dir -cargs -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -largs
gnatbind -aOCMakeFiles/ada_test.dir -x .../build/CMakeFiles/ada_test.dir/hello.ali
gnatlink .../build/CMakeFiles/ada_test.dir/hello.ali -o ada_test
[100%] Built target ada_test

```

---

_[View the full topic](https://discourse.cmake.org/t/linking-ada-objects-into-a-c-shared-library/2011)._
