# How to statically link external library by target\_link\_libraries()?

**URL:** https://discourse.cmake.org/t/how-to-statically-link-external-library-by-target-link-libraries/1718
**Category:** Code
**Tags:** os:linux
**Created:** [August 18, 2020, 2:49am UTC](https://discourse.cmake.org/t/how-to-statically-link-external-library-by-target-link-libraries/1718 "2020-08-18T02:49:08Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![yosukesan](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/y/d26b3c/32.png) [@yosukesan](https://discourse.cmake.org/u/yosukesan)
#### Post date: [August 18, 2020, 2:49am UTC](https://discourse.cmake.org/t/how-to-statically-link-external-library-by-target-link-libraries/1718/1 "2020-08-18T02:49:08Z")

</div>

I am trying to achieve the equivalent of following command by CMake.

`gcc -static -O0 -g main.c /usr/lib/x86_64-linux-gnu/libpthread.a`

My cmake script successfully built executable file, but gave segmentation fault at run.  
Can anybody point me what should I fix ? The command line above built and ran normally, and if I linked libpthtread.so by my cmake script it also terminated without issue.

- main.c

- CMakeLists.txt

- cmake command  
mkdir build  
cd build  
cmake -DCMAKE\_BUILD\_TYPE=“DEBUG” -DCMAKE\_C\_FLAGS\_DEBUG="-O0 -g -static" …/

- env  
$ dpkg -l | grep cmake  
ii cmake 3.13.4-1 amd64 cross-platform, open-source make system

---

<div class="post-metadata">

### Author: ![yosukesan](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/y/d26b3c/32.png) [@yosukesan](https://discourse.cmake.org/u/yosukesan)
#### Post date: [August 18, 2020, 5:35am UTC](https://discourse.cmake.org/t/how-to-statically-link-external-library-by-target-link-libraries/1718/2 "2020-08-18T05:35:13Z")

</div>

Self solved. If there is more efficient way please reply.

```
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)

project(pthread_task)
add_executable(pthread_task main.c)

add_library(libpthread STATIC IMPORTED)
set_target_properties(libpthread PROPERTIES IMPORTED_LOCATION /usr/lib/x86_64-linux-gnu/libpthread.a)
set_target_properties(libpthread PROPERTIES INTERFACE_INCLUDE_DIRECTORIES /usr/include)

target_link_libraries(pthread_task libpthread)
install(TARGETS pthread_task DESTINATION ${CMAKE_INSTALL_PREFIX})

```

- Just for check

---

<div class="post-metadata">

### Author: ![alex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alex/32/125_2.png) [@alex](https://discourse.cmake.org/u/alex)
#### Post date: [August 18, 2020, 9:21am UTC](https://discourse.cmake.org/t/how-to-statically-link-external-library-by-target-link-libraries/1718/3 "2020-08-18T09:21:29Z")

</div>

> If there is more efficient way please reply.

You should not be manually creating imported static libraries for system libraries! The correct commands are find\_library or (better) find\_package. In this case, the [FindThreads](https://cmake.org/cmake/help/v3.13/module/FindThreads.html) module is what you need.

Also, for installing, prefer to use the [GNUInstallDirs](https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html) module.

```auto
cmake_minimum_required(VERSION 3.13)
project(pthread_task)

set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)

add_executable(pthread_task main.c)
target_link_libraries(pthread_task PRIVATE Threads::Threads)

include(GNUInstallDirs)
install(TARGETS pthread_task DESTINATION ${CMAKE_INSTALL_BINDIR})

```

---

<div class="post-metadata">

### Author: ![yosukesan](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/y/d26b3c/32.png) [@yosukesan](https://discourse.cmake.org/u/yosukesan)
#### Post date: [August 18, 2020, 11:40am UTC](https://discourse.cmake.org/t/how-to-statically-link-external-library-by-target-link-libraries/1718/4 "2020-08-18T11:40:25Z")

</div>

Thanks for the advice alex. I check your cmake script also built and ran successfully.

---

<div class="post-metadata">

### Author: ![yosukesan](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/y/d26b3c/32.png) [@yosukesan](https://discourse.cmake.org/u/yosukesan)
#### Post date: [August 19, 2020, 5:37pm UTC](https://discourse.cmake.org/t/how-to-statically-link-external-library-by-target-link-libraries/1718/5 "2020-08-19T17:37:32Z")

</div>

I noticed the suggested method, which uses include(GNUInstallDirs), by alex is the best way to make a packge by cpack. My script created .deb and .rpm package with absolute pass. This is going to be trouble when you install to your system. However, the proposed method generate packages with relocatable path.
