I want to make use of the module support for consuming the C++ std library in Visual Studio/C++ 2017. I have code which makes use of the assert macro but I can't seem to get the definition of assert from the std library modules. For example:
import std.core;
void f()
{
assert(true);
}
Fails with errors:
1>API_Constants.cpp(10): error C3861: 'assert': identifier not found
What do I need to do to get the definition of assert?
Update 1
Thanks to those who suggested #include <cassert> that was what I have been trying. Unfortunately, for the code:
import std.core;
#include <cassert>
import std.core;
I get the following errors:
1>verify-header-compilation.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.12.25827\include\vadefs.h(134): error C2953: '__vcrt_va_list_is_reference': class template has already been defined
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.12.25827\include\vadefs.h(131): note: see declaration of '__vcrt_va_list_is_reference'
Note that the following works so I will need to put the #include <cassert> as the first include of any cpp file that uses assert (similar to the way pre-compiled header includes need to be the first include).
#include <cassert>
import std.core;
import std.core;
Update 2
While appropriately placed #include <cassert> resolves my specific question I have hit the same problem with other macros from standard headers. In particular I am now in trouble with RAND_MAX from cstdlib and pulling the same trick does not work. It would seem that these headers need to be rewritten so that it is possible to mix includes with imports or we need new headers that just give us the macros.
Update 3
Note that making #include <cassert> the last include in a cpp file is problematic for two reasons. First, I use assert with templates in headers so I would need to edit a non-trivial amount of files to get this to work. Second, the following doesn't compile at present (fails with same errors as above):
import std.core;
#include <cassert>
Maybe you need #include <cassert> at the top of your file.
The MSVC doc about cpp modules says:
Standard Library Modules (Experimental)
std.regex provides the content of header <regex>
std.filesystem provides the content of header <experimental/filesystem>
std.memory provides the content of header <memory>
std.threading provodes the contents of headers <atomic>, <condition_variable>, <future>, <mutex>, <shared_mutex>, <thread>
std.core provides everything else in the C++ Standard Library
So it should be included. But maybe StoryTeller is right and macros are not included.
You need to move #include to be the final header file included, so that the assert macro is not undefined, i.e., so that the assert macro applies to this .cpp file's source code.
Related
Here's a MS official example of how to use MSXML
void AddCollectionSample()
{
IXMLDOMDocument2Ptr pIXMLDOMDocument2;
IXMLDOMSchemaCollection2Ptr pIXMLDOMSchemaCollection2Ptr;
...
}
The code is simple, but doesn't compile on my Visual Studio 2019 with the error can't find the declaration of IXMLDOMDocument2Ptr.
I've already included these headers and lib.
#include "msxml2.h"
#include "msxml6.h"
#pragma comment(lib, "msxml6.lib")
I searched this IXMLDOMDocument2Ptr online and found out it's supposed to be under MSXML2 (a namespace). But my project doesn't recognize this namespace as well. Then I found it, you have to import this library,#import <msxml6.dll>
But this is supposed to be for C#. How about my C++ project?
Edit:
I find this,
https://www.codeproject.com/Articles/43309/How-to-create-a-simple-XML-file-using-MSXML-in-C
The first thing you need to do is add these two lines in the project stdafx.h file:
#import "MSXML4.dll" rename_namespace(_T("MSXML"))
#include <msxml2.h>
I modified this MSXML4.dll to my newly MSXML6.dll, but the project can't find it still.
The sample code is using _com_ptr_t, a COM smart pointer class template provided by Visual Studio. Yet the code provided is incomplete.
To declare a _com_ptr_t specialization for any given interface you would usually use the _COM_SMARTPTR_TYPEDEF macro. In this case the following declarations need to be added:
_COM_SMARTPTR_TYPEDEF(IXMLDOMDocument2, __uuidof(IXMLDOMDocument2));
_COM_SMARTPTR_TYPEDEF(IXMLDOMSchemaCollection2, __uuidof(IXMLDOMSchemaCollection2));
This declares the types IXMLDOMDocument2Ptr and IXMLDOMSchemaCollection2Ptr as specializations of _com_ptr_t, ready to be used. You'll need to #include <comip.h>, too.
An alternative would be to use the compiler specific #import directive. This generates header files from a type library (TLB), and by default includes the respective _COM_SMARTPTR_TYPEDEF declarations (see Primary type library header file).
From this demo
The first thing you need to do is add these two lines in the project stdafx.h file:
#import "MSXML6.dll" rename_namespace(_T("MSXML"))
#include <msxml2.h>
Then correct all the MSXML2 to MSXML, as we specified the name rename_namespace(_T("MSXML")).
I didn't bother to do this, I at first tried namespace MSXML2=MSXML didn't work. So I just defined a MACRO #define MSXML2 MSXML. It compiled!
I'm using mongoose to build an HTTP server in C++, and I'm getting an error message when I try to include other files in my program:
/Library/Developer/CommandLineTools/usr/include/c++/v1/cstdint:183:8: error:
expected unqualified-id
using::intptr_t;
^
/Users/cs/Downloads/mongoose-master/mongoose.c:2415:18: note:
expanded from
macro 'intptr_t'
#define intptr_t long
^
This happens whenever I attempt include the following files in my program:
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <iterator>
#include <sstream>
I've tried to narrow it down to one of these files causing the problem by commenting out some of them, but it appears that any one of them causes the error. Interestingly enough, string.h does not cause the error.
It sounds like your source code contains something like this:
#include <mongoose.c>
The .c file defines a macro that collides with words used in the standard library headers.
Including a .c file is not a good practice. Instead, you should build the mongoose library, and link your program against it.
If you really have to keep everything in a single translation unit, you should be able to move that dubious include statement to after all other headers, or even to the bottom of your cpp file.
But it would be best to figure out how to build mongoose.c separately, then link against the resulting library. You can ask a separate question, or see if you get anything out of this: Can't figure out how to build C application after adding Mongoose Embedded
MS Visual Studio 2017, new Windows Console Application project from VisualC++ category. The default stdafx.h header includes the following lines:
#include <stdio.h>
#include <tchar.h>
And below them:
// TODO: reference additional headers your program requires here
Because this comment is put below, not above #include <stdio.h>, I am inclined to believe that stdio.h is included by default not as a convenience for the user who would probably #include it anyway, but is for some reason required? The fact that stdio.h is included rather than cstdio only seems to support this interpretation?
AM I right? Can I safely remove #include <stdio.h>? I'm asking because the very first line in my main function reads:
std::ios_base::sync_with_stdio(false);
Which is obviously incorrect if facilities from stdio are being used anyway, as this header being included would suggest.
1) Can I safely remove the line that says #include <stdio.h> from stdafx.h?
2) Can I safely call std::ios_base::sync_with_stdio(false)?
3) Is the fact that tchar.h is also included by default relevant here?
Yes you can remove it.
You can even name your precompiled header not stdafx.h, or disable percompiled header at all.
It is just convenience and example to start with.
Its up to you if you want to use it, however before removing it read on this MSDN about precompiled Header Files
Hope this helps!
I'm including a library in a C++ program, which includes memory and other libraries from the std c++ implementation, like so:
#include <memory>
There is an error happening:
Lexical or Preprocessor issue 'memory' file not found
This happens the same with all the includes in this file:
#include <cmath>
#include <vector>
I'm wondering what could cause such an issue? Could it be that the compiler is not set somewhere properly in the build settings? If so, any idea where?
I lately decided to include the JavaScript V8 Engine in my project. After compiling and linking to it, I wanted to run the example from the Getting Started guide.
It works in general, but for some reasons there are namespace conflicts when I do not specify the v8 namespace in front of each class name. Visual Studio 2012 tells me for example, that the name Context would be ambiguous. But I do not understand why.
The only namespaces I include in this file are std and v8. It is a header file and before you ask, it's meant to be so since it claims to be a header-only library.
#pragma once
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <functional>
#include <memory>
#include <typeindex>
#include <iostream>
#include <fstream>
#include <V8/v8.h>
namespace library_name
{
using namespace std;
using namespace v8;
// here comes the example code and more...
}
To find out where a symbol with the name Context might be defined too, I used the Find Definition command in Visual Studio, available in the context menu. This is the list of results.
It lists for example sf::Context which I use in the project but not in that header. There are also definitions in cryptic namespaces located at files in a directory named Windows Kits. I neither know what they are for nor have I included them intentionally. I don't include other header files except from the standard library and JavaScript V8 as shown above.
Why do the Contexts from different namespaces collides with each other? How can I fix this collisions to use the v8 namespace?
Try actually compiling the code. The compiler should list the actual conflicting symbols, possibly including where they are actually defined. You will need to either remove one of your usings or fully qualify the identifier.