Compiler Error Assumed value of MB_LEN_MAX wrong - c++

I'm currently trying to run this image segmentation program, but I'm having errors when I try to compile it. To compile, you just make, which runs the command
g++ -g -O3 -I. -o segment segment.cpp -lm
However, it runs into a compilation error -- its output is
In file included from /usr/include/wchar.h:887:0,
from /usr/include/c++/6/cwchar:44,
from /usr/include/c++/6/bits/postypes.h:40,
from /usr/include/c++/6/iosfwd:40,
from /usr/include/c++/6/ios:38,
from /usr/include/c++/6/istream:38,
from /usr/include/c++/6/fstream:38,
from ./pnmfile.h:27,
from segment.cpp:23:
/usr/include/x86_64-linux-gnu/bits/wchar2.h:448:3: error: #error "Assumed value of MB_LEN_MAX wrong"
# error "Assumed value of MB_LEN_MAX wrong"
^~~~~
Makefile:14: recipe for target 'segment' failed
make: *** [segment] Error 1
Here is the relevant area of /usr/include/x86_64-linux-gnu/bits/wchar2.h
/* We would have to include <limits.h> to get a definition of MB_LEN_MAX.
But this would only disturb the namespace. So we define our own
version here. */
#define __WCHAR_MB_LEN_MAX 16
#if defined MB_LEN_MAX && MB_LEN_MAX != __WCHAR_MB_LEN_MAX
# error "Assumed value of MB_LEN_MAX wrong"
#endif
if (__bos (__s) != (size_t) -1 && __WCHAR_MB_LEN_MAX > __bos (__s))
return __wcrtomb_chk (__s, __wchar, __ps, __bos (__s));
return __wcrtomb_alias (__s, __wchar, __ps);
}
And the relevant areas of segment.cpp:
#include <cstdio>
#include <cstdlib>
#include <image.h>
#include <misc.h>
#include <pnmfile.h> //<-- Line 23 of segment.cpp, where the error occurs
#include "segment-image.h"
and pnmfile.h:
#include <cstdlib>
#include <climits>
#include <cstring>
#include <fstream> //<-- Line 27 of pnmfile.h where the error occurs
#include "image.h"
#include "misc.h"
Does anyone know what's causing this error (and if so, how to fix it)? I'm running Ubuntu 16.04 with g++ 6.4.0. Thank you for any help you can give!
EDIT: I've confirmed that fstream does work. I compiled the following
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
int main() {
ifstream is("in.txt");
int a, b;
is >> a >> b;
cout << a << endl;
cout << b << endl;
return 0;
}
with the same command
g++ -g -O3 -I. -o temp.exe temp.cpp -lm
and it successfully printed out two integers read from a text file.

You need to include limits.h from the same libc from which your bits/wchar2.h is coming. If you check the comment in the latter file just above the #error which you are getting, you'll see why - it serves as a safeguard against mixing incompatible header files.

This kind of longtime issues is my motivation to build my own Linux distro.
Googling gets you nowhere. But ... building your own (cross) compiler
solves the problem. Compiler has it own definition in his relevant headers MB_LEN_MAX 1
MB_LEN_MAX 16 in glibc includes. No problem.

Related

Warning message when including numpy/arryobject.h

I'm trying to include NumPy objects from C++ code.
I started this from today, and as a first sample program, I made the following stub code.
#include <Python.h>
#include <numpy/arrayobject.h>
#include <iostream>
static PyObject* SpamError;
int main(void) {
std::cout << "Hello, world!\n";
return (0);
}
I used the following command for building. I did building on Ubuntu 14.04.
g++ -o out test.cc -I/usr/include/python2.7 -lpython2.7
But if I use the above build command, then the following error message appears.
/usr/include/python2.7/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
#warning "Using deprecated NumPy API, disable it by " \
I do not clearly understand the meaning of this, and I would like to know how to avoid this issue. Could anyone give some advice on this?
Either upgrade to the latest library as pointed by the error or add #define as below to your code
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <Python.h>
#include <numpy/arrayobject.h>
#include <iostream>
static PyObject* SpamError;
int main(void) {
std::cout << "Hello, world!\n";
return (0);
}
This is a known issue. If you read the thread there, it seems like you could 1. upgrade your numpy version, 2. ignore it until you do, or 3. for the time being, use the #define mentioned in the warning message:
#define NPY_NO_DEPRECATED_API
#include <Python.h>
#include <numpy/arrayobject.h>
#include <iostream>
Of course, upgrading to a version where this warning does not occur is the best alternative (but not always possible).

Mingw doesn't compile due to "'isblank' was not declared in this scope" error

