[CMake] Moving static library to separate project

Andreas Naumann Andreas-Naumann at gmx.net
Wed Sep 26 03:56:29 EDT 2018


Dear Chris,

you have several ways to cope with that.
First, you should write a MyLogConfig.cmake-File [2], which imports your 
static library as import library [1] and sets the include path. The same 
file also defines the dependency on pthread via target_link_library.
Your project with the executable then uses find_package(MyLog ) to 
insert the library into the project.

The second way uses the install(Export ...) directive to create export 
files. These files contain the transitivie dependencies and the include 
paths at once. In this case, your executable project has to use the 
export-File via find_file / input.

Hope that helps a little bit,
Andreas


[1] https://cmake.org/cmake/help/v3.5/command/add_library.html
[2] https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html

Am 25.09.2018 um 23:58 schrieb Christopher E Robison:
> 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
>
>
>
>
>
>
>
>



More information about the CMake mailing list