# Build external library in subdirectory

**URL:** https://discourse.cmake.org/t/build-external-library-in-subdirectory/6342
**Category:** Usage
**Tags:** os:linux, gen:makefiles, gen:ninja
**Created:** [August 25, 2022, 3:46pm UTC](https://discourse.cmake.org/t/build-external-library-in-subdirectory/6342 "2022-08-25T15:46:06Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![andarius](https://discourse.cmake.org/user_avatar/discourse.cmake.org/andarius/32/2710_2.png) [@andarius](https://discourse.cmake.org/u/andarius)
#### Post date: [August 25, 2022, 3:46pm UTC](https://discourse.cmake.org/t/build-external-library-in-subdirectory/6342/1 "2022-08-25T15:46:06Z")

</div>

Hi,

First, I would like to say I’m new to using CMake to there might be an obvious solution to this that I’m missing.

I’m trying to build [zstd](https://github.com/facebook/zstd) to be used in Android with jni with the following `CMakeLists.txt`. I cloned the repository as a submodule into the `zstd` folder.

```cmake
cmake_minimum_required(VERSION 3.9.0)

project(reactnativezstd)

set(BUILD_DIR ${CMAKE_SOURCE_DIR}/build)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_STANDARD 11)

set(ZSTD_BUILD_PROGRAMS OFF) # Avoid Target "zstd" of type EXECUTABLE may not be linked into another target error
set(ZSTD_LEGACY_SUPPORT OFF)

add_subdirectory(../zstd/build/cmake zstd)

add_library(reactnativezstd
        SHARED
            ../cpp/react-native-zstd.cpp
            cpp-adapter.cpp
        )

target_include_directories(
    reactnativezstd
    PUBLIC
        ../zstd/lib
        ../cpp
)

target_link_libraries(
    reactnativezstd
    PRIVATE
    zstd
)

```

However I keep getting the same error: `ld: error: cannot find -lzstd`

Any ideas of what I’m missing?

Here is the full error:

```auto
 Build command failed.
  Error while executing process /home/julien/Android/Sdk/cmake/3.10.2.4988404/bin/ninja with arguments {-C /home/julien/Projects/react-native-zstd/android/.cxx/Debug/681tx4bf/armeabi-v7a libzstd_shared reactnativezstd}
  ninja: Entering directory `/home/julien/Projects/react-native-zstd/android/.cxx/Debug/681tx4bf/armeabi-v7a'
  [1/2] Linking CXX shared library ../../../../build/intermediates/cxx/Debug/681tx4bf/obj/armeabi-v7a/libreactnativezstd.so
  FAILED: ../../../../build/intermediates/cxx/Debug/681tx4bf/obj/armeabi-v7a/libreactnativezstd.so 
  : && /home/julien/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=armv7-none-linux-androideabi21 --gcc-toolchain=/home/julien/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/julien/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security -O2 -frtti -fexceptions -Wall -fstack-protector-all -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libreactnativezstd.so -o ../../../../build/intermediates/cxx/Debug/681tx4bf/obj/armeabi-v7a/libreactnativezstd.so CMakeFiles/reactnativezstd.dir/home/julien/Projects/react-native-zstd/cpp/react-native-zstd.cpp.o CMakeFiles/reactnativezstd.dir/cpp-adapter.cpp.o -lzstd -latomic -lm && :
  /home/julien/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lzstd
  /home/julien/Projects/react-native-zstd/cpp/react-native-zstd.cpp:15: error: undefined reference to 'ZSTD_compressBound'
  /home/julien/Projects/react-native-zstd/cpp/react-native-zstd.cpp:18: error: undefined reference to 'ZSTD_compress'
  clang++: error: linker command failed with exit code 1 (use -v to see invocation)
  [2/2] Linking C shared library ../../../../build/intermediates/cxx/Debug/681tx4bf/obj/armeabi-v7a/libzstd.so
  ninja: build stopped: subcommand failed.

```

Thank you!

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [August 25, 2022, 9:30pm UTC](https://discourse.cmake.org/t/build-external-library-in-subdirectory/6342/2 "2022-08-25T21:30:00Z")

</div>

`zstd` is built in its own folder. Linking `reactnativezstd` is done in a separate folder. The linker doesn’t find `zstd`, since it’s not in the folder where `reactnativezstd` is linked.

---

<div class="post-metadata">

### Author: ![andarius](https://discourse.cmake.org/user_avatar/discourse.cmake.org/andarius/32/2710_2.png) [@andarius](https://discourse.cmake.org/u/andarius)
#### Post date: [August 26, 2022, 6:56am UTC](https://discourse.cmake.org/t/build-external-library-in-subdirectory/6342/3 "2022-08-26T06:56:59Z")

</div>

Hi Alain, thank you for your time.  
So what’s the right way to make find zstd? With `add_library`? Or is it something else?

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [August 26, 2022, 11:37am UTC](https://discourse.cmake.org/t/build-external-library-in-subdirectory/6342/4 "2022-08-26T11:37:32Z")

</div>

The line

```auto
add_subdirectory(../zstd/build/cmake zstd)

```

makes all targets of the `zstd` project build as part of your project. You can make `reactnativezstd` depend on the right target and CMake will compute what to pass to the linker to make it find the library.

---

<div class="post-metadata">

### Author: ![andarius](https://discourse.cmake.org/user_avatar/discourse.cmake.org/andarius/32/2710_2.png) [@andarius](https://discourse.cmake.org/u/andarius)
#### Post date: [August 28, 2022, 8:54am UTC](https://discourse.cmake.org/t/build-external-library-in-subdirectory/6342/5 "2022-08-28T08:54:01Z")

</div>

Thanks Alain, I got it working with the following.

```auto
cmake_minimum_required(VERSION 3.10.2)

project(reactnativezstd)

set (CMAKE_VERBOSE_MAKEFILE ON)
set (CMAKE_CXX_STANDARD 11)
#
set(ZSTD_BUILD_PROGRAMS OFF) # Avoid Target "zstd" of type EXECUTABLE may not be linked into another target error
set(ZSTD_LEGACY_SUPPORT OFF)
set(ZSTD_BUILD_TESTS OFF)

add_subdirectory(../zstd/build/cmake _zstd)
#

add_library(reactnativezstd
            SHARED
            ../cpp/react-native-testlib.cpp
            cpp-adapter.cpp
)

target_include_directories(
    reactnativezstd
    PUBLIC
       ../zstd/lib
        ../cpp
)
#
add_library(libzstd STATIC IMPORTED)
set_property(TARGET libzstd PROPERTY IMPORTED_LOCATION _zstd/lib/libzstd.a)

target_link_libraries(
        reactnativezstd
        PRIVATE
        libzstd
)

```

Are there some improvements to this file or is it fine as it is?

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [August 30, 2022, 6:29pm UTC](https://discourse.cmake.org/t/build-external-library-in-subdirectory/6342/6 "2022-08-30T18:29:41Z")

</div>

Why do you not simply use the `libzstd_static` target?

```cmake
target_link_libraries(
        reactnativezstd
        PRIVATE
        libzstd_static
)

```

---

<div class="post-metadata">

### Author: ![andarius](https://discourse.cmake.org/user_avatar/discourse.cmake.org/andarius/32/2710_2.png) [@andarius](https://discourse.cmake.org/u/andarius)
#### Post date: [August 30, 2022, 7:00pm UTC](https://discourse.cmake.org/t/build-external-library-in-subdirectory/6342/7 "2022-08-30T19:00:42Z")

</div>

Hi Claus,  
Indeed that works too. I was not aware that I could do that. Does the `_static` notation work for any library?

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [August 30, 2022, 8:38pm UTC](https://discourse.cmake.org/t/build-external-library-in-subdirectory/6342/8 "2022-08-30T20:38:23Z")

</div>

Quoting my own comment:

> [@McMartin](#):
>
> The line
> 
> ```auto
> add_subdirectory(../zstd/build/cmake zstd)
> 
> ```
> 
> makes all targets of the `zstd` project build as part of your project.

`libzstd_static` is one of these targets. It is provided by the CMake code of the `zstd` project. More precisely, it is defined here:

> <https://github.com/facebook/zstd/blob/155d6a58a2a6290710194fd6367d720fca94a923/build/cmake/lib/CMakeLists.txt#L92-L94>

---

<div class="post-metadata">

### Author: ![andarius](https://discourse.cmake.org/user_avatar/discourse.cmake.org/andarius/32/2710_2.png) [@andarius](https://discourse.cmake.org/u/andarius)
#### Post date: [August 30, 2022, 8:54pm UTC](https://discourse.cmake.org/t/build-external-library-in-subdirectory/6342/9 "2022-08-30T20:54:46Z")

</div>

I see, thank you for your time!
