c++ - 编译期间 MSVC 中的字符串长度限制?

标签 c++ gcc string-length

我正在将 C++ 代码从 Linux 转换到 Windows(使用 Visual Studio 2013)。但是 MSVC 对字符串有长度限制(大约 2048 字节?),而 GCC 则没有。我的问题是,有一个配置文件包含几个巨大的字符串,它在 GCC 下运行良好。但 MSVC 报告编译错误为

error C2026: string too big, trailing characters truncated.

字符串非常简单,CONFIGSTRING 很大。

const std::string CONFIGSTRING="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

这个问题有什么解决办法吗?我可以在Windows下使用GCC将配置文件单独编译为目标文件并将其链接到其他文件吗?如果可能的话,任何人都可以简单地告诉我该怎么做?

最佳答案

根据 MSDN 文档,这应该有效:

const std::string str =
  "xxxx" // Max 2048 bytes
  "xxxx" // Max 2048 bytes
         // ... and so on (up to 65535 bytes)
  ;

如果这仍然不够,那么请执行以下操作:

std::string str;
str = "part1";
str += "part2";
str += "part3"; // And so on.

Can I separately compile the config file to object file using GCC under windows and link it to other files?

不,他们使用不同的 C++ 标准库。

关于c++ - 编译期间 MSVC 中的字符串长度限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31460913/

相关文章:

c++ - 指针分配说明

c - 严格别名仅适用于第一个元素吗?

c++ - 如何在 C++ 中从用户那里获取具有预定义字符串长度的字符串输入?

c++ - PIC 编程 - 将多个按钮的值获取到一个端口

c++ - 赋值顺序

c++ - 构建 Boost 动态可链接会导致链接错误?

c - 为什么 strlen() 函数给出了错误的值

c++ - 维护ABI : adding constructor to struct

linux - 说服 gcc 忽略系统库以支持本地安装的库

java - Kotlin 字符串最大长度? (带有长字符串的 Kotlin 文件无法编译)