title: Undefined reference to `mysql_init'
body: <p>I am trying to compile my program on my new server, but it's not working for me at the moment.</p>
  
  <p>Error log is:</p>
  
  <pre><code>rasmus@web01:~/c++$ make test
  g++ `mysql_config --cflags --libs` main.cpp logger.cpp cpulogger.cpp -o test
  /tmp/ccPaMZUy.o: In function `CPULogger':
  /home/rasmus/c++/cpulogger.cpp:7: undefined reference to `mysql_init'
  /home/rasmus/c++/cpulogger.cpp:8: undefined reference to `mysql_real_connect'
  /home/rasmus/c++/cpulogger.cpp:10: undefined reference to `mysql_get_client_info'
  /tmp/ccPaMZUy.o: In function `~CPULogger':
  /home/rasmus/c++/cpulogger.cpp:16: undefined reference to `mysql_close'
  collect2: ld returned 1 exit status
  make: *** [all] Error 1
  </code></pre>
  
  <p>As you can see I am compiling against MySQL - I have checked that mysql.h is present in include paths.</p>
  
  <p>What am I missing?</p>
  
  <p>cpulogger.cpp has #include "cpulogger.h" at the top, then cpulogger.h has this:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;mysql/mysql.h&gt;
  </code></pre>
  
  <p>The compiler does not complain about missing mysql/mysql.h, so that part must work?</p>
  
  <h3>Output of mysql_config:</h3>
  
  <pre><code>rasmus@web01:~/c++$ mysql_config --cflags --libs
  -I/usr/include/mysql -DBIG_JOINS=1  -fno-strict-aliasing  -g
  -L/usr/lib -lmysqlclient -lpthread -lz -lm -lrt -ldl
  </code></pre>
  
  <h3>Makefile:</h3>
  
  <pre><code>all:
      g++ `mysql_config --cflags --libs` main.cpp logger.cpp cpulogger.cpp -o test
  
  
  test: all
      ./test
  </code></pre>
  
  <p>It's a fresh Ubuntu server installation with a mysql-server install on it.</p>
  
  <p><strong>[solved]:</strong></p>
  
  <p>Putting linker libraries at the end of the compiler commands works.</p>
  
  <pre><code>all:
      g++ main.cpp logger.cpp cpulogger.cpp -o test `mysql_config --cflags --libs`
  </code></pre>
  
  <p>See answer below for explanation.</p>