[CMake] simple cmake example

Michael Hertling mhertling at online.de
Sat Oct 8 03:07:44 EDT 2011


On 10/08/2011 05:23 AM, Tim Coddington wrote:
> Thank you.  After adapting your file ccmake complains about line 7:
> 
>   include(${MRPT_USE_FILE})
> 
> "CMake Error at CMakeLists.txt:7 (include):
>    include called with wrong number of arguments.  Include only takes
> one file."
> 
> How is MRPT_USE_FILE defined?

AFAICS, it's not defined at all in MRPT's configuration file, so leave
out this INCLUDE(${MRPT_USE_FILE}) line and try anew. Besides, MRPT's
config file has several flaws which I tend to rate as quite critical:
ADD_DEFINITIONS(), INCLUDE_DIRECTORIES() and LINK_DIRECTORIES(). Such
functions shouldn't appear in config files or find modules since they
modify the current scope's compilation environment, something a user
might not expect.

Additionally, I'd recommend to use two CMakeLists.txt files in your
exemplary project: A top-Level one containing ADD_SUBDIRECTORY(src),
and a src/CMakeLists.txt identical to the one John has proposed but
with adapted paths, of course, i.e.

SET(${PROJECT_NAME}_SRC
    file3.cpp
    file4.cpp
    main.cpp
)

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

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

and without the CMAKE_MINIMUM_REQUIRED() AND PROJECT() commands.

IMO, the CMakeLists.txt files should follow the project's organization,
and you organize the sources in the src subdirectory, so there should
be a src/CMakeLists.txt. The top-level CMakeLists.txt suits perfectly
to add things like additional targets to generate documentation or to
account for further source directories, e.g. supplementary or third
party components which you possibly don't want to mix up with your
project's actual source tree.

Regards,

Michael

> On Fri, 2011-10-07 at 18:27 -0400, John Drescher wrote:
>> 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