Showing posts with label Eclipse. Show all posts
Showing posts with label Eclipse. Show all posts

Monday, October 29, 2012

How to change the code templates date format in Eclipse Indigo CDT

As I commented in a previous post, last week I spent some time configuring my recently installed version of Eclipse CDT.

After importing my preferences and settings, using Tomáš Kramár's script, I realize that the date format that appeared when using the code template for class comments didn't have the right format. I was getting Oct 26, 2012 instead of 26/10/2012. I needed to change the locale date format.

After googling a bit, I found that what I had to do was just adding the following two lines to the eclipse.ini file:
-Duser.language=es 
-Duser.region=ES

Script to create a new Eclipse workspace with all your old settings

Last week I had to configure my new installation of Eclipse.
I'd like to thank Tomáš Kramár for his script to create a new Eclipse workspace with all my old settings:
It saved me a lot of time.

Friday, March 2, 2012

How to install cppcheck and use it in Eclipse CDT

I have to thank my colleague Dani for this post.

Cppcheck is an static analysis tool for C/C++ code that we are using at work.
It's helping us to detect some problems that the compiler can not detect.

1- Install cppcheck:
  • Go to cppcheck project site and download the current version of cppcheck.
  • Untar it and compile it (e.g. "make" in the corresponding folder).
  • Move it to a convenient folder (e.g. /home/bscuser/ )
2- Install the cppcheclipse plugin to use cppcheck from Eclipse:
  • In Eclipse, go to Help > Eclipse Marketplace
  • Look for "cppcheclipse" in the bar and click in "install"
3- Configure it:
  • In Eclipse, go to Window > Preferences. In the C/C++ > cppcheclipse select the binary path (e.g. /home/bscuser/cppcheck-1.52/cppcheck)
Now we are going to configure cppcheclipse for this particular project.
The next menus can be found in Eclipse, go to Project > Properties.
  • The configuration in the cppcheclipse menu can be found in the following snapshot:
  • In cppcheclipse > Problems choose "Use project settings" and unselect "portability" (we are still not interested in this point). Keep the other ones selected.
4- Run it!
  • In the Project Explorer (by default at the left of the screen) right click on "src" and select cppcheck > Run cppcheck. Alternatively, use the shortcut: Shift + Ctrl + c.
The results are prompt in an alternative Console. To see them click on "Display Selected Console" and choose the "cppcheck" one, or open a new one with the "Open Console" button.
Have fun!

Thursday, February 23, 2012

How to install Boost C++ libraries in Ubuntu and use them in Eclipse CDT

Boost is one of the most popular C++ development libraries. If you want to use them in your C++ project in Eclipse CDT in an Ubuntu environment, follow these instructions:

Installing the package
In the last Ubuntu releases, boost libraries have been split in different packages. The fastest option is to install the whole library:
$ sudo apt-get install libboost-dev
Adding the library in Eclipse CDT
Right click into your C++ project, select "Properties". Go to "C/C++ Build" options and here select "Settings". In the "GCC C++ Compiler" subsection, select "Includes" and add in the "Includes files (-include)" field, try to include any of the Boost C++ headers.
In our case we wanted to use the random numbers libraries, so we included the header file: "/usr/include/boost/random.hpp". After having done this, click on Apply.
You'll probably need to "Clean" your project before everything compiles again.

Once you've finished the settings, try to include any of the Boost sub-libraries, for example creating a new C++ class and including the following lines:
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_01.hpp>
If there is no problem at compiling nor linking, Boost C++ libraries are ready to be used in your machine.

Update
These setting instructions are fine for Eclipse Indigo.
For Eclipse Helios, though, one of the steps is different:
- To include the Boost C++ header you need to go to "GCC C Compiler" subsection instead of "GCC C++ Compiler" .

Friday, November 18, 2011

Configuring Eclipse for TDD in Python: The Nosetests way

In my daily work, Python is my main (and really beloved) programming language. For coding in Python, I usually work with vim in remote machines and Eclipse IDE in my desktop one. In the next sections I'll explain how to convert Eclipse in the killer TDD Python-IDE, but you can have a look at the official Python Wiki for more options: Python IDE's and Python Editors.

