g ++命令行宏定义字节流 [英] g++ command line macro define byte stream

查看:123
本文介绍了g ++命令行宏定义字节流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找通过gcc/g ++命令行通过选项-D将字节流定义为宏的解决方案,例如-Dxxx = byte_stream.

I'm looking for the solution to define a byte stream as a macro from gcc/g++ command line via option -D, e.g. -Dxxx=byte_stream.

下面是代码段,

#ifndef MAGIC_BYTES
#define MAGIC_BYTES "\x01\x02\x00\x00\xa0\xb0"
#endif

我希望每次都可以重新编译代码而不用编辑源代码,而是使用-DMAGIC_BYTES = xxx来定义字节流.

I wish every time I can recompile the code without editing the source but using -DMAGIC_BYTES=xxx to define the byte stream.

我知道编辑源代码可能是解决方案,但是只是想知道如何从命令行定义这样的字节流.

I know to edit the source could be the solution, but just wonder how to define such byte stream from command line.

更新

针对此问题,我在下面放置了简单的代码,

I put the simple code below for this issue,

/* When
 * compile: gcc -o macro ./macro.c
 * output: 0x1, 0x2, 0x3, 0x4, 0x5,
 *
 * compile: gcc -o macro -DMAGIC_BYTES=\"\xa1\xa2\xa3\xa4\xa5\" ./macro.c
 * output: 0x78, 0x61, 0x31, 0x78, 0x61, 
 * but I expect 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
 */
#include <stdio.h>

#ifndef MAGIC_BYTES
#define MAGIC_BYTES "\x01\x02\x03\x04\x05"
#endif

int main()
{
    char buf[] = { MAGIC_BYTES };
    for (int i = 0; i < 5; ++i)
        printf("%#x, ", buf[i]);
    printf("\n");
    return 0;
}

推荐答案

首先,这很大程度上取决于您的环境以及所使用的shell.对于/bin/sh,您可以尝试类似

First this depends a lot from your environment, and the shell that you are using. For /bin/sh you could try something like

-DMAGIC_BYTES='"\x01\x02"'

即使用''转义整个字符串.

that is escape the whole string with ''.

这篇关于g ++命令行宏定义字节流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