如何将重复或相乘的字符串文字合并到GCC中 [英] how to merge duplicated or multiplicated string literals into one in GCC

查看:372
本文介绍了如何将重复或相乘的字符串文字合并到GCC中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了像\\ b
$ b

  char * a1 =apalaxia这样的倍增字符串文字。 
char * t =apalaxia;
char * zzzz =apalaxia;

跨几个编译单元/ .o文件

我强烈需要它们在一个运行时合并
地址(因此对于所有它们都是一个char *值)

如何在GCC中执行此操作在c + +模式)?我尝试了-fmerge-constants
和-fmerge-all-constants,但是id不合并

解决方案

-constants或-fmerge-all-constants保证字符串文字将被合并。两者都表示他们试图合并相同的常量,但不保证他们会这样做。

另外,C ++标准的第2.13.5节指出:是否所有字符串文字都是不同的(即存储在非重叠对象中),并且对字符串文字的连续评估是否产生相同的或未指定的不同对象。



所以不幸的是,我不认为有一种方法可以获得保证的单个位置,而不必明确声明每个单独的副本并将其导出到使用它的翻译单元。


i got multiplicated string literals like

char* a1 = "apalaxia";
char* t = "apalaxia";
char* zzzz = "apalaxia";

across couple of compile units/ .o files

I strongly need all them to merge under one runtime adress (thus one char* value for all them)

How to do that in GCC (in c++ mode)? I tried -fmerge-constants and -fmerge-all-constants but id do not merge

解决方案

Neither -fmerge-constants nor -fmerge-all-constants guarantee that string literals will be merged. Both state that they "attempt to merge identical constants", but don't guarantee that they will.

Also, section 2.13.5 of the C++ standard states that "Whether all string literals are distinct (that is, are stored in nonoverlapping objects) and whether successive evaluations of a string-literal yield the same or a different object is unspecified."

So unfortunately, I don't think there's a way to get the guaranteed single-location without explicitly declaring a single copy of each and exporting it to the translation units that use it.

这篇关于如何将重复或相乘的字符串文字合并到GCC中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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