Warning message when including numpy/arryobject.h - c++

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).

Related

Matplotlib wrapper for c++

I'm trying to get Matplotlib wrapper to work on wxDevC++
The code
#include <cstdlib>
#include <iostream>
#include "matplotlib-cpp/matplotlibcpp.h"
#include <vector>
namespace plt=matplotlibcpp;
using namespace std;
int main(int argc, char *argv[])
{
std::vector<double> y={1,3,2,4};
plt::plot(y);
plt::savefig("minimal.pdf");
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
I use Win 7, I have python 27 and Python 38. It keeps telling me that there is no Python.h file. I've no idea how to fix this.
You have to set the include path for Pathon.h in your build configuration and you have to use at least C++11.
You can set the C++ standard in GCC and compatible compilers with the compiler option -std=c++11 or in older versions with -std=c++0x. For the GNU variants you can use -std=gnu++11 resp. -std=gnu++0x.

Where does Homebrew install C++ packages?

I would like to use C++'s std::format library to format strings. See the minimal working example below.
/* example.cpp */
#include <iostream>
#include <format>
#include <string>
int main() {
std::string s = std::format("Hello, {}!", "John");
std::cout << s << std::endl;
return 0;
}
However, when I compile my code, I get the following error message:
example.cpp:2:10: fatal error: format: No such file or directory
2 | #include <format>
I am using the latest version of macOS, and I have Homebrew installed as my package manager. I already installed clang-format through Homebrew, but for some reason, my compiler can't locate the header file. Can somebody help me figure out the problem is? I have tried using Apple's GCC and the custom GCC10 provided by Homebrew, but in both cases, I get the same error message. Is this a Homebrew issue or a C++ issue?
Following this answer to use fmt, you can install the library with brew install fmt, then modified the code to
/* example.cpp */
#include "fmt/format.h"
#include <iostream>
#include <string>
int main() {
std::string s = fmt::format("Hello, {}!", "John");
std::cout << s << std::endl;
return 0;
}
and compile with
clang++ -std=c++11 test.cpp -lfmt

Compiler Error Assumed value of MB_LEN_MAX wrong

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.

Error compiling private.hpp OpenCV 3.0.0-rc1

I downloaded OpenCV 3.0.0-rc1 and build it using CMAKE-gui 3.2.2 using VS2012 Win64 compiler. Binaries and libraries got generated and I set it up with Qt 64 bit. All programs are working fine except when I try to use the feature cv::LineSegmentDetector it shows a compile error in private.hpp file. The error says
unexpected end-of-line
My code is as follows
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/private.hpp>
#include <opencv2/core/utility.hpp>
using namespace std;
int main()
{
cv::Mat image = cv::imread("C:\\Users\\IMAGE\\Desktop\\PROJ\\SAMPLE.png");
cv::imshow("TEST",image);
cv::waitKey();
cv::LineSegmentDetector lsd;
return 0;
}
And on following the error I found the 2nd line of the following part of the code in private.hpp as error highlighted.
#ifdef HAVE_EIGEN
# if defined __GNUC__ && defined __APPLE__
# pragma GCC diagnostic ignored "-Wshadow"
# endif
# include <Eigen/Core>
# include "opencv2/core/eigen.hpp"
#endif
# if defined __GNUC__ && defined __APPLE__
Please let me know if I am doing some implementation mistake or some changes in the private.hpp can fix this error. I am using windows 8 64 bit.
oh, never try to use something, that is called "private", i guess...
#include <opencv2/opencv.hpp> // includes all others
#include <opencv2/core/utility.hpp> // getTickCount, etc.
int main()
{
// LineSegmentDetector is an abstract class, you can't create an
// instance on the stack, but need to use Ptr and factory:
cv::Ptr<cv::LineSegmentDetector> lsd = cv::createLineSegmentDetector();
return 0;
}

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++.