[CMake] Standard template for building and installing a library

Littlefield, Tyler tyler at tysdomain.com
Tue Sep 10 17:38:43 EDT 2013


Hello:
Thanks for the info. Here's what I have so far. In my top-level dir:
#xlog CMake file
cmake_minimum_required (VERSION 2.6)
project(xlog)
Set(xlog_VERSION_MAJOR 1)
Set(xlog_VERSION_MINOR 0)
add_subdirectory(lib)
in lib:
#lib configuration
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -march=native")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -ggdb -O3")
include_directories(includes)
set(src_files
src/test.cpp
)

add_library(xlog ${src_files})

I have a few questions regarding this.
First, I can create a demo directory and from there I can have a 
cmakelists.txt per demo. Is there a way somehow to link to the generated 
static library?

If I move the CXXFLAGS_DEBUG/etc to a higher level file, is there a way 
to insure that it promotes down?
My final question is how to make the debug/release build work, as well 
as to allow to let this install. I need to install libxlog.a and the 
include directory
as well to includedir/xlog/*. Is there a typical method to do this?
Thanks,

On 9/10/2013 1:34 AM, Micha Hergarden wrote:
> 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
>


-- 
Take care,
Ty
http://tds-solutions.net
He that will not reason is a bigot; he that cannot reason is a fool; he that dares not reason is a slave.



More information about the CMake mailing list