在编译时更改字符串宏

时间:2014-10-06 09:10:47

标签: c++ string c++11 macros compilation

我正在开发一个必须在不同机器上工作的独特客户端。在每台计算机上,服务器都在不同的IP地址中运行,但这个地址是已知的。

我不想在每次运行时告诉客户端哪个是IP,所以我想在编译时告诉它。

问题在于,在使用g++ -DHOSTNAME=127.0.0.1进行编译时(也尝试使用双引号),编译器会说:

error: too many decimal points in number
./include/Client.h:18:25: note: in expansion of macro ‘HOSTNAME’

我也尝试使用localhost。

error: ‘localhost’ was not declared in this scope
./include/Client.h:18:25: note: in expansion of macro ‘HOSTNAME’

还尝试使用互联网上的一些东西。

#define XSTR(x) STR(x)
#define STR(x)

编译错误:

./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));

此时我认为宏可能不是处理此问题的正确方法,但我不知道该怎么做。

如果有人对此有任何提及,我将非常感激。

编辑: 这些是代码。

Client.h:

#ifndef __CLIENT_HH__
#define __CLIENT_HH__

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#include <string>
#include <iostream>

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

Client.cpp

#include "BSCClient.h"

#include <stdlib.h>
#include <time.h>

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 < 0) 
        error("ERROR opening socket");

    server = gethostbyname(HOSTNAME);

    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }

    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 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 < 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 < 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 < 0)
                error("ERROR reading output");
        string ret(data);
        return ret;
} 

void Client::closeSocket() {
    close(sockfd);
}

2 个答案:

答案 0 :(得分:5)

你必须逃避双引号:

g++ -DHOSTNAME=\"127.0.0.1\"

否则,引号只是对您的 shell 127.0.0.1是您要赋予-DHOSTNAME的值,如果值包含空格,则该值很有用例如:

g++ -DMAGIC_NUMBER="150 / 5"

(那里,MAGIC_NUMBER将被150 / 5替换为没有引号)

如果您希望引号成为宏的一部分(如#define HOSTNAME "127.0.0.1"中所示),您必须向您的shell说明它们是您为-DHOSTNAME赋予的值的一部分,这是完成的通过逃避他们。

修改

另外,正如Angew指出的那样,你误用了XSTR技巧。除了我的答案之外,这是解决问题的另一种方法。

它肯定是这样的:

#define XSTR(x) STR(x)
#define STR(x) #x

这样你就不必逃避引号。

这两个宏将文字127.0.0.1更改为"127.0.0.1"。在XSTR宏将HOSTNAME宏转换为127.0.0.1之前,STR宏可以将"127.0.0.1"扩展为STR。如果您直接使用"HOSTNAME"宏,则最终会使用"127.0.0.1"而不是{{1}}。

我认为我更倾向于使用转义解决方案来使用代码中涉及两个宏的技巧,但这也有效。

答案 1 :(得分:0)

您想要将其硬编码到可执行文件中似乎很奇怪。使用getenv("MY_SERVER_ADDR")之类的东西应该更灵活,只需在运行服务器之前设置该环境变量即可。或者当然你可以做更典型的事情并将其作为命令行参数,但有些事情告诉我你已经决定不这样做。

如果你在Linux上,一个稍微怪想的想法是将IP地址写入文本文件并使用ldobjcopy创建一个ELF目标文件;然后,如果您真的想要“硬编码”它,可以将其作为共享对象加载到您的应用程序中,甚至是静态加载。但我不确定为什么这会比前面提到的选项更好。