Linking to a prebuilt library

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.

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, 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.

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.

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?

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