[CMake] Standard template for building and installing a library

Micha Hergarden micha.hergarden at gmail.com
Tue Sep 10 01:34:33 EDT 2013


On 09/09/2013 10:07 PM, Littlefield, Tyler wrote:
> Hello:
> I had a quick question. Is there a sort of standard template to build
> a library? I've been looking around and an mot really sure where to
> go--most of the tutorials show only using one file.
> I'd also like to know if there's a way to build demos and tests. I'm
> thinking my setup will look something like:
> |top_dir|
> -lib-
> -demos-
> -tests-
>
> Thanks,
>
Hello Tyler,

Which tutorial are you referring to? The standard way to build a library
is by using 'add_library(<your_library_name> <sourcefile1> <sourcefile2>
... )'
However, you are not telling us if you want a static or dynamically
linked library, and also which lanquage / platform you are targetting. A
bit more information would help us help you. :)

For the second question:
CMake works its way through all directories and generates a buildsystem
from the encountered CMakeLists.txt files. You will have to tell cmake
which subdirectories there are of course. So in your example you could
write a CMakeLists.txt file in the [top_dir] which contains:

add_subdirectory(-lib-)
add_subdirectory(-demos-)
add_subdirectory(-tests-)

And then in each subdirectory you could write a CMakeLists.txt file
which could add an executable ( add_executable(<your_exe_name>
<sourcefile1> <sourcefile2> ...) ) or the above add_library.

I suggest you search and download a few projects that use cmake to see
how others have implemented their projects. If you have some money
burning in your pocket you can also buy the 'Mastering CMake' book. Its
a bit outdated, but the basic information on how to set up a project
still holds.

With kind regards,
Micha Hergarden

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: OpenPGP digital signature
URL: <http://www.cmake.org/pipermail/cmake/attachments/20130910/636fa441/attachment.pgp>


More information about the CMake mailing list