I try to compile & link the following program with mingw. When I use the default version it works well. But when I use c++11 version it doesn't compile and gives me the following error newfile.cpp:22:18: error: 'isblank' was not declared in this scope.
for testing the following program it's enough to call the _isblank function in main.
For compiling the Netbeans 8.0.2 uses g++ -c -g -Wall -std=c++11 -MMD -MP -MF "build/Debug/MinGW_1-Windows/newfile.o.d" -o build/Debug/MinGW_1-Windows/newfile.o newfile.cpp.
The mingw version is 4.8.1 and everything is configured well(Default).
I tried by adding/removing namespace std. The problem seems to be in cctype header! But I wonder how to solve it. The project will have to be compiled with g++ on linux! Will these problems remain?
#include <cstdlib>
#include <cctype>
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
#include <map>
#include <functional>
#include "newfile.h"
using namespace std;
conf_info_t CONF_INFO;
#define CONF_FILE_ADDRESS "confs.txt"
//
//typedef std::map<std::string, std::function<void (const std::string&)>> confMap_t;
//confMap_t confMap;
int _isblank(int c){
return isblank(c);
//return c == ' ' || c == '\t';
}
In my version of GNU library the declaration of isblank in <ctype.h> header is protected by some conditional compilation directives, which nevertheless should make this function available in C++.
However, in <cctype> header this function is is separated from all other declarations and given a special treatment for some reason. It is only made available in namespace std if macro _GLIBCXX_USE_C99_CTYPE_TR1 is defined. This is what it looks like inside <cctype>
#if __cplusplus >= 201103L
#ifdef _GLIBCXX_USE_C99_CTYPE_TR1
#undef isblank
namespace std
{
using ::isblank;
} // namespace std
#endif // _GLIBCXX_USE_C99_CTYPE_TR1
#endif // C++11
I don't know what the purpose that _GLIBCXX_USE_C99_CTYPE_TR1 macro is supposed to serve. In my GCC installation this macro is defined, which makes isblank available in my case.
You might want to check what your <cctype> looks like and see if something like that is happening on your side as well.
It worked when I undefined STRICT_ANSI in the translation unit adding -U__STRICT_ANSI__ to the compiler options. But I wonder which part of my program violates C++ standards.
It should have been compiled in this way:
g++ -U__STRICT_ANSI__ -c -g -Wall -std=c++11 -MMD -MP -MF "build/Debug/MinGW_1-Windows/main.o.d" -o build/Debug/MinGW_1-Windows/main.o main.cpp

Error when trying to separating class into .h, .cpp

This is a minimal program that I made to understand this problem better.
ADT.h
#ifndef ADT_H
#define ADT_H
class ADT {
public:
void print();
};
#endif
ADT.cpp
#include <iostream>
#include "ADT.h"
using namespace std;
void ADT::print()
{
cout << "This program works." << endl;
}
testADT.cpp
#include <iostream>
#include "ADT.h"
using namespace std;
int main(void)
{
ADT sa;
sa.print();
return 0;
}
I compiled it with the vim/minGW compiler my school provided me like so:
g++ testADT.cpp
Which produced the following error:
C:\Users\King\AppData\Local\Tempcc6eoWAP.o:testADT.cpp(.text+0x15 reference to 'ADT::print()'
collect2.exe error: ld returned 1 exit status
Can you explain this error message and indicate the error in my code?
You didn't post the error, but I see that you're missing the semicolon after void print()in the header.
EDIT: That's a linker error. Each source file should be compiled into an object file; then the object files linked:
g++ -c -oADT.o ADT.cpp
g++ -c -otestADT.o testADT.cpp
g++ -oADT ADT.o testADT.o
You can also do it in one line as in michaeltang's answer, but then you can't recompile the sources individually (the 2 step method scales better).
You should also compile ADT.cpp
g++ -o testadt testADT.cpp ADT.cpp

GCC iostream fstream error in Ubuntu 13.10

I am using Ubuntu 13.10. I am getting some errors for the following code.
#include <stdlib.h>
#include <stdio.h>
#include <fstream.h>
int main(int argc, char *argv[])
{
error.set_program_name(argv[0]);
if ( argc != 2 )
{
// printf(argv[0] + " usage: fifo_client [string] \n");
/// cout << argv[0] << " usage: fifo_client [string]" << endl;
exit(EXIT_FAILURE);
}
ofstream out(fifo_file);
if(out)
out << argv[1] << endl;
return(EXIT_SUCCESS);
}
If I run the above program a.c using command
gcc a.c -o a
a.c:1:20: fatal error: iostream: No such file or directory
#include <iostream>
^
compilation terminated.
I don't know whats the problem.
Use g++ instead of gcc. gcc could compile a c++ file if it had the right extension (.cpp for instance) or with the right arguments (-x c++) but adding the arguments needed to link with the C++ libraries is far too complex to avoid the simple solution.
The problem is that you're mixing C & C++ code and compiling it using GCC.
try
#include <fstream>
using namespace std;
instead of #include <fstream.h>
anyway your source code is not full to make correct suggestion.
I ran your code in my compiler and got following error :-
test2.c:3:21: fatal error: fstream.h: No such file or directory
#include <fstream.h>
^
compilation terminated.
so i think your question has typo.
It is because you are mixing c and c++ code, fstream is part of c++. try to run by g++.

ICC 11.1 has strange behaviour regarding PTHREADS on ia64

I'm working on a ia64-machine using ICC 11.1. The following program compiles nicely:
#include <pthread.h>
#include <iostream>
using namespace std;
int main()
{
cout << PTHREAD_STACK_MIN << '\n';
return 0;
}
When I compile it with icc test.cpp -o test
BUT when I change the contents of the file to to:
#include <pthread.h>
#include <stdio.h>
int main()
{
printf("%d\n", PTHREAD_STACK_MIN);
return 0;
}
I suddenly get:
icc -c test.cpp -o test.o test.cpp(6):
error: identifier "PTHREAD_STACK_MIN"
is undefined
printf("%d\n", PTHREAD_STACK_MIN);
^
compilation aborted for test.cpp (code
2)
Can anyone explain to me why? Or more importantly: how I can work around this issue so that the second code example will also compile?
Well, that's easy: you forgot to include <limits.h> where the PTHREAD_STACK_MIN is supposed to be declared (as per POSIXv6/SUSv3).
And from the error one can conclude that <iostream> internally also includes the <limits.h> why in C++ mode the error doesn't happen.