title: undefined references when linking with static library with OTL using a makefile
body: <p>I want to link my project to a static library (msodbcsql11.lib), and I want to include it's header (msodbcsql.h) and another header file of a framework otlv4.h) using a simple makefile, but it looks like it can't find the library. Here is my makefile:</p>
  
  <pre><code>CC=g++
  LDFLAGS=
  CFLAGS=-c -Wall
  SOURCES=main.cpp
  LIBB = C:\temp\lib
  LIBINCL = C:\temp\include
  CFLAGS += -I$(LIBINCL)
  LDFLAGS += -L$(LIBB)
  OBJECTS=$(SOURCES:.cpp=.o)
  EXECUTABLE=main
  all: $(SOURCES) $(EXECUTABLE)
  
  $(EXECUTABLE): $(OBJECTS)
      $(CC) $(LDFLAGS) $(OBJECTS) -o $@
  
  .cpp.o:
      $(CC) $(CFLAGS) $&lt; -o $@
  </code></pre>
  
  <p>And here is my c++ code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #define OTL_ODBC_MSSQL_2008 // Compile OTL 4/ODBC, MS SQL 2008
  #include &lt;otlv4.h&gt;
  
  int main(){
      otl_connect db; //undefined reference errors
  }
  </code></pre>
  
  <p>Here is the build when I declared otl_connect:</p>
  
  <pre><code>18:42:33 **** Incremental Build of configuration Default for project mak ****
  make all 
  g++ -c -Wall -IC:\temp\include main.cpp -o main.o
  g++  -LC:\temp\lib main.o -o main
  main.o:main.cpp:(.text$_ZN8otl_connD1Ev[_ZN8otl_connD1Ev]+0x66): undefined reference to `SQLFreeHandle'
  main.o:main.cpp:(.text$_ZN8otl_connD1Ev[_ZN8otl_connD1Ev]+0x66): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SQLFreeHandle'
  main.o:main.cpp:(.text$_ZN8otl_connD1Ev[_ZN8otl_connD1Ev]+0x9e): undefined reference to `SQLFreeHandle'
  main.o:main.cpp:(.text$_ZN8otl_connD1Ev[_ZN8otl_connD1Ev]+0x9e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SQLFreeHandle'
  main.o:main.cpp:(.text$_ZN8otl_conn6logoffEv[_ZN8otl_conn6logoffEv]+0x54): undefined reference to `SQLDisconnect'
  main.o:main.cpp:(.text$_ZN8otl_conn6logoffEv[_ZN8otl_conn6logoffEv]+0x54): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SQLDisconnect'
  main.o:main.cpp:(.text$_ZN8otl_conn5errorER7otl_exc[_ZN8otl_conn5errorER7otl_exc]+0x70): undefined reference to `SQLGetDiagRec'
  main.o:main.cpp:(.text$_ZN8otl_conn5errorER7otl_exc[_ZN8otl_conn5errorER7otl_exc]+0x70): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SQLGetDiagRec'
  main.o:main.cpp:(.text$_ZN8otl_conn6commitEv[_ZN8otl_conn6commitEv]+0x23): undefined reference to `SQLEndTran'
  main.o:main.cpp:(.text$_ZN8otl_conn6commitEv[_ZN8otl_conn6commitEv]+0x23): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SQLEndTran'
  collect2: error: ld returned 1 exit status
  makefile:14: recipe for target 'main' failed
  make: *** [main] Error 1
  
  18:42:35 Build Finished (took 1s.993ms)
  </code></pre>
  
  <p>What is wrong with my makefile? What should I do?</p>
  
  <p>edit: I updated based on @Jonathan Wakely's answer:</p>
  
  <pre><code>LIBB = C:\temp\lib
  LIBFILE = msodbcsql11
  LDFLAGS += -L$(LIBB) -l$(LIBFILE)
  </code></pre>
  
  <p>I'm still getting the same errors</p>
  
  <p>Update: Updated makefile as suggested by @Jonathan Wakely, but still getting errors. BTW, I changed the file format of libmsodbcsql11.lib to libmsodbcsql11.a because the compiler can't detect the .lib version</p>
  
  <pre><code>CC=g++
  LDFLAGS=
  CFLAGS=-c -Wall
  SOURCES=main.cpp
  LIBB = C:\temp\lib
  LIBFILE = msodbcsql11
  LIBINCL = C:\temp\include
  CFLAGS += -I$(LIBINCL)
  OBJECTS=$(SOURCES:.cpp=.o)
  LDFLAGS += -L$(LIBB) -l$(LIBFILE)
  EXECUTABLE=main
  
  all: $(SOURCES) $(EXECUTABLE)
  
  $(EXECUTABLE): $(OBJECTS)
      $(CC) $(OBJECTS) $(LDFLAGS) -o $@
  
  .cpp.o:
      $(CC) $(CFLAGS) $&lt; -o $@
  </code></pre>