title: Interactive Brokers C++ Error: error: 'min' was not declared in this scope
body: <p>I am trying to setup the <a href="https://www.interactivebrokers.com/en/index.php?f=5041" rel="nofollow noreferrer">Interactive Brokers API</a> on Ubuntu (18.04). I have installed both the IB Gateway, which is used for communicating with exchanges, as well as other API software for developing trading algorithms in Java, C++, C# and Python. (Which you can find <a href="http://interactivebrokers.github.io/" rel="nofollow noreferrer">here</a>). The API is written  in both Java and C++, and as stated prior it offers support for both. However when attempting to compile an example from <em>their</em> source code there is an error in the EReader.cpp file. I have dealt with several other C++ errors in their code however this one I cannot figure out. Here is the code:</p>
  
  <pre><code>#include "StdAfx.h"
  #include "shared_ptr.h"
  #include "Contract.h"
  #include "EDecoder.h"
  #include "EMutex.h"
  #include "EReader.h"
  #include "EClientSocket.h"
  #include "EPosixClientSocketPlatform.h"
  #include "EReaderSignal.h"
  #include "EMessage.h"
  #include "DefaultEWrapper.h"
  
  #define IN_BUF_SIZE_DEFAULT 8192
  
  static DefaultEWrapper defaultWrapper;
  
  EReader::EReader(EClientSocket *clientSocket, EReaderSignal *signal)
  : processMsgsDecoder_(clientSocket-&gt;EClient::serverVersion(), clientSocket-&gt;getWrapper(), clientSocket) {
      m_isAlive = true;
      m_pClientSocket = clientSocket;       
      m_pEReaderSignal = signal;
      m_needsWriteSelect = false;
      m_nMaxBufSize = IN_BUF_SIZE_DEFAULT;
      m_buf.reserve(IN_BUF_SIZE_DEFAULT);
  }
  
  EReader::~EReader(void) {
  m_isAlive = false;
  
  #if defined(IB_WIN32)
  WaitForSingleObject(m_hReadThread, INFINITE);
  #endif
  }
  
  void EReader::checkClient() {
  m_needsWriteSelect = !m_pClientSocket-&gt;getTransport()- 
  isOutBufferEmpty();
  }
  
  void EReader::start() {
  #if defined(IB_POSIX)
  pthread_t thread;
  pthread_attr_t attr;
  pthread_attr_init(&amp;attr);
  pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_DETACHED);
  pthread_create( &amp;thread, &amp;attr, readToQueueThread, this );
  pthread_attr_destroy(&amp;attr);
  #elif defined(IB_WIN32)
  m_hReadThread = CreateThread(0, 0, readToQueueThread, this, 0, 0);
  #else
  #   error "Not implemented on this platform"
  #endif
  }
  
  #if defined(IB_POSIX)
  void * EReader::readToQueueThread(void * lpParam)
  #elif defined(IB_WIN32)
  DWORD WINAPI EReader::readToQueueThread(LPVOID lpParam)
  #else
  #   error "Not implemented on this platform"
  #endif
  {
  EReader *pThis = reinterpret_cast&lt;EReader *&gt;(lpParam);
  
  pThis-&gt;readToQueue();
  return 0;
  }
  
  void EReader::readToQueue() {
  EMessage *msg = 0;
  
  while (m_isAlive) {
      if (m_buf.size() == 0 &amp;&amp; !processNonBlockingSelect() &amp;&amp; m_pClientSocket-&gt;isSocketOK())
          continue;
  
      if (!putMessageToQueue())
          break;
  }
  
  m_pClientSocket-&gt;handleSocketError();
  m_pEReaderSignal-&gt;issueSignal(); //letting client know that socket was closed
  }
  
  bool EReader::putMessageToQueue() {
  EMessage *msg = 0;
  
  if (m_pClientSocket-&gt;isSocketOK())
      msg = readSingleMsg();
  
  if (msg == 0)
      return false;
  
  m_csMsgQueue.Enter();
  m_msgQueue.push_back(ibapi::shared_ptr&lt;EMessage&gt;(msg));
  m_csMsgQueue.Leave();
  m_pEReaderSignal-&gt;issueSignal();
  
  return true;
  }
  
  bool EReader::processNonBlockingSelect() {
  fd_set readSet, writeSet, errorSet;
  struct timeval tval;
  
  tval.tv_usec = 100 * 1000; //100 ms
  tval.tv_sec = 0;
  
  if( m_pClientSocket-&gt;fd() &gt;= 0 ) {
  
      FD_ZERO( &amp;readSet);
      errorSet = writeSet = readSet;
  
      FD_SET( m_pClientSocket-&gt;fd(), &amp;readSet);
  
      if (m_needsWriteSelect)
          FD_SET( m_pClientSocket-&gt;fd(), &amp;writeSet);
  
      FD_SET( m_pClientSocket-&gt;fd(), &amp;errorSet);
  
      int ret = select( m_pClientSocket-&gt;fd() + 1, &amp;readSet, &amp;writeSet, &amp;errorSet, &amp;tval);
  
      if( ret == 0) { // timeout
          return false;
      }
  
      if( ret &lt; 0) {  // error
          m_pClientSocket-&gt;eDisconnect();
          return false;
      }
  
      if( m_pClientSocket-&gt;fd() &lt; 0)
          return false;
  
      if( FD_ISSET( m_pClientSocket-&gt;fd(), &amp;errorSet)) {
          // error on socket
          m_pClientSocket-&gt;onError();
      }
  
      if( m_pClientSocket-&gt;fd() &lt; 0)
          return false;
  
      if( FD_ISSET( m_pClientSocket-&gt;fd(), &amp;writeSet)) {
          // socket is ready for writing
          onSend();
      }
  
      if( m_pClientSocket-&gt;fd() &lt; 0)
          return false;
  
      if( FD_ISSET( m_pClientSocket-&gt;fd(), &amp;readSet)) {
          // socket is ready for reading
          onReceive();
      }
  
      return true;
  }
  
  return false;
  }
  
  void EReader::onSend() {
  m_pEReaderSignal-&gt;issueSignal();
  }
  
  void EReader::onReceive() {
  int nOffset = m_buf.size();
  
  m_buf.resize(m_nMaxBufSize);
  
  int nRes = m_pClientSocket-&gt;receive(m_buf.data() + nOffset, m_buf.size() - nOffset);
  
  if (nRes &lt;= 0)
      return;
  
  m_buf.resize(nRes + nOffset);   
  }
  
  bool EReader::bufferedRead(char *buf, int size) {
  while (size &gt; 0) {
      while (m_buf.size() &lt; size &amp;&amp; m_buf.size() &lt; m_nMaxBufSize)
          if (!processNonBlockingSelect() &amp;&amp; !m_pClientSocket-&gt;isSocketOK())
              return false;
  
      int nBytes = (std::min&lt;unsigned int&gt;)(m_nMaxBufSize, size);
  
  
  
      std::copy(m_buf.begin(), m_buf.begin() + nBytes, buf);
      std::copy(m_buf.begin() + nBytes, m_buf.end(), m_buf.begin());
      m_buf.resize(m_buf.size() - nBytes);
  
      size -= nBytes;
      buf += nBytes;
  }
  
  return true;
  }
  
  EMessage * EReader::readSingleMsg() {
  if (m_pClientSocket-&gt;usingV100Plus()) {
      int msgSize;
  
      if (!bufferedRead((char *)&amp;msgSize, sizeof(msgSize)))
          return 0;
  
      msgSize = htonl(msgSize);
  
      if (msgSize &lt;= 0 || msgSize &gt; MAX_MSG_LEN)
          return 0;
  
      std::vector&lt;char&gt; buf = std::vector&lt;char&gt;(msgSize);
  
      if (!bufferedRead(buf.data(), buf.size()))
          return 0;
  
      return new EMessage(buf);
  }
  else {
      const char *pBegin = 0;
      const char *pEnd = 0;
      int msgSize = 0;
  
      while (msgSize == 0)
      {
          if (m_buf.size() &gt;= m_nMaxBufSize * 3/4) 
              m_nMaxBufSize *= 2;
  
          if (!processNonBlockingSelect() &amp;&amp; !m_pClientSocket-&gt;isSocketOK())
              return 0;
  
          pBegin = m_buf.data();
          pEnd = pBegin + m_buf.size();
          msgSize = EDecoder(m_pClientSocket-&gt;EClient::serverVersion(), &amp;defaultWrapper).parseAndProcessMsg(pBegin, pEnd);
      }
  
      std::vector&lt;char&gt; msgData(msgSize);
  
      if (!bufferedRead(msgData.data(), msgSize))
          return 0;
  
      if (m_buf.size() &lt; IN_BUF_SIZE_DEFAULT &amp;&amp; m_buf.capacity() &gt; IN_BUF_SIZE_DEFAULT)
      {
          m_buf.resize(m_nMaxBufSize = IN_BUF_SIZE_DEFAULT);
          m_buf.shrink_to_fit();
      }
  
      EMessage * msg = new EMessage(msgData);
  
      return msg;
  }
  }
  
  ibapi::shared_ptr&lt;EMessage&gt; EReader::getMsg(void) {
  m_csMsgQueue.Enter();
  
  if (m_msgQueue.size() == 0) {
      m_csMsgQueue.Leave();
  
      return ibapi::shared_ptr&lt;EMessage&gt;();
  }
  
  ibapi::shared_ptr&lt;EMessage&gt; msg = m_msgQueue.front();
  
  m_msgQueue.pop_front();
  m_csMsgQueue.Leave();
  
  return msg;
  }
  
  
  void EReader::processMsgs(void) {
  m_pClientSocket-&gt;onSend();
  checkClient();
  
  ibapi::shared_ptr&lt;EMessage&gt; msg = getMsg();
  
  if (!msg.get())
      return;
  
  const char *pBegin = msg-&gt;begin();
  
  while (processMsgsDecoder_.parseAndProcessMsg(pBegin, msg-&gt;end()) &gt; 0) 
  {
      msg = getMsg();
  
      if (!msg.get())
          break;
  
      pBegin = msg-&gt;begin();
  } 
  }
  </code></pre>
  
  <p>The error I get it is the following:
  <a href="https://i.stack.imgur.com/hjGAI.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hjGAI.jpg" alt="error"></a></p>
  
  <pre><code>error: 'min' was not declared in this scope  int nBytes = 
  min(m_nMaxBuffSize, size);
  </code></pre>
  
  <p>I have had to do other things such as editing other source code and makefiles, I am stuck here. Any insight would be appreciated. </p>