Mongoose - error when including standard C++ library files - c++

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

Related

'strtoll' and many others "not a member of 'std'"

strtoull,strtof,strtold,vsnprintf,wcstoll,wcstoull,wcstof, and wcstold are not members of 'std.' The problem is I don't use any of these functions. I didn't even know they existed. they are in file basic_string.h, which I also didn't know I was using. I'm using VS2015 so I assume I am compiling using the VSC++ 14.0 compiler.
I see that there over a dozen similar questions but I can only find two common errors: not using the C++11 flag and not #includeing . I have had the C++11 flag set and #include <algorithm> changes nothing. this question makes me curious,though. I thought the order of #includes didn't matter. I tried system libraries <> first and 3rd party libraries second "" and also vice-versa and observed no difference.
I also tried #include "stdafx.h" as some other answers indicated, but no such file exists.
not sure what to include here because I finally got to the point where the IDE identifies no errors in my code. again, all the issues are in "basic_string.h." here are my includes:
#include <iostrream>
#include <string>
#include <cstring>
#include <sstream>
#include <map>
#include "curl/curl.h"
#include "curl/easy.h"
this may be a continuation of my other question here, which I will update now.
edit 1: I have included <cstdlib>,<cstdio>,and<cwchar> and I see no difference. I'm curious why it's having issues with functions I've never used in files I've never touched.
I totally forgot I ran a repair on VS2015 yesterday and here are the results. I'm not sure if these are optional modules or core issues or what. "Windows Software Development Kit" sounds important but I am not developing for a windows machine(though I am developing on Win7).
edit 2: someone asked for minimal reproducible code. here is the whole file sans comments:
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <map>
#include <cstdio>
#include <cwchar>
int main(int argc, char *argv[])
{
return 0;
}
just realized I don't need all the includes for nothing(because i commented all functional code out) so I will now see which one is causing the issue.
edit 3 : cstring,cstdlib,cstdio, cwchar, and the two curl headers are the only headers that will not cause this issue. I can include all six of them at once without issue. any other #include that was mentioned in the OP or the previous edit will cause the same ...is not a member of 'std'error. However, I don't think I had any of the <c...>included before I started having this issue.iostream,string,sstream,map each (individually even) cause this issue. are we still looking at a complete VS2015 reinstall?
edit 4: As one user suggested, I tried to make a new project but this is what happened. I was really hoping we wouldnt get to this point because I'm not sure how much you all will be able to help. Basically,we have some complicated build setup which I don't understand. The other guy who works here also does not understand the setup. The one person who knows is out on paternity leave. I'm developing on one machine and building/debugging on a server. However, I was poking around the settings and I saw a different diagram that has a second server in the mix and I'm not sure how that plays into it. After that, it is deployed to a different machine. Anyway I selected the same options as the current project but I can't even create a blank one. I'm not sure if this failure is because of this complicated system or because my VS2015 is messed up. Either way I can't fix this as it is so I think I will work on repairing VS2015 (which already had issues once) while monitoring this thread to see if this new project error reveals something important. thanks a lot to all who helped so far
edit 5: I'm taking next week off. I won't be able to give any updates till the 3rd for anybody who still cares. I'm going to repair and then reinstall
Those functions are in the namespace std if you include the correct standard headers, which are <cstdlib>, <cstdio> and <cwchar>. "basic_string.h" is not a standard header; it is probably some implementation specific header. "stdafx.h" is what Visual Studio uses for precompiled headers (it's not in the standard either) and the order in which you include headers can matter although it usually shouldn't.
I see that there over a dozen similar questions but I can only find... not #includeing
That appears to be the problem.
In order to use standard functions, the header which declares those functions must be included. The listed functions cannot be used because the corresponding headers haven't been included.
For example, strtoull is declared in <cstdlib>, which has't been included according to your quoted list of includes.
The problem is I don't use any of these functions. I didn't even know they existed. they are in file basic_string.h
If the error indicates that basic_string.h uses those functions, and that's the list of headers that basic_string.h includes, then basic_string.h is buggy.
I'm curious why it's having issues with functions I've never used in files I've never touched.
You either use basic_string.h, or you use some header that in trurn uses basic_string.h. The error message should tell you how the inclusion takes place.
I have ... #include <algorithm>
Including <algorithm> lets you use the declarations from that header. It doesn't let you use declarations from other headers.
I also tried #include "stdafx.h" as some other answers indicated, but no such file exists.
If your project is configured to use pre-compiled "stdafx.h", then you must include it. If it is not configured to use it, then you must not include it. Pre-compiled headers must be included before anything else in your file.
it was a problem with my Include directories in VisualGDB Properties-->Makefile settings. I was adding a new directory for each library I got. I reinstalled VS2015, but that did nothing because the culprit was a project option. I copied the default* include directories and that was it.
*someone on my team provided me with a list of the default include directories

Visual Studio C++ 2017 modules assert macro

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.

xcode C++ : Lexical or Preprocessor issue 'memory' file not found

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?

Intel C++ Compiler can't find stdio.h

I am trying to build my code using the Intel C++ Compiler, but for some reason it fails with this error:
catastrophic error: cannot open source file "stdio.h"
In this line #include <stdio.h>.
Any ideas?
stdio.h is a standard header file; it's a bad idea to have a local file of the same name. If you meant to include the standard header, it should be on your include path, and you should include it with
#include <stdio.h>
You should also consider whether you might get more benefit from including <iostream> or including <cstdio> (like including <stdio.h>, but puts the symbols safely into the std namespace).
If you're running on Windows, then installing Visual Studio, then invoking "psxevars.bat" might solve your problem, it solved it for me.

string.h and <string>

I am trying to build a library in a legacy C++ project. Platform is power-pc using gcc compiler.
The compiler is giving build errors similar to this: error: string: No such file or directory or error: vector: No such file or directory.
My understanding is that it is not able to locate the standard library. Where are the standard library files typically reside, and in what format? Is it *.h or some other? I searched for this on internet but I don't think I fully understand it.
The confusing part is that another library in the same project using same source code file builds prefectly alright. This suggests to me that may be the makefiles for these two projects are different, where one IS able to locate the std lib, other isn't. But a quick comparison b/w the two didn't bring up any obvious differences. Any other thoughts on this please?
Lastly, I just learned that string.h is for c-style strings, and string is for C++ std lib. Is it ok to mix them, i.e. a source file has #include string.h, and also #include string implicitly through including some other file? I ask because this is the same situation in the file that is not building.
Thanks.
Error Message:
Compiling SOURCE_FILE.cpp
In file included from SOURCE_FILE.cpp:3:
HDR_FILE.h:1: error: string: No such file or directory
HDR_FILE.h:2: error: vector: No such file or directory
CODE IN SOURCE_FILE.cpp
#include <stdlib.h>
#include <string.h>
#include "fileABC.h"
using namespace std;
// Other code
CODE IN HRD_FILE.h
#include <string>
#include <vector>
#include <hdr.h>
#include <hdr-cseq.h>
//... OTHER FILES ETC.
If the files are not being detected as C++ source code then you can get errors like this. Are the c++ files named using an appropriate file extension?
cat test.c
#include <vector>
int main() {}
gcc test.c
test.c:1:18: error: vector: No such file or directory
The command above will compile C code, but not C++. Remember that these are two different languages. To build C++ programs you have to tell the compiler that the source is C++ one way or another. If you use g++ it will automatically assume files with '.c' extensions are C++ (at least my version does. Or you can tell the compiler to use C++ by passing -x c++ right before you pass the file. You also have to tell the linker to include the C++ library. g++ does this automatically as well, or you can pass the linker flag -lstdc++.
The hard way:
gcc -x c++ test.c -lstdc++
The easy way:
g++ test.c
In answer to your last question, you can freely mix <string.h> and <string> (though you should probably use <cstring> instead of <string.h>).
The C language string is different than the C++ std::string.
The C language string type is a sequence of characters terminated by a nul, zero, '\0', character. The function declarations for managing this data structure are in <string.h> or <cstring>, depending on whether you compile as C or C++, respectively.
The C++ std::string type is a container of characters. The actual implementation may vary among compilers, but the interface won't. The methods of the std::string are declared in <string>. The compiler must be set up for compiling in the C++ language.
The C++ std::string has methods for generating C language strings and also for creating C++ strings from C language style strings. Read up on the Standard Template Library for more information.
Also, search the Web for "C++ FAQ". This is mandatory reading before programming in the C++ language. There is also a FAQ for the C language too.