[CMake] Moving static library to separate project

Christopher E Robison cerobison at utexas.edu
Tue Sep 25 17:58:07 EDT 2018


I've got a situation I've searched for answers on, and though I've found
similar questions, I've found no generally-applicable answers, or at least
ones I can use. Apologies in advance for a beginner-level question.

I have a group of applications that all use some common functionality I've
located in a static library (for logging in this case). we'll call it
libmylog here. Libmylog is pretty simple, but at some point I wanted to
solve some thread safety situations, so I included some synchronization
features that now require me to link with pthreads. Easy enough,
"target_link_libraries(mylog pthread)" takes care of it.

I've recently begun developing a separate application which doesn't belong
in the same tree, and so I've put it in its own new project. I've decided
I'd like this new application to use libmylog as well, and so now I've
moved mylog into its own separate project too. I've added install
directives and the library (libmylog.a) and its header file end up in the
correct spots in /usr/local when I do a "make install". Looks good so far.

The problem is when I compile any of my executables, the link fails with
undefined reference errors since after removing libmylog from the same
build tree every application I've written that uses it must now link with
libpthread as well. This is an odd thing to have to specify for small
utilities that don't have threads and shouldn't need to care about them. I
look at libmylog.a with nm and all the references to pthreads symbols are
undefined.

More critically, it seems that since the build process for libmylog doesn't
generate an executable, the "target_link_libraries(mylog pthread) line is
now _silently ignored_.

What is the Right Way of dealing with this in CMake? I'd like a way to tell
the linker to force the inclusion of the relevant code in the static
library, but if there's a more canonical approach I'd appreciate the
advice. (For example, do I need to [learn how to] create a CMake "package"
for my libmylog installation -- would this help propagate the -lpthread
requirement to the build for the executables?)


Thanks!
Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180925/43de14ee/attachment.html>


More information about the CMake mailing list