Monday, February 24, 2014

Netbeans and CppUnit on Mac OS X Mavericks

To continue logging my progress with NetBeans.

I wanted to get into this whole unit testing business, since there's never been a class I actually used that for, and a lot of actual jobs want you to know it. So, since NetBeans is my current IDE of choice (I actually do not have a reason  for this, except that I managed to make GDB and C++ work on it) and since the project has a logical folder called "Test Files", I decided to give it a try.

I'm mostly using this guide, but it's sort of misleading, since it's doing CUnit and I wanted to do CppUnit. So, I followed that guide to install CUnit, and then I went on to add a CppUnit, oops. Turns out those two are different, so I had to install the CppUnit also. Which was confusing on Mac OS (I downloaded the file but couldn't figure out how to configure it on the first go through). 

Then I remembered that I have Homebrew installed, so I decided to take full advantage of that. So, I did a:
brew install cppunit
which worked out-of-the-box.

And then I went on following the guide. I wrote the equivalent of their example (yeah, removed the .h includes essentially)
#include <cstdlib>
#include <iostream>

using namespace std;

long factorial(int arg) {
    long result = 1;
    int i;
    for (i = 2; i <= arg; ++i) {
        result *= i;
     }
    return result;
}

int main(int argc, char** argv) {
    printf("Type an integer and press Enter to calculate the integer's factorial: \n");
    int arg;
    cin>>arg;
    cout<<"factorial("<<arg<<") = "<<factorial(arg)<<endl;
    return 0;
}

And then I right-clicked on the cpp file, selected Create Test -> New CppUnit Test, and then selected my cpp file (at the top of the options, which automatically also selected the factorial function). I left everything else default, per the tutorial.

And then I happily right clicked the Test Files logical folder, and selected Test from there, only to get a bunch of errors. Turns out Homebrew doesn't put the cppunit files where Netbeans expects them.

Turns out the newtestclass.h file contains this include
#include <cppunit/extensions/HelperMacros.h>
And the compiler has no idea where to look for that. This was an easy fix:
Preferences -> C/C++ -> Code Assistance -> C++ Compiler

Select Add on the right-hand-side, and add this:
/usr/local/Cellar/cppunit/1.12.1/include
So, that gets rid of the warning, but then the big error came.
/bin/sh: cppunit-config: command not found
along with many other information.

But! If I went to my terminal and ran
cppunit-config
it worked!

So, I googled forever for an answer, which took me through the netbeans.conf file to no avail. The only thing that worked was starting Netbeans from the terminal.


Note: I should now have syntax highlighting here, thanks to Awesome code syntax highlighting made easy and SyntaxHighlighter (which is the main reason I quoted that much code). But it took me forever to get this to work, I'll make another post right after this one.

1 comment:

  1. Redefine your environment variable PATH at your NetBeans /bin/sh configuration file in order to make cppunit-config available. As usual you can get cppunit-config path with a humble "which cppunit-config":

    /usr/local/bin/cppunit-config

    So in my case the file /Applications/NetBeans/NetBeans\ 8.2.app/Contents/Resources/NetBeans/bin/netbeans needed these extra lines:

    PATH=$PATH:/usr/local/sbin:/usr/local/Cellar/perl/5.24.0_1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/TeX/texbin
    export PATH

    See latest lines in http://wiki.netbeans.org/MacOSXEnvForApp for further info.

    ReplyDelete