Is std::string header only in Visual Studio? - c++

It looks like std::string is a header only file at Community/VC/Tools/MSVC/?/include/xstring, and all generated code should be included inside a build target.
If I'm correct, how does Microsoft guarantee that the next Visual Studio version doesn't change xstring and the std::string internal structure?
Update 1:
I got many downvotes for this question so let me explain why I decided to ask it.
I'm faced with strange crash, and I can not understand why this happen.
I use latest Qt 5.13.0 (MSVC2017_x64) and also I have some external libraries compiled with Visual Studio 2017. All have /MDd, I checked this with dumpbin util.
When I try to run any code that invokes Qt library and std::string, I'm getting wrong result (and crash at the end).
Here is very simple example:
#include <QApplication.h>
int main(int argc, char** argv) {
QString s1("Test");
std::string s2 = s1.toStdString(); // here we have s2 variable with wrong internal structure
return 0;
}
My idea was that QtCore DLL library has std::string with internal structure not compatible with std::string from Visual Studio 2017. But Qt was created with Visual Studio 2017 (maybe not same as my current Visual Studio, because there was several minor releases), so I decided to ask here if they are compatible or not.
Update 2:
Problem was in _ITERATOR_DEBUG_LEVEL. Looks like Qt was compiled with level 2 and all my external libraries and application were compiled with level 0.
This option affects internal structure of many C++ standard library classes and introduce such side effects. So when we are inside toStdString() and create std::string, we have level 2 and one internal structure. When we are in application code, we have level 0 and another internal structure. We assign object with one internal structure to object with another.
Anyway now I have better understanding of some internals.

how does Microsoft guarantee that the next Visual Studio version doesn't change xstring and the std::string internal structure?
Because they make a decision to guarantee that, or to not guarantee that.
For example, Visual Studio 2015 to 2019 are binary compatible.
That's a decision that was made, to do that. The result, if what you say is true, is that some of the implementation specifics of std::string on that platform are frozen. This is not unusual for libraries. libstdc++'s std::list::size was non-compliant to C++11 for many years, because they could not add a needed member variable without breaking binary compatibility.
In short, this is basically a project management decision, and if they ever change the header in a way that breaks things, they will tell you that binary compatibility has been broken and you need to rebuild and relink things accordingly.
As for your Qt issue, it does smell like a binary compatibility issue. But you say that both Qt and your application have been built in Visual Studio 2017 with /MDd, which would appear to rule that out. I would ask the Qt community for further help, possibly with slightly more information about your environment and about where you obtained Qt. Also ensure that you're using the version of Qt that's intended — perhaps there are multiple installations? Which one's on your include path?

Is std::string header only in Visual Studio?
Not entirely, it also depends on parts of the Standard C++ library that are implemented in Microsoft's Visual C++ Runtime. Building a binary with MSVC requires the VC++ runtimes to be linked. Only static libraries may be built without linking to a runtime, and then you must be careful to include none of the headers that require the runtime.
Is the std::string header only in Visual Studio?
(I originally read the headline this way.)
std::string is part of the C++ standard.
To use std::string on any platform that supports standard C++, you should use #include <string>. It is a standard header available with pretty much any C++ compiler.
Every compiler or platform may implement the standard in its own way though. For example with MSVC you can see that xstring is how Microsoft implements std::string under the hood. If you include xstring.h directly you are writing code that depends on the version of MSVC that provides that header. That code would not be portable to other compilers.
If I'm correct, how does Microsoft guarantee that the next Visual Studio version doesn't change xstring and the std::string internal structure?
Microsoft does not guarantee that the next Visual Studio version will have the same std::string internal structure. In the past the implementation of the standard library has changed with every VC++ runtime release, which is why Windows users end up having dozens of VC++ runtime versions installed in their Add/Remove programs list.
Thankfully Microsoft has given us a guarantee that Visual Studio 2015, 2017, and 2019 all use a binary-compatible C++ runtime. This means that binaries built using the standard library provided in Visual Studio 2015 are compatible with binaries built using 2017 and 2019 too. There is no guarantee (yet) that a future version of Visual Studio will not change the VC++ runtime implemenation again.

