How do I get CMake to dynamically link a merged static library with system libraries?

Viewed 417

I have to merge one of my app's libs with the NVIDIA CUDA static lib using this horrific awful CMake code:

GET_TARGET_PROPERTY(OUTPUT_LIB ${LIBNAME} LOCATION)
add_custom_command (TARGET ${LIBNAME}
                    POST_BUILD
                    COMMAND mv ${OUTPUT_LIB} ${OUTPUT_LIB}.old
                    COMMAND echo "create ${OUTPUT_LIB}" > combineLibs.mri
                    COMMAND echo "addlib ${OUTPUT_LIB}.old" >> combineLibs.mri
                    COMMAND echo "addlib ${CUDA_LOCATION}"  >> combineLibs.mri
                    COMMAND echo "save"  >> combineLibs.mri
                    COMMAND echo "end" >> combineLibs.mri
                    COMMAND ar -M <combineLibs.mri
                    COMMAND rm ${OUTPUT_LIB}.old
                    COMMENT "Building merged library for ${LIBNAME} at ${OUTPUT_LIB}, including ${CUDA_LOCATION}"
)
target_link_libraries(${LIBNAME} -pthread -c)

This successfully produces a merged static library that has all the symbols in it. However, the NVIDIA CUDA static lib brought with it dependencies on libpthread and libc in the form of unresolved symbols. Now the merged library also has those unresolved symbols, and the target_link_libraries line doesn't seem to do what I seem to think it does, because the symbols don't get resolved at link-time. How do I get the merged static library to dynamically link against libpthread and libc?

1 Answers
Related