# How to make cmake use -shared -dlink

**URL:** <https://discourse.cmake.org/t/how-to-make-cmake-use-shared-dlink/4669>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [December 15, 2021, 12:59pm UTC](https://discourse.cmake.org/t/how-to-make-cmake-use-shared-dlink/4669 "2021-12-15T12:59:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgphpc](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@jgphpc](https://discourse.cmake.org/u/jgphpc)\
**Post date:** [December 15, 2021, 12:59pm UTC](https://discourse.cmake.org/t/how-to-make-cmake-use-shared-dlink/4669/1 "2021-12-15T12:59:41Z")

</div>

Hi,

This is my first post here.  
I am trying to write a CMakeLists.txt that will build a HIP test code (hipify from cuda):

> hipcc --offload-arch=gfx908 -fPIC -fgpu-rdc -std=c++17 -w -c domain/util.cu  
> hipcc --offload-arch=gfx908 -fPIC -fgpu-rdc -std=c++17 -w -c domain/test.cu -Iinclude  
> hipcc -fPIC -shared -dlink util.o test.o -o device\_link.o -fgpu-rdc --hip-link  
> /opt/rocm/llvm/bin/clang++ src/main.cpp util.o test.o device\_link.o -o exe -L/opt/rocm/hip/lib -lamdhip64 -Iinclude

My cmake version is 3.22.1

I start with:

> cmake -S . -B build  
> – The CXX compiler identification is GNU 7.5.0  
> – The C compiler identification is GNU 7.5.0  
> – Detecting CXX compiler ABI info  
> – Detecting CXX compiler ABI info - done  
> – Check for working CXX compiler: /usr/bin/c++ - skipped  
> – Detecting CXX compile features  
> – Detecting CXX compile features - done  
> – Detecting C compiler ABI info  
> – Detecting C compiler ABI info - done  
> – Check for working C compiler: /usr/bin/cc - skipped  
> – Detecting C compile features  
> – Detecting C compile features - done  
> – Found HIP: /opt/rocm-4.3.1/hip (found version “4.3.21331-94fc2572”)  
> – Found HIP: 4.3.21331-94fc2572  
> – Looking for a HIP compiler  
> – Looking for a HIP compiler - /opt/rocm-4.3.1/llvm/bin/clang++  
> – The HIP compiler identification is Clang 13.0.0  
> – Detecting HIP compiler ABI info  
> – Detecting HIP compiler ABI info - done  
> – Check for working HIP compiler: /opt/rocm-4.3.1/llvm/bin/clang++ - skipped  
> – Detecting HIP compile features  
> – Detecting HIP compile features - done  
> – Configuring done  
> – Generating done  
> – Build files have been written to: build

My CMakeLists.txt have [add\_library](https://gitlab.com/jgphpc/my-gists/-/blob/main/202112/domain/CMakeLists.txt#L2) and [$\<TARGET\_OBJECTS:test\_obj\>](https://gitlab.com/jgphpc/my-gists/-/blob/main/202112/src/CMakeLists.txt#L7) but I am unable to make cmake build the object:

> cd build; make test\_obj VERBOSE=1  
> make[3]: Nothing to be done for ‘domain/CMakeFiles/test\_obj.dir/build’.

What did I miss ?

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![jgphpc](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@jgphpc](https://discourse.cmake.org/u/jgphpc)\
**Post date:** [December 16, 2021, 2:59pm UTC](https://discourse.cmake.org/t/how-to-make-cmake-use-shared-dlink/4669/2 "2021-12-16T14:59:22Z")

</div>

The solution is to add:

in CMakeLists.txt

> ```
> set(CMAKE_HIP_FLAGS "-fPIC -fgpu-rdc -w")
> 
> ```

and in domain/CMakeLists.txt

> set\_source\_files\_properties(test.cu util.cu PROPERTIES LANGUAGE HIP)  
> add\_library(test\_obj OBJECT test.cu)  
> target\_include\_directories(test\_obj PRIVATE ${PROJECT\_SOURCE\_DIR}/include)

and in src/CMakeLists.txt

> set\_source\_files\_properties(main.cpp PROPERTIES LANGUAGE HIP)

see this [commit](https://gitlab.com/jgphpc/my-gists/-/commit/1677e0fb0c07e7d1b6983249aa24deec67ebd08e)

---

<div class="post-metadata">

**Author:** ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)\
**Post date:** [December 17, 2021, 12:55pm UTC](https://discourse.cmake.org/t/how-to-make-cmake-use-shared-dlink/4669/3 "2021-12-17T12:55:38Z")

</div>

If you use the file extension `.hip` instead of `.cu` CMake should automatically detect the right language.