Related

Are Visual C++ 2013 binaries compatible with Visual C++ 2017 binaries?

In one of our C++ solutions, we use 3rd part libraries. These libraries are compatible to VS 2013. Now we are migrating our solution to VS 2017 and found that some of the 3rd party libraries do not have VS 2017 compatible versions.
So we tried to use some of the VS2013 compatible libraries in VS20173 and the tried API calls work fine.
Can I assume that the libraries work with VS 2017 executable without any issues?
In general - no. AFAIK, VC++2015 (aka toolset v140) and VC++2017 (aka toolset v141) are stated to be binary compatible. No such statement were made wrt VC++2013, and I believe there are breaking changes (like sizeof(list) etc).
It might work, but could lead to hard-to-debug problem
Microsoft statement:
"A more-severe kind of change, the breaking change can affect binary compatibility, but these kinds of binary compatibility breaks only occur between major versions of Visual Studio. For example, between Visual Studio 2013 and Visual Studio 2015."
see https://learn.microsoft.com/en-us/cpp/porting/visual-cpp-change-history-2003-2015
Nothing is guaranteed but binary compatibility of Visual C++ compilers is generally better than officially announced. Just make sure you do not create/destroy object across different runtimes, propagate exceptions and do not pass STL related objects as parameters.
If the third party libraries expose C style interfaces and they are compiled as DLLs the task is even easier. So you should review those interfaces and verify how much they vary from the general interoperability guidelines.

Visual Studio 2017 C++ Shared Items Project does not recognize STL

I am experimenting with the Shared Items projects in VS2017 (Community Edition). (The goal is to create a non-visual library running on Windows and Linux.)
I added a C++ class, and the most standard of include directives for some reason fail. E.g. #include <iostream> results in an error. There are some headers in the automatic completion list but they look like proprietary Microsoft stuff.
I had the impression this stuff should work out of the box. For some reason, the Project properties also don't have much going about them, no place to tweak the libraries.
Thanks to #RichardCritten, I found that indeed some components were missing (Clang, I believe, although I installed some others). They were not called "standard components" though.
Bad, bad Visual Studio.

Use std::initializer_list in Visual C++ Compiler November 2012 CTP

