[CMake] simple cmake example

John Drescher drescherjm at gmail.com
Fri Oct 7 18:27:56 EDT 2011


On Fri, Oct 7, 2011 at 6:00 PM, Tim Coddington
<tim.coddington at rteamworks.com> wrote:
> Hi
>   I recently started working with open source code that uses cmake for
> build.  I've been able to make do since most of the CMakeLists.txt files
> are provided or specific instructions are given.
>
>   I'm about to embark on my first contribution to build a driver for an
> IMU and I want to build an example that is very similar to mine but it's
> been "extracted" from an existing code tree, i.e. current CMakeLists.txt
> files would port easily.
>
>   I'm having problems constructing what should be a relatively
> straightforward CMakeLists.txt file for the extracted code that I want
> to run in isolation.  I've been reading tutorials, etc but I don't seem
> to get it.  Please consider
> taking my general C++ example described below and tell me what (exactly)
> the CMakeLists.txt should look.
>
> [Ubuntu 11.04, cmake 2.8.3, C++ code]
>
> Given:
> Top level directory "proj/" and subdir "proj/src"
>
> The proj/ directory shall contain the parent CMakeLists.txt and
> proj/src shall contain a child CMakeLists.txt (if
> necessary/appropriate).
>
> The src/ directory contains these source related files:
>
> file1.h       >> header only no associated cpp file
> file2.h       >> header only "
> file3.h
> file3.cpp
> file4.h
> file4.cpp
> main.cpp
>
> In addition to standard references, some of these files depend on
> external library which is found with the FIND_PACKAGE(MRPT REQUIRED
> hwdrivers slam)    [note: these libraries come from Mobile Robot
> Programming Toolkit]
>
> Please tell me what the two (or one) CMakeLists.txt file(s) should
> contain in order to build this code.
>

Something like the following:

cmake_minimum_required(VERSION 2.8)

PROJECT(MYProjectName)

FIND_PACKAGE(MRPT REQUIRED hwdrivers slam)
include(${MRPT_USE_FILE})

 # Set your files and resources here
SET(${PROJECT_NAME}_SRC
./src/file3.cpp
./src/file4.cpp
./src/main.cpp
)

SET(${PROJECT_NAME}_HDR
./src/file1.h
./src/file2.h
./src/file3.h
./src/file4.h
 )

include_directories(src)

ADD_EXECUTABLE( ${PROJECT_NAME} ${${PROJECT_NAME}_SRC} ${${PROJECT_NAME}_HDR})

TARGET_LINK_LIBRARIES( ${PROJECT_NAME} ${MRPT_LIBRARIES)


More information about the CMake mailing list