Is there a (Microsoft-specific) CPP macro to determine when I'm using the VC9 compiler in Visual Studio 2010 as opposed to Visual Studio 2008? _MSC_VER returns the compiler version, so with VS2010 multi-targeting feature, I'll get the same result as with VS2008.
The reason for wanting to know the difference is that I created a new VS2010 project which contains code removed from a larger project. I just left the VS2008 stuff "as is" since we're moving away from VS2008 "soon" anyway and I didn't want to go through the hassle of creating a vcproj file along with the new vcxproj.
For now, I've just defined my own macro to indicate whether the code is compiled into its own DLL or not; it works just fine, but it would be nice if there were something slightly more elegant.
_MSC_VER returns the compiler version
It sounds like that's what you really do want (or am I misunderstanding?).
If the compiler from VS2008 (which confusingly is also known as VC9 or cl.exe 15.0) is being used, then _MSC_VER will return a value that's greater than or equal to 1500. If the compiler from VS2010 is used (also known as VC10 or cl.exe 16.0), then _MSV_VER will evaluate to 1600.
It seems there is no solution, a custom macro works even if it isn't quite as elegant as I would like.
Related
I start a C++ Project that uses the "Visual Studio 2017 - Windows XP (v141_xp)" Platform Toolset
Set Runtime Library to "Multi-Threaded (/MT)"
Change "Debug" to "Release"
Add #include "<Wbemidl.h>" to the source.
CTRL F5 (aka Build)
Now, I am aware that platform set is deprecated but, either way, the default x64 build has no issues. However, when I change to "x86" from "x64" (as that's what I want), it then produces numerous errors like the following:
C++ 'default argument': cannot convert from 'const wchar_t' to 'BSTR'
You might ask what am I trying to do? Maybe some of my code is bad? You should not convert wchar_t to BSTR as it is not defined what will be the result?
But these aren;t my concerns, here. I didn't add any code – just the single header include (I left the "Hello World" part).
I didn't explicitly convert anything as the error is in the Microsoft system headers, not in my Code. I didn't add anything to do with QT nor do I need or want QT so that's not the issue.
I just for now want to compile my "Hello World" Program using the <Wbemidl.h> header. (Of course, I don't need that header to compile it but, if I can compile it without issues, I most likely then can use the library without issues.) I wanted to make this as simple as possible, hence why it just is "Hello World" (a MCVE).
I tried adding the following but without success:
#pragma comment(lib, "Shlwapi.lib")
#pragma comment(lib, "wbemuuid.lib")
My best guess is that I didn't install something and or its just deprecated; the funny thing is "2015 Visual Studio (140_xp)" works.
I just want to compile my C++ "Hello World" Program with the Header "<Wbemidl.h>" , Compiled in "x86", Toolset of "Visual Studio 2017 - Windows XP (v141_xp)" and Runtime Library as "Multi-Threaded (/MT)"
My code (MCVE):
#include <iostream>
#include <Wbemidl.h>
int main()
{
std::cout << "Hello World!\n";
}
Microsofts Code ERROR
Other things I tried:
Strict QT strings tags
Including other stuff
Changing from "Multi-Threaded (/MT)" to "Multi-threaded DLL (/MD)"
Tried making 4 Different projects
Since (IIRC) VS 2019 (and certainly for VS 2022), there is a C++ compiler setting that (by default) forces much stricter adherence to the language standard. That setting ("Conformance Mode") will prevent the conversion (const w_char[] to BSTR) that is being used in the (older) SDK header files.
This option was already present in VS 2017 but was disabled by default; in VS 2015 (which, you say, works without the errors), the option is simply not present. Thus, when you change the toolset from VS 2022 to VS 2017, you are (unwittingly) enabling that "conformance mode" option.
To disable these errors, you need to turn off conformance mode. In your project settings, go to the "C/C++" … "Language" property page and set "Conformance Mode" to "No":
There will very likely still be numerous other warnings but the system headers will, at least, compile without errors.
Note that, when using the newer toolsets, the MSVC compiler uses system header files from a newer SDK; in those headers, the issues have been properly addressed, so conformance mode can be used.
These two trivial lines of code:
const bool equal = (HUGE_VALF == HUGE_VALF);
static_assert(equal == true, "Fatal error");
in a test program work perfectly (no warnings and no errors).
When I copy them in the main project, I get this error on the assert:
error C2057: expected constant expression
Here the equal object is a constant with value = true.
Those lines of code are in both cases inside functions.
The two projects have identical settings (C++11, Visual C++ 2013 compiler, Visual C++ 2019 IDE, /W4, no optimizations).
What could be the reason of this different behaviour from the compiler?
When a newer Visual Studio IDE (like VS 2019) loads an older project for the first time, it asks to retarget the project. If You have the old Visual Studio installed, You can reject this action. Then the compiler from the original VS will be used by the newer IDE.
After loading the solution, in the Solution Explorer next to the project name, You will have the Visual Studio which will be used to compile the project.
If the paranthesis is missing, that means the compiler and standard library from the current Visual Studio is been used.
As mentioned in the comments:
I have no parenthesis after the project name (if I am looking in the right place). However, I realised about the issue in the include file paths: Test Project: 14.24.28314\include\cmath; Main Project: Microsoft Visual Studio 12.0\VC\include\cmath, i.e. a much older version, that internally still relies on the C math.h header. So, same compiler (VS2013), different libraries...
This indicates that You have most probably retargeted the project or created a new project in VS 2019.
To make a project in VS 2019 make use of the older compiler, You need to got to the project Properties -> Configuration Properties -> General -> Platform Toolset and change it to the appropriate Visual Studio.
Also make sure that all configurations (Debug, Release,...) for all machine types (32bit, 64 bit) have this setting the same value.
Because the test project is using VS 2019 - it has all the required definitions - so it works.
So the issue is that most probably, VS 2013 standard library is not C++11 complete and does NOT have a definition for HUGE_VALF.
But it could contain a definition for HUGE_VAL
I use Visual Studio 2012 to edit source code that is targeting Linux. The problem is that syntax highlighting gets it all wrong, because it thinks _WIN32, _MSC_VER, and similar macros are defined. What can I do to get rid of them?
I tried two methods:
added /u command in the property page for C++;
added /U"_WIN32" /U"_MSC_VER" and so on.
but without any effect. The problem is the same in Visual Studio 2013.
What should I do?
These are compiler-predefined macros. You can only #undef them in the code. Use a custom header file, probably.
Anyway, the requirement seems strange to me. Why do you use MSVC if you want to scan the Linux code? If the Linux code uses any GCC extensions (it often does), it can't compile under MSVC at all.
You can edit them in you're project's configuration.
Right click project->properties->C/C++->preprocessor->preprocessor definitions
I create a test project. The definitions only affected when the code be compiled.
I don't like qt creator as IDE and love VS, but I must use exactly mingw compiler. Sad story :'(
Just set up a makefile project - that way you can tell VC what command to run to compile your files.
You have to maintain a makefile in addition to the Visual Studio project, but that's really not too big of a problem since in that case the VS project becomes just a list of the files you want Visual Studio to know about.
Unfortunately, the VS debugger is not useful in this scenario, but all of the IDE's code navigation works fine.
You can't easily replace the C++ compiler in Visual Studio.
But at one time (in the 1990's) I used Visual Studio as simply an editor for Java. And since there are extensions for e.g. the D programming language (well that's the only one I've used) you can certainly, with a lot of work, make the full Visual Studio work with g++ or any other compiler for whatever language, as an additional "language". It can even work with the debugger, if the language implementation is suitable for that.
It's just that nowadays it's much easier to use an IDE that does support the tools you want to use. E.g., for g++ you have Eclipse, Code::Blocks, even old DevC++, etc. Oh yes, and the QT thing.
I have made a sudoku solver using SDL (for GUI) on VCpp 2010 IDE.
The program compiles without errors and runs fine. However, i cannot pass the compiled executable on to some people because they don't have msvc dll on their pc.
I thought i could use devc++ that compiles with GCC but this IDE is kinda buggy and just won't let me include some header files.I also have some problems setting up SDL expansions.
So, is there a way to change VisualStudio compiler to GCC?
I really need to stay with VS because it is easy to use and there is a lot of online support.
Short answer: no, you cannot change cl.exe (MS cc compiler) with gcc. Or mingw. (You can with a compatible compiler, like Intel C compiler)
Long answer: you don't need to to that. Use the static linked runtime library, and you don't have a dependency ms dlls anymore (see this answer: How do I make a fully statically linked .exe with Visual Studio Express 2005?)
Or redistribute the VC++ runtime with your app. It's free (http://www.microsoft.com/en-us/download/details.aspx?id=26999)
You don't need to change compiler - they need to download the Visual Studio 2010 redistributable:
http://www.microsoft.com/en-gb/download/details.aspx?id=5555
In short: no. You can't simply use the GCC compiler in Visual Studio.
The long version: you can use a Makefile project to run GCC, but then you lose some of the benefits of using Visual Studio.
I'm not sure when this became a feature in Visual Studio (probably wasn't one in 2013), but it is now possible to change the "Platform Toolset" - and thus the compiler - used by Visual Studio to other compilers like Clang/LLVM and GCC. I have been happily using Clang with VS2017 ever since the MSVC++ compiler bugged up with some 3rd party libraries.
Microsoft made a blog post on the installation instructions and how to switch.