I want to use std::initializer_lists in Visual Studio 2012 like a guy in this example does. My operating system is Windows 8 x64.
Therefore I lately installed the Visual C++ Compiler November 2012 CTP and as mentioned by Microsoft, I changed the platform toolset of my project to use that new updated compiler.
But even after doing so, there is neither a std::initializer_list nor a <initializer_list> header available. But the linked website from Microsoft tells me (under the headline "Overview") that initializer lists would be available with that update. I restarted both the IDE and my PC. I am not sure if it could be caused by the fact that I am (sadly) using the German edition of Visual Studio and the compiler update is in English. What am I doing wrong?
Update: Trying to compile the line auto a = { 0 }; which is criticized by IntelliSense the compiler output shows 'Microsoft Visual C++ Compiler Nov 2012 CTP' is for testing purposes only. and then compiler crashes and a error window appears which reads Microsoft (R) C/C++ Compiler Driver has stopped working. Without any new syntax, everything compiles and works fine with the new compiler selected.
(I work for Microsoft and with Dinkumware to maintain VC's Standard Library implementation.)
[danijar]
I am not sure if it could be caused by the fact that I am (sadly) using the German edition of Visual Studio and the compiler update is in English.
Unfortunately, the English-only CTP does not support German VS.
The "compiler driver" cl.exe is what invokes the compiler front-end c1xx.dll, the compiler back-end c2.dll, and the linker link.exe. It is very unusual for the compiler driver to crash. I speculate that it's attempting to display one of the error messages that were added by the CTP, but since the CTP didn't update the German resources, the compiler driver can't load the error message and proceeds to crash.
Note that this is different from an Internal Compiler Error in the front-end or back-end, or a normal compiler error that happens to be incorrectly emitted. (Many ICEs and bogus errors have been fixed after the CTP was released.)
But even after doing so, there is neither a std::initializer_list nor a <initializer_list> header available.
The CTP installed <initializer_list> in a special location. (It was actually written by the compiler team.)
On the command line, the incantations to use the CTP and put <initializer_list> on the include path are (assuming default locations):
"C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat" x86
set PATH=C:\Program Files (x86)\Microsoft Visual C++ Compiler Nov 2012 CTP\bin;%PATH%
set INCLUDE=C:\Program Files (x86)\Microsoft Visual C++ Compiler Nov 2012 CTP\include;%INCLUDE%
Trying to compile the line auto a = { 0 }; which is criticized by IntelliSense
This was documented - Intellisense was not updated by the CTP, therefore it will not recognize any of the new features.
[rubenvb]
The C++ Standard Library was not updated with the compiler, leaving you without decent <tuple> and <intializer_list> (this includes the omission of the braced init list constructors for all standard containers)
You may be interested to learn that we've updated the standard library to fully support scoped enums and initializer lists. This includes all initializer_list overloads mandated by the current Working Paper (N3485), plus installing <initializer_list> in the usual location along with all other standard headers. (It is also Dinkumware's official copy, although the differences between it and the compiler team's "fake" version were mostly cosmetic.) This stuff will be available in the next public release, whenever and whatever that is. Our next task is to update the standard library with explicit conversion operators and variadic templates, replacing our brittle simulations.
As you have noticed, the November CTP is very limited in usability for at least two reasons:
The compiler has numerous crash-causing bugs, such as the one you discovered.
The C++ Standard Library was not updated with the compiler, leaving you without decent <tuple> and <intializer_list> (this includes the omission of the braced init list constructors for all standard containers)
Also: the linked example is very ugly code. If you want to use this feature, use a compiler like GCC or Clang that supports this syntax. They are both available for Windows. Hacking around a half-implemented language feature by writing extra code is just stupid.

Visual Studio store hidden data in the compiled files?

I want to know if Visual Studio store hidden data in compiled files ( or other files ).
I am talking about information data about the fact that the program is written with Visual Studio.
Visual Studio itself does not watermark your programs in this way, but it is often detectable that you used Microsoft's toolchain simply because of incidentals, i.e. what order methods are compiled into the file, what implementation of exceptions is used (MSVC++ uses a specific exception code on top of NT's SEH in order to implement C++ exceptions; MinGW, for example, uses a completely different system), and what decorations are applied to function names. These bits of information are how tools like PEID operate.
The MS c++ compiler/linker will add some encoded data between the DOS stub and the PE header, it starts with the letters Rich and contains mostly version info, see this page for more info
if I correctly understand your question:
it's impossible to distinguish if you build your program using Visual C++ Express Edition (free) or other non-free editions if you don't use specific options not available in Express Edition. some comparison of VS2008 editions can be found here

How can I build VS 2010's C Runtime Library?

I need to modify the C runtime which ships with VS2010 because the 2010 CRT relies on functions released in Windows XP SP2, and I need to be able to deploy to Windows 2000.
Specifically, I need to remove any and all calls to EncodePointer and DecodePointer.
The source for the C runtime is included in C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\crt\src, so it seems like it should be possible to build the runtime after slightly modifying the source.
Oh, I don't need to be able to build the dynamic versions of the runtime -- static versions only. (I cannot rely on the user installing the CRT on their system either).
With VS2008 and earlier, there was a tutorial in MSDN describing how to build the CRT, but I can't seem to find it for 2010.
Is such a thing possible?
Here's an MSDN link. It looks like you have to do it yourself in VS2010.
You can use the following compiler and linker options to rebuild the MFC, CRT, and ATL libraries. Starting in Visual C++ 2010, scripts for rebuilding these libraries are no longer shipped.
If it is a option, I would consider using the VC++ 2008 toolset within VS2010 instead of building a custom CRT. The procedure is explained here.
"Oh, I don't need to be able to build the dynamic versions of the runtime -- static versions only."
Since you only need static linking, you can try this trick to provide implementations of EncodePointer and DecodePointer.