c/c++ · eclipse · linux

eclipse: importing a cmake project

CMake is an open-source, cross-platform family of tools designed to build, test and package software. It is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice.

In this post we are going to learn how to transform your cmake project into an Eclipse project.

Installing Cmake

The easiest way to install Cmake is to follow one one multiples paths available at its download page. As this process may vary, we wont detail each one.

I’m using Ubuntu 16.04, so my steps were:


$ apt-cache search cmake
$ sudo apt-get install cmake
$ cmake -version
cmake version 3.5.1

Creating a Hello World Cmake Project

Now that Cmake is installed we can create our first project. For the for the sake of simplicity we won’t go too deep into too much details. Cmake has a great documentation from where you can learn a lot.

The  first step is to create a file called CMakeList.txt. This is a persistent key/value string storage which is used to cache value between runs. Values stored in here can be paths to library dependencies or whether an optional component is to be built at all.

First, create a directory to host your code:


mkdir ./simple_project

Then create an empty CMakeList.txt:


touch CMakeList.txt

And add the following content:


cmake_minimum_required(VERSION 2.8)
project(my_simple_project)
add_executable(simple_project main.c)

Now we can create our simple_project application:


vi ./simple_project/main.c

And add its very complex logic:

#include<stdio.h>;

int main(){
    printf("Hello World");
return 0;
}

At this point you should have something like this:

rpsene@rpsene:~/simple_project$ tree
.
├── CMakeLists.txt
└── main.c

Now, is time to build using Cmake. It is very simple, just issue the following command:

rpsene@rpsene:~/simple_project$ cmake ./CMakeLists.txt
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/rpsene/simple_project
 

And that’s all. All the necessary build files where automatically generated:

rpsene@rpsene:~/simple_project$ tree
.
├── CMakeCache.txt
├── CMakeFiles
│   ├── 3.5.1
│   │   ├── CMakeCCompiler.cmake
│   │   ├── CMakeCXXCompiler.cmake
│   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   ├── CMakeSystem.cmake
│   │   ├── CompilerIdC
│   │   │   ├── a.out
│   │   │   └── CMakeCCompilerId.c
│   │   └── CompilerIdCXX
│   │   ├── a.out
│   │   └── CMakeCXXCompilerId.cpp
│   ├── cmake.check_cache
│   ├── CMakeDirectoryInformation.cmake
│   ├── CMakeOutput.log
│   ├── CMakeTmp
│   ├── feature_tests.bin
│   ├── feature_tests.c
│   ├── feature_tests.cxx
│   ├── Makefile2
│   ├── Makefile.cmake
│   ├── myapp.dir
│   │   ├── build.make
│   │   ├── cmake_clean.cmake
│   │   ├── DependInfo.cmake
│   │   ├── depend.make
│   │   ├── flags.make
│   │   ├── link.txt
│   │   └── progress.make
│   ├── progress.marks
│   └── TargetDirectories.txt
├── cmake_install.cmake
├── CMakeLists.txt
├── main.c
└── Makefile

Now we just invoke make to build it:

rpsene@rpsene:~/simple_project$ make
Scanning dependencies of target myapp
[ 50%] Building C object CMakeFiles/myapp.dir/main.c.o
[100%] Linking C executable myapp
[100%] Built target myapp

And finally run the application:

rpsene@rpsene:~/simple_project$ ./myapp
Hello World

Importing the project into Eclipse

CMake has an option which allows to generate the .project file necessary to convert the raw CMake project into an Eclipse CDT project.

To do it, we need to set this option when invoking CMake:

rpsene@rpsene:~/simple_project$ cmake -G "Eclipse CDT4 - Unix Makefiles" ./CMakeLists.txt
-- Configuring done
-- Generating done
-- Build files have been written to: /home/rpsene/simple_project

The result is two hidden files: .project  and .cproject:

rpsene@rpsene:~/simple_project$ ls -la
total 68
drwxrwxr-x 3 rpsene rpsene 4096 Set 7 23:17 .
drwxr-xr-x 52 rpsene rpsene 4096 Set 7 23:10 ..
-rw-rw-r-- 1 rpsene rpsene 11533 Set 7 23:07 CMakeCache.txt
drwxrwxr-x 5 rpsene rpsene 4096 Set 7 23:17 CMakeFiles
-rw-rw-r-- 1 rpsene rpsene 1365 Set 7 23:07 cmake_install.cmake
-rw-rw-r-- 1 rpsene rpsene 88 Set 7 21:59 CMakeLists.txt
-rw-rw-r-- 1 rpsene rpsene 4744 Set 7 23:17 .cproject
-rw-rw-r-- 1 rpsene rpsene 74 Set 7 23:10 main.c
-rw-rw-r-- 1 rpsene rpsene 4683 Set 7 23:17 Makefile
-rwxrwxr-x 1 rpsene rpsene 8608 Set 7 23:10 myapp
-rw-rw-r-- 1 rpsene rpsene 3737 Set 7 23:17 .project

Now you can easily import this simple_project as a Makefile project into you Eclipse instance 🙂

File > Import

import-make

make_2

screenshot-from-2016-09-07-23-28-39

Right click the project and select build:

screenshot-from-2016-09-07-23-29-46

To run, right-click the project again select Run and Run Configurations. Then create a new launch:

screenshot-from-2016-09-07-23-32-35

And press Run:

screenshot-from-2016-09-07-23-33-20

That’s all!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s