Porting a VS project to Eclipse CDT (C++)

Few days ago I tested Eclipse C++ (see here) and was actually pleased with the results. Today in office I needed to work with a C++ pipeline tool and decided to convert it to Eclipse C++ while I was working on it.

Overall it went really smoothly and I like Eclipse C++ more and more. Here are the few things I needed to do.

  1. To edit your project building properties, go into the “Project Explorer” panel on the left, right click your project and select “properties”. Then unfold “C/C++ Build” and select “Settings”. The good thing is you can edit both debug and release configurations to set identical settings, so in the “Configuration” drop down select “[ All Configurations ]”
  2. There is no #pragma comment(lib,”mylib.lib”) to link with libraries. So make sure you go to “MinGW C++ Linker” then “Libraries”. Add the name of the library without .a nor .lib in “Libraries”, and add the path in “Library search path”. The specific project I worked on used DevIL the image library and the linker had no problem linking with the provided libraries. Again the trick is to remove any suffix from the library name. If you enter Devil.lib into the settings, the linker will complain about not being able to find the library. Change that to Devil (without .lib) and the linker will find it.
  3. If you compile your project with default settings you will most likely need to ship it with libraries like libgcc_s_dw2-1.dll or libstdc++-6.dll, so again in the settings page, go to “MinGW C++ Linker” then “Miscellaneous” and in the linker flag box add -static. This will increase a bit your executable size but will make it easier to deploy and overall will be smaller than shipping with all the Dlls needed.

Overall I already made two small projects with Eclipse CDT this week and it went as smoothly as it should, and as smoothly as VS, so the next test will be a project with more Windows / DirectX specific code. But so far, Eclipse CDT seems like a perfectly viable free alternative to VS.

Javascript Performance Playground

Javascript is surprisingly fast in modern browsers, but as for every language you will need to optimize at some point. I found a very useful website called JSPerf which provides a test framework, allows you to create and edit existing test framework, provides online persistence allowing to get wider results than if you ran them by yourself.

A simple example. I was wondering what was the fastest way to convert from a float to an int in Javascript. I had several options, and the best way was not as suggested by some colleagues. And the way to make sure what is the fastest way is to benchmark, and not to guess. The results of the benchmark are here, you can test on your machine by running the test. Your results will be stored and shared with everyone, enhancing the global knowledge. The fastest I found is the following, if you find faster, please let me know.

myInt = myFloat | 0

A second example. What is the fastest typed array to write and read integers? The results are here and sadly there is no fastest answer for all browsers. So if you do not want to complicate your code with browser tests and everything that goes with it, the best solution is to use the following.

var myArray = new Int32Array( size );

A third interesting example about the fastest way to clear a buffer and copy it in a canvas every frame. The results are here. The fastest way is to create a working buffer outside your frame, create another one initialized with the values you want, and copy using the method set every frame.

At initialization time

var external_data_1024 = ctx.createImageData(1024, 1024);
var external_source_1024 = ctx.createImageData(1024, 1024);

During the frame processing

ctx.putImageData(external_data_1024, 0, 0);

Finally a word of caution, make sure you benchmark the right things.

  1. The initial float to int did not include the two solutions I added to the benchmark |0 and >>0, so having something that look fast does not mean you have found the optimal solutions.
  2. The original array speed test included array creation for the write, so the test was measuring creation+write instead of just write, and the results were very different. So make sure to measure what really matters to you.
  3. The last example was a subtle example of the points #1 and #2 above. The initial “fastest” function included unnecessary code in clear_canvas(), adding a new test without that code proved to be faster. The loop test was looping with a double indirection “data.data[i] = 0;”, removing that indirection made it much faster and measured the real filling time instead of the sum of filling time+overhead with “buffer[i] = 0;”.

Testing the performance of Javascript

I had an interesting conversation last Sunday with a friend about the different technologies available on the web, and we talked about benchmarking them with simple things. So I decided to give Javascript a try, and here is what I came up with. You can get the source code here.

To give credit where it is due, I looked at the code of this sample to get a quick start and learn the basics. I will share my learnings in a future blog post.

Logitech Solar Keyboard K750

