title: Linking GSL to C via make
body: <p>Umpteenth linking question. I am trying to build some simple C code that calls the GNU scientific library. However, the GSL folder is not nested in my project folder. So, the code lives in, say, <code>C:/c-examples/</code> and the GSL library is <code>C:/gsl</code>. </p>
  
  <p>This is the C code</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;gsl_math.h&gt;
  #include &lt;fit/gsl_fit.h&gt;
  
       int
       main (void)
       {
         int i, n = 4;
         double x[4] = { 1970, 1980, 1990, 2000 };
         double y[4] = {   12,   11,   14,   13 };
         double w[4] = {  0.1,  0.2,  0.3,  0.4 };
  
         double c0, c1, cov00, cov01, cov11, chisq;
  
         gsl_fit_wlinear (x, 1, w, 1, y, 1, n,
                          &amp;c0, &amp;c1, &amp;cov00, &amp;cov01, &amp;cov11,
                          &amp;chisq);
  
         printf ("# best fit: Y = %g + %g X\n", c0, c1);
         printf ("# covariance matrix:\n");
         printf ("# [ %g, %g\n#   %g, %g]\n",
                 cov00, cov01, cov01, cov11);
         printf ("# chisq = %g\n", chisq);
  
         for (i = 0; i &lt; n; i++)
           printf ("data: %g %g %g\n",
                          x[i], y[i], 1/sqrt(w[i]));
  
         printf ("\n");
  
         for (i = -30; i &lt; 130; i++)
           {
             double xf = x[0] + (i/100.0) * (x[n-1] - x[0]);
             double yf, yf_err;
  
             gsl_fit_linear_est (xf,
                                 c0, c1,
                                 cov00, cov01, cov11,
                                 &amp;yf, &amp;yf_err);
  
             printf ("fit: %g %g\n", xf, yf);
             printf ("hi : %g %g\n", xf, yf + yf_err);
             printf ("lo : %g %g\n", xf, yf - yf_err);
           }
         return 
  }
  </code></pre>
  
  <p>And here is the makefile I wrote for it:</p>
  
  <pre><code>CC=gcc
  CFLAGS=-Wall -IC:/gsl -lgsl
  OLSexample: OLSexample.o 
  
  clean:
      rm -f OLSexample OLSexample.o
  </code></pre>
  
  <p>However, running <code>make</code> on this exits with error 2, file not found. I think I might be doing something wrong in the makefile specifying the dependencies, or in linking the libraries. Any help is welcome.</p>
  
  <hr>
  
  <p><strong>EDIT2:</strong></p>
  
  <p>Following mux's advice, and the template <a href="http://mrbook.org/tutorials/make/Makefile-4" rel="nofollow">here</a> I changed the makefile to the following (including the full paths to the library). I continue to get the previous error (e=2).</p>
  
  <pre><code>CC=gcc
  CFLAGS=-c -Wall -IE:/programming/c/libraries/gsl-1.15.tar/gsl-1.15/
  LDFLAGS= -LE:/programming/c/libraries/gsl-1.15.tar/gsl-1.15/
  LIBS= -lgsl
  SOURCES=OLSexample.c
  OBJECTS=$(SOURCES:.c=.o)
  EXECUTABLE=OLSexample
  
  all: $(SOURCES) $(EXECUTABLE)
  
  $(EXECUTABLE): $(OBJECTS)
      $(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $@
  
  .c.o:
      $(CC) $(CFLAGS) $&lt; -o $@
  </code></pre>
  
  <p>The complete error message is included here for reference:</p>
  
  <pre><code>e:\programming\c\WorkingFolder\gslExamples\1-ols&gt;make
  make
  gcc -c -Wall -IE:/programming/c/libraries/gsl-1.15.tar/gsl-1.15/   -c -o OLSexample.o OLSexample.c
  process_begin: CreateProcess((null), gcc -c -Wall -IE:/programming/c/libraries/gsl-1.15.tar/gsl-1.15/ -c -o OLSexample.o OLSexample.c, ...) failed.
  make (e=2): The system cannot find the file specified.
  make: *** [OLSexample.o] Error 2
  </code></pre>