Monday, September 17, 2012

XCode 4 Build Locations (Lion, Mountain Lion)

Apparently, the "most advanced" operating system in the world is not very friendly for power-users and developers.

The new xcode 4 has had a major rehaul. I just updated to mountain lion and the latest xcode (4.4.1). The first annoying feature that you will notice about xcode is the non-intuitive and complex user interface. I can't even find the console or the debugging tools... oh wait, they're placed at the bottom of the screen and they're practically invisible. Maybe it is time to read the documentation... but who has time for that?

In any event, I went ahead and tested a "Bonjour y'all!" code and it worked. but I could not locate the executable file in the project's directory. Are you serious? It turns out that XCode defaults to some location in your library. Fortunately, I just found a way to go back to standard operation and place your executable where it BELONGS:

  1. Open XCODE
  2. go to: Preferences
  3. Click on the "Locations" tab
  4. Change the path for "Derived Data" to relative and that should do it.
  5. If you want to be a bit more picky, click on Advanced
    1. select "Custom"
      1. Select "Relative to Workspace"
That last step will place your executable in its normal location.

Hope that helps.

Cite as:
Saad, T. "XCode 4 Build Locations (Lion, Mountain Lion)". Weblog entry from Please Make A Note.

Friday, September 14, 2012

Calling gnuplot from C/C++ on Mac OS X

Here's another way of calling gnuplot from Mac OS X.

First install gnuplot either from their website or using macports. I like the macports model because it installs all dependencies for you automatically.
  1. Install macports from: 
  2. Open up your terminal
  3. type: sudo port selfupdate (to update port names)
  4. type: sudo port install gnuplot
and Voila! you now have gnuplot installed on your mac. To test it type: gnuplot and you should enter the gnuplot environment. Another caveat that you have to bear in mind when using gnuplot on mac is that you have to tell gnuplot to use x11. You do this as follows:
  1. Open up your terminal
  2. type: gnuplot
  3. type: set terminal x11
Now to the fun stuff. Here's the code that I used before to call gnuplot for windows, but modified to work on Unix based systems such as Mac.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void plotResults(double* xData, double* yData, int dataSize);
int main() {
  int i = 0;
  int nIntervals = 100;
  double intervalSize = 1.0;
  double stepSize = intervalSize/nIntervals;
  double* xData = (double*) malloc((nIntervals+1)*sizeof(double));
  double* yData = (double*) malloc((nIntervals+1)*sizeof(double));
  xData[0] = 0.0;
  double x0 = 0.0;
  for (i = 0; i < nIntervals; i++) {
      x0 = xData[i];
      xData[i+1] = x0 + stepSize;
  for (i = 0; i <= nIntervals; i++) {
      x0 = xData[i];
      yData[i] = sin(x0)*cos(10*x0);
  return 0;
void plotResults(double* xData, double* yData, int dataSize) {
  FILE *gnuplotPipe,*tempDataFile;
  char *tempDataFileName;
  double x,y;
  int i;
  tempDataFileName = "tempData";
  gnuplotPipe = popen("gnuplot","w");
  if (gnuplotPipe) {
      fprintf(gnuplotPipe,"plot \"%s\" with lines\n",tempDataFileName);
      tempDataFile = fopen(tempDataFileName,"w");
      for (i=0; i <= dataSize; i++) {
          x = xData[i];
          y = yData[i];            
          fprintf(tempDataFile,"%lf %lf\n",x,y);        
      printf("press enter to continue...");        
      fprintf(gnuplotPipe,"exit \n");    
  } else {        
      printf("gnuplot not found...");    

This code should run as is. Of course, feel free to take the plot function and use it in your own codes.

Cite as:
Saad, T. "Calling gnuplot from C/C++ on Mac OS X". Weblog entry from Please Make A Note.