My original wired iMac keyboard delete key broke, so I considered replacing it with a wireless keyboard. Unfortunately the Apple keyboard is terrible for me without a proper numerical keypad, so I looked at alternatives and decided to get the Logitech Solar Keyboard K750 and I am really satisfied with it.

The look matches the iMac design, it is wireless with a proper numerical keypad, no need of batteries thanks to the solar panels, and it also has a unifying small usb connector so that you can connect all your logitech devices to the same usb instead of needing several ones.

If you use Bootcamp and Windows, you might have issues with F1, F2 keys not working properly. Just download and install Logitech’s Setpoint software, and everything will work as expected.

Trying Eclipse for C++

I have been doing a lot of Java and Flash programming recently and both development environment have been relatively enjoyable with the Eclipse framework, so I decided to give it a try for C++. Yes I know there are other alternatives for C++ like Netbeans, Codeblocks, Codelite, and even MonoDevelop now. But having to be familiar with a single GUI is actually quite enjoyable.

  1. First download and install MinGW it will be your compiler. Untick everything except C compiler and C++ compiler.
  2. Get the latest Eclipse CDT it is the eclipse environment customized for C++.
  3. Launch Eclipse, open the “File” menu, then “New”, then “C++ project”
  4. Expand the Executable box (click on the left of it, sometimes the triangle does not show up on Windows 7). Select “Hello World C++ Project”, put a name, select “MinGW GCC”, and click Finish.
  5. Go to the “Project” menu and select “Build Project”.
  6. You should now be able to run the project and see “Hello world” in the console.

This seems all good, but actually when you try the simplest things like printf, the environment will have many issues like text not showing up. If you google for it you will find pseudo solutions recommending setvbuf( stdout, NULL, _IONBF, 0 ); but this makes things worse and create a whole lot of issues. The best is really to emulate the VS behavior and to have a separate console if you need one.

  1. Create gdbinit.txt in C:\MinGW\bin and put “set new-console” without the quotes in it.
  2. Go to the “Run” menu, then “Debugger” tab, and putĀ  “C:\MinGW\bin\gdbinit.txt” without the quotes in “GDB Command Line” and click “Apply”

Now your printf will output to the external console as they do in VS, and OutputDebugString will output text to the Eclipse console.

Overall although the printf issue is kind of bad, at the end Eclipse seem like a good environment. I like things like CTRL+SHIFT+N to automatically add a missing include file. Next time I need to write a small C++ tool, I will give it a more serious try.

Excellent free alternatives to Maple and Mathematica

I was in office the other day and we do not have a license for Maple nor Mathematica so I decided to try the free alternatives, and I found two excellent ones.

1. Euler Math Toolbox (71MB) is the best option. It provides excellent numerical algorithms like remez to find the minimax polynomial approximation of a function for example. And it is also capable of symbolic calculations like calculating integrals, derivatives, dealing with linear algebra problems. The plotting and multiline commands are not as slick as what you can do in Maple or Mathematica but it is still an excellent software. I found the minimax algorithm to be more robust and easier to use than the Mathematica one for example, and basically as good as the one implemented in Maple

Example to plot a function:


Example to define and multiply a matrix:

A:=[1,1;0,1]; A.A

Example to use Maxima for polynomial expansion:


Example to get the degree 2 minimax polynomial approximating sin(x) between 0 and Pi:

x=equispace(0,Pi,50); {t,d}=remez(x,sin(x),2); P=polytrans(t,d)

2. WxMaxima (32MB) is part of the default Maxima installation and proved to me the closest and best alternative to Maple in terms of symbolic and matrix calculations. At the same time it lacks some important numerical algorithms, which is why I prefer Euler Math Toolbox overall. But since its multiline handling is closer to Maple I still end-up using it for anything requiring flow controls and loops.

Example to integrate/differentiate a function:

integrate(sin(x),x); diff(sin(x),x);

Example to plot two functions:

wxplot2d([sin(x),cos(x)], [x,-5,5]);

Example to define and multiply a matrix:

a:matrix([1,1],[0,1]); a.a;

Example of polynomial expansion:


Example of solving for polynomial roots:


Example of simple computation loop:

for i:1 thru 8 do ( res[i]:mod(z^i,8) )$