title: Change a string macro at compilation time
body: <p>I'm developing an unique client that has to work on different machines. In every machine the server is running in a different IP address, but this address is known.</p>
  
  <p>I don't want to tell the client which is the IP every time I run it, so I though about tell it in compilation time.</p>
  
  <p>The problem is that when compiling with <code>g++ -DHOSTNAME=127.0.0.1</code> (also tried with double quotes) the compiler is saying: </p>
  
  <pre><code>error: too many decimal points in number
  ./include/Client.h:18:25: note: in expansion of macro ‘HOSTNAME’
  </code></pre>
  
  <p>I tried it using localhost, too.</p>
  
  <pre><code>error: ‘localhost’ was not declared in this scope
  ./include/Client.h:18:25: note: in expansion of macro ‘HOSTNAME’
  </code></pre>
  
  <p>Also tried using some things found on the internet.</p>
  
  <pre><code>#define XSTR(x) STR(x)
  #define STR(x)
  </code></pre>
  
  <p>compile error:</p>
  
  <pre><code>./src/BSCClient.cpp:15:45: note: #pragma message: HOSTNAME: 
  #pragma message("HOSTNAME: " XSTR(HOSTNAME))
  
  ./src/BSCClient.cpp:16:39: error: too few arguments to function ‘hostent* gethostbyname(const char*)’
    server = gethostbyname(XSTR(HOSTNAME));
  </code></pre>
  
  <p>At this point I'm thinking that maybe macros isn't the proper way to handle this, but I don't figure out how to do it.</p>
  
  <p>If someone has any reference about it I will be thankful.</p>
  
  <p>EDIT: 
  These are the codes.</p>
  
  <p>Client.h:</p>
  
  <pre><code>#ifndef __CLIENT_HH__
  #define __CLIENT_HH__
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  #define HOSTNAME 127.0.0.1
  #define MAX_MESSAGE_LENGTH 10 
  
  class Client {
  private:
      string client_name;
      int sockfd, portno;
      struct sockaddr_in serv_addr;
      struct hostent *server;
      error(const char *msg);
  
  public:
  
      BSCClient (string name, int port);
      void identifyme();
      void sendData (string data);
      string recvData ();
  
      void closeSocket();
  };
  
  #endif
  </code></pre>
  
  <p>Client.cpp</p>
  
  <pre><code>#include "BSCClient.h"
  
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  
  void BSCClient::error(const char *msg)
  {
      perror(msg);
      exit(0);
  }
  
  Client::Client(string name, int port)
  {
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      portno = port;
      client_name = name;
  
      if (sockfd &lt; 0) 
          error("ERROR opening socket");
  
      server = gethostbyname(HOSTNAME);
  
      if (server == NULL) {
          fprintf(stderr,"ERROR, no such host\n");
          exit(0);
      }
  
      bzero((char *) &amp;serv_addr, sizeof(serv_addr));
      serv_addr.sin_family = AF_INET;
      bcopy((char *)server-&gt;h_addr, 
           (char *)&amp;serv_addr.sin_addr.s_addr,
           server-&gt;h_length);
      serv_addr.sin_port = htons(portno);
      if (connect(sockfd, (struct sockaddr *) &amp;serv_addr, sizeof(serv_addr)) &lt; 0) 
          error("ERROR connecting");
  
      sendData(client_name);
  }
  
  void Client::identifyme() {
      FILE *fp;
      fp = popen("id -gn", "r");
  
      char text[6];
      fscanf(fp, "%s", text);
      pclose(fp);
      string data(text);
      sendData(data);
  }
  
  void Client::sendData (string data) {
      const char *sdata = data.c_str();
      int n;
      n = write(sockfd, sdata, strlen(sdata));
      if (n &lt; 0) 
           error("ERROR writing to socket");
  }
  
  string Client::recvData () {
          int n;
          int bytes;
          char *longitud = new char[MAX_MESSAGE_LENGTH+1];
          n = read(sockfd, longitud, MAX_MESSAGE_LENGTH);
          if (n &lt; 0) {
                  error("ERROR recieving size of output");
          }
          bytes=atoi(longitud);
          //Para forzar el fin del string (ya que al imprimir el string hay veces que muestra caracteres de más)
          longitud[MAX_MESSAGE_LENGTH]='\0';
          char *data = new char[bytes];
          n = read(sockfd, data, bytes);
          if (n &lt; 0)
                  error("ERROR reading output");
          string ret(data);
          return ret;
  } 
  
  void Client::closeSocket() {
      close(sockfd);
  }
  </code></pre>