CMake and Eclipse: Issues Resolved

This post addresses some issues with using CMake in Eclipse. CMake is a cross-platform build system that I have found to be a good substitute for Autotools. Although CMake provides considerable support for using it as a build system for Eclipse projects, it has one big shortcoming: There is no support for version control within Eclipse if the build directory is separate from the source directory of the project. For more details about this issue, see Option 1 of the CMake: Eclipse UNIX Tutorial.

To allow our project to have a separate source and build directory, while enabling support for CMake and version control from within Eclipse, we will do the following:

  1. We will first get sources of the project from a SVN repository in a top level ‘src’ folder
  2. We will use CMake to generate Makefiles, and Eclipse project configuration files in the ‘src’ folder
  3. We will then delete all the Makefiles and CMake cache files, but leave the Eclipse project configuration files in place
  4. We will create a separate ‘build’ directory to compile our sources
  5. We will regenerate the Makefiles for sources in this ‘build’ directory using CMake
  6. We will import the sources as a single project in Eclipse
  7. We will tell Eclipse to build our project in the ‘build’ directory rather than the ‘src’ directory
  8. Finally, we will work with Eclipse as usual

Ok, let’s begin.

  1. Getting the sources of your (not yet) Eclipse project from SVN: I’ll let you do this on your own and assume that the sources have been placed in a folder named ‘src’.
  2. Generating Makefiles: CMake is not a replacement for Makefiles. It allows you to simply generate Makefiles for your project. You need to provide CMake with some build information (in a file called CMakeLists.txt) and it will do the rest. For example, you can tell CMake which sources must be used to build which executable, without worrying about dependencies etc. Here is a sample CMakeLists.txt file:
  3. project (tpm_integrity)
     
    cmake_minimum_required(VERSION 2.6)
     
    # Lets you see the command executed by make
    #set(CMAKE_VERBOSE_MAKEFILE true)
     
    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${tpm_integrity_BINARY_DIR}/lib)
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${tpm_integrity_BINARY_DIR}/lib)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${tpm_integrity_BINARY_DIR}/bin)
     
    include_directories(
      ${tpm_integrity_SOURCE_DIR}/common
      )
     
    link_directories(
        )
     
    set(DEBUG_DEFINES
        #RPC_DEBUG=1
        #TPM_DEBUG=1
        )
     
    set(RELEASE_DEFINES
        #DO_MCAST=1
        ) 
     
    set(SRCS
        common/TPMRPC.cpp
        common/tpm_msg_lib.c
        common/listener.c
        common/listener_long_msg.c
        integrityMain.cpp
        )
     
    add_executable(aggregator
        ${SRCS}
        )
     
    set_target_properties(
      aggregator
      PROPERTIES
      COMPILE_DEFINITIONS_DEBUG
      "${DEBUG_DEFINES}"
      COMPILE_DEFINITIONS
      "${RELEASE_DEFINES}"
      COMPILE_FLAGS
      "-Wall -static"
      )

    Given this sort of information Cmake can easily generate a Windows Makefile or a Unix Makefile with the help of a ‘generator’. In our case, we will use a special generator called the ‘Eclipse CDT4 Generator’ and not only create a Unix Makefile, but also the Eclipse project configuration files.

    $ cd src
    $ cmake -G"Eclipse CDT4 - Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug .
  4. Deleting all CMake generated files except the Eclipse project configuration files: In step 2, CMake should have created two files that Eclipse recognizes as project configuration files: ‘.project’ and ‘.cproject’, Makefiles, and a bunch of other files and directories that are needed by CMake. Since we don’t want to build our project in the ‘src’ directory we will delete the CMake generated files from ‘src’ and recreate them in a new ‘build’ directory.
  5. # make sure you are in the 'src' directory
    $ find -name 'CMakeFiles' -prune -or -name 'cmake_install.cmake' -or -name 'CMakeCache.txt' -or -name 'Makefile' | xargs rm -rf
  6. Create a new ‘build’ directory. This is simple.
  7. $ cd ../
    $ mkdir build
  8. Regenerate Makefiles
  9. $ cd build
    $ cmake -i ../src

    Notice that this time we did not specify a generator. Why? Because we want CMake to use the default generator (i.e. for UNIX Makefiles) rather than the one for Eclipse CDT.

  10. Import sources as a single project in Eclipse: Open Eclipse, click File->Import->Existing Project Into Workspace (Keep the Copy projects into workspace box unchecked.)
  11. Tell Eclipse to build project in the ‘build’ directory: Select Project->Properties->C/C++ Make Project. Change the value of the Build directory field to the path to your build directory.
  12. And you’re all done.

One thought on “CMake and Eclipse: Issues Resolved

  1. Hi Akshay,

    Nice article ! I was trying to do this for a week and still I was having problems with getting the debugger to work. Later I found a recent article [http://www.voom.net/use-cmake-with-eclipse] and it did the trick. Now I can use the debugger in Eclipse properly. Not sure if there was any significant different the two methods however later one seems to work. Note that I had to specify my “Build directory” as opposed to leaving it blank, as mentioned in the new article.

    Having said all that, I could be a problem with my Eclipse configuration as opposed to a problem with the method.

    Regards
    Dilan

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>