String length limit in MSVC during compile?

2019-09-10 09:19发布

问题:

I am converting C++ codes from Linux to windows (using Visual Studio 2013). But MSVC has length limit on string (around 2048 bytes?), the GCC doesn't instead. My problem is that, there is a config file containing several huge string, it works well under GCC. But MSVC reports compile error as

error C2026: string too big, trailing characters truncated.

The string is quite simple, CONFIGSTRING is huge.

const std::string CONFIGSTRING="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

Any solution for this problem? Can I separately compile the config file to object file using GCC under windows and link it to other files? If possible, anyone can briefly show me how to do it?

回答1:

According to the MSDN docs, this should work:

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

If this is still not sufficient, then do:

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?

No, they are using different C++ standard libraries.