1. Installing Eclipse
In the Eclipse downloads section, get the Eclipse Classic package that corresponds to your platform (I'll focus on Linux in this post, but it should be easy to follow the same steps in Windows and Mac OS platforms as well).
Installing Eclipse is as easy as extracting the downloaded package if you have an existing Java Virtual Machine installed in your system. If not, check this FAQ.

2. PyDev
PyDev is a Python IDE for Eclipse with a lot of goodies such code completion, syntax highlighting, debugger, unit testing support and refactoring options. You can find a very nice installation manual in the offical project page: PyDev Manual (Installing). For lazy readers, it should be as easy as opening Eclipse, go to Help > Install New Software, adding a new software location with the Add button and fill in the location field with the url http://pydev.org/updates. Once added, check PyDev option and follow the instructions.

3. Nosetests
python-nose or nosetests is an unit test-based framework that is capable of discover and execute your tests without having to register them first. Nose makes my life much easier writing test functions to check for exceptions and discovering and executing my test packages. However, I strongly suggest you to read this article to understand which are the main motivations to use a unit test framework and why choosing nose.

Nose is available via setuptools:
$ easy_install nose

Now try to import nose in python:
$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import nose
>>>

If nose installation is fine, you shouldn't see any error messages after importing the nose package in the Python console.

If you don't have setuptools installed in your system, there are a few more options to install nose package, check the official installation guide.

4. Configuring Eclipse for continuous-testing
First of all, create a new PyDev project: File > New > Project, navigate to PyDev and choose "PyDev Project". Click "Next", choose a project name (in our case "tdd-example"), select Grammar Version as 2.6 or 2.7 (nose supports Python 3 syntax, but many plugins not) and select the option "Add project directory to the PYTHONPATH?". Finally, click "Finish". You should see now in the PyDev Package Explorer view your new tdd-example project.

Now, go to Run >  External Tools > External Tools Configurations. In Program option, create a new one. Name could be "NoseTestTool", working directory will be "${project_loc}" and in location the path to nosetests binary, in my case "/usr/local/bin/nosetests".

Right click in the project tdd-example > Properties and navigate to Builders. Click in Import and choose the previous created NoseTestTool. Make sure is the first option selecting it and clicking the Up button. Now, click in Edit. In the Main tab, add in Arguments field "--processes=4" where processes is the number of simultaneous threads that will be used to execute your tests. In my case, I use 4 cores, so I set processes to 4.
In the Environment tab, create a new variable with name "PYTHONPATH" and value "${project_loc}".
In Build Options tab, check the options "Allocate Console (necessary for input)", "Launch in background", "After a Clean", "During manual builds" and "During auto builds".

To test that everything is configured fine, create a new Python file in your project, test_all.py and include the following code:

def test1():
    assert True
    
def test2():
    assert False 

At the time that you save the file, nose may execute your tests automagically and an ouput like this should be shown:

.F
======================================================================
FAIL: test_all.test2
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/nose-1.1.2-py2.6.egg/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/dev/workspace/python/tdd-example/test_all.py", line 6, in test2
    assert False
AssertionError

----------------------------------------------------------------------
Ran 2 tests in 0.002s

FAILED (failures=1)

Nose searches for files and functions that start with "test*". In our case, two functions will be tested, test1 and test2, and one of them will fail (assert False) as seen in the previous output.

I hope this howto will motivate people working with continuous TDD in Python :)

Monday, June 6, 2011

Using JsonCpp to parse JSON in C++

We are using JSON (JavaScript Object Notation) to write the configuration files for our simulations.

JSON is a lightweight data-interchange format. The good thing is that JSON is easy for humans to read and write and that you can find good C++ libraries that parse and write it.

We're using one of them: JsonCpp.

There were several other C++ libraries listed in the JSON project website, but JsonCpp's documentation convinced me to use it.

I installed JsonCpp as a shared library (I'll soon post here how to do it in Linux and MacOs) and wrote a tiny example to test it:

#include <cstdio>
#include <cstring>

// This is the JSON header
#include "jsoncpp/json.h"

using namespace std;

int main(int argc, char **argv)
{
 string json_example = "{\"array\": \
                            [\"item1\", \
                            \"item2\"], \
                            \"not an array\": \
                            \"asdf\" \
                         }";

 // Let's parse it  
 Json::Value root;
 Json::Reader reader;
 bool parsedSuccess = reader.parse(json_example, 
                                   root, 
                                   false);
  
 if(not parsedSuccess)
 {
   // Report failures and their locations 
   // in the document.
   cout<<"Failed to parse JSON"<<endl 
       <<reader.getFormatedErrorMessages()
       <<endl;
   return 1;
 }
  
 // Let's extract the array contained 
 // in the root object
 const Json::Value array = root["array"];
 
 // Iterate over sequence elements and 
 // print its values
 for(unsigned int index=0; index<array.size(); 
     ++index)  
 {  
   cout<<"Element " 
       <<index 
       <<" in array: "
       <<array[index].asString()
       <<endl;
 }
  
 // Lets extract the not array element 
 // contained in the root object and 
 // print its value
 const Json::Value notAnArray = 
               root["not an array"];
 
 if(not notAnArray.isNull())
 {
   cout<<"Not an array: "
       <<notAnArray.asString()
       <<endl;
 }
 
 // If we want to print JSON is as easy as doing:
 cout<<"Json Example pretty print: "
     <<endl<<root.toStyledString()
     <<endl;
 
 return 0;
}

And this is the output:
$ g++ -o test_json test_json.cpp -ljson_linux-gcc-4.4.5_libmt
$ ./test_json 
Element 0 in array: item1
Element 1 in array: item2
Not an array: "asdf"

Json Example pretty print: 
{
   "array" : [ "item1", "item2" ],
   "not an array" : "asdf"
}

There are many other useful methods in JsonCpp's API.

Using a library like JsonCpp, we avoided all the parsing and could focus only on how to use the configuration data. This saved us a lot of time.