# Linking to a prebuilt library

**URL:** https://discourse.cmake.org/t/linking-to-a-prebuilt-library/1733
**Category:** Usage
**Tags:** os:qnx
**Created:** [August 21, 2020, 3:21pm UTC](https://discourse.cmake.org/t/linking-to-a-prebuilt-library/1733 "2020-08-21T15:21:49Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![developer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.cmake.org/u/developer)
#### Post date: [August 21, 2020, 3:21pm UTC](https://discourse.cmake.org/t/linking-to-a-prebuilt-library/1733/1 "2020-08-21T15:21:49Z")

</div>

I am trying to link to a specific QNX library for the architecture I’m cross compiling for. Let’s call that library foo. I have a QNX toolchain file very similar to the example in the toolchains.cmake documentation.

I want to link an existing target to this library here are the options I see. I would like to link to this library statically. I’ve tried a couple options that seem to at the very least build.

```auto
target_link_libraries(MyTarget PRIVATE foo.a)
target_link_libraries(MyTarget PRIVATE /full/path/to/lib/foo.a)
target_link_libraries(MyTarget PRIVATE "-lfoo")
target_link_libraries(MyTarget PRIVATE foo) # simplest, but does this guarantee static?

find_library(FooLib NAMES foo.a) 
# FooLib actually appears to be the full path to foo.so, which isn't desired
target_link_libraries(MyTarget PRIVATE ${FooLib})

```

Are any of these options the _recommended_ approach?  
I do want to guarantee that I’m linking to the static library. Does just specifying `foo` accomplish this? Or will this default to the shared (.so) library.

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [August 21, 2020, 3:26pm UTC](https://discourse.cmake.org/t/linking-to-a-prebuilt-library/1733/2 "2020-08-21T15:26:40Z")

</div>

The simple answer to your question is that specifying the full path is the recommended approach, as that will guarantee use of the static library. However, you can use `find_library()` to find the full location of the library without having to hard-code its full path.

Ultimately the “best” way is to create a `FindFoo.cmake` or `FooConfig.cmake` (for use by `find_package()`) which creates an `IMPORTED` library target. This target has the full path as its imported location.

---

<div class="post-metadata">

### Author: ![developer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.cmake.org/u/developer)
#### Post date: [August 21, 2020, 5:36pm UTC](https://discourse.cmake.org/t/linking-to-a-prebuilt-library/1733/3 "2020-08-21T17:36:32Z")

</div>

Thanks for the quick response!

So that said, the full path to that library contains the architecture which is specific in the QNX toolchain file. I would prefer to not hardcode this path in case the architecture were to change.

Is it an acceptable/common practice to use `target_link_libraries(MyTarget PRIVATE foo.a)`?

The issue with find\_library is that even if I give it the `NAMES foo.a`, the found target is still the `.so` library. Do you know why this would be?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [January 4, 2021, 3:20pm UTC](https://discourse.cmake.org/t/linking-to-a-prebuilt-library/1733/4 "2021-01-04T15:20:18Z")

</div>

The docs state that to find static libraries, you need the prefix too. Does searching for `libfoo.a` work instead?

> [@](#):
>
> Each library name given to the `NAMES` option is first considered  
> as a library file name and then considered with platform-specific  
> prefixes (e.g. `lib`) and suffixes (e.g. `.so`). Therefore one  
> may specify library file names such as `libfoo.a` directly.  
> This can be used to locate static libraries on UNIX-like systems.
