Clang/gcc use different c++ standard for header and main - c++

I was using CImg for picture processing, which don't compile for C++11 or above:
CImg.h:48902:22: error: there are no arguments to 'pclose' that depend on a template parameter, so a declaration of 'pclose' must be available [-fpermissive]
But my program which includes CImg.h does use some C++11 feature. Can I use different C++ standard to process these two files? CImg.h itself is a header+code so I have to #include that.
Here is my test program:
#define cimg_display 0
#include "CImg.h"
int main(){}
CImg.h comes from the lastest version from http://cimg.eu/.

Related

How to import matrix market files with eigen library in c++

I'm new to C++ and used to MATLAB. Unfortunatly my matrix size got too big for MATLAB, so I want to try it in C++.
I've found the eigen library 3.3.7 to do matrix manipulations. For this I need to import my matrix market files into Visual Studio 2019. I know some basics in C++ and tried to import my files with loadMarket. After trying to compile it I get like 30 errors in the MarketIO.h file.
This is the file I'm using.
https://eigen.tuxfamily.org/dox/unsupported/MarketIO_8h_source.html
#include <Eigen/Sparse>
#include <unsupported/Eigen/src/SparseExtra/MarketIO.h>
int main(){
typedef Eigen::SparseMatrix<float, Eigen::RowMajor>SMatrixXf;
SMatrixXf A;
Eigen::loadMarket(A, "B.mtx");
}
You must never directly include files from unsupported/Eigen/src/... (or from Eigen/src/...). Just include the corresponding parent header instead:
#include <unsupported/Eigen/SparseExtra>
I probably met the same problem or a closely related one (unfortunately the question has not a detailed error message) while using the SparseExtra module as in the question.
By compiling the code provided in the question I get many errors among which:
/usr/include/eigen3/unsupported/Eigen/src/SparseExtra/MarketIO.h:115:20:
error: variable ‘std::ifstream in’ has initializer but incomplete type
115 | std::ifstream in(filename.c_str(),std::ios::in);
| ^~~~~~~~
.... many warnings and errors releated to std::ifstream and std::ofstream ...
/usr/include/eigen3/unsupported/Eigen/src/SparseExtra/MarketIO.h:272:9:
error: no match for ‘operator<<’ (operand types are ‘std::ofstream’ {aka
‘std::basic_ofstream<char>’} and ‘const char [42]’)
compiled using g++ -std=c++17, g++ version 12.1.0, Eigen3 v 3.3
I don't know if this is an Eigen bug, but as the first line in the error above shows it seems that the compiler is not able to figure out the definition of std::ifstream.
The problem is solved by modifying the MarketIO.h source code (on my machine located at usr/include/eigen3/unsupported/Eigen/src/SparseExtra/MarketIO.h) as follow:
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2011 Gael Guennebaud <gael.guennebaud#inria.fr>
// Copyright (C) 2012 Desire NUENTSA WAKAM <desire.nuentsa_wakam#inria.fr>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef EIGEN_SPARSE_MARKET_IO_H
#define EIGEN_SPARSE_MARKET_IO_H
#include <iostream>
#include <vector>
#include <fstream> // IMPORT THIS HEADER FILE!
... // no changes in the rest of the file
This removes any error and makes the code to compile and properly load the matrix.
Since OP mentions large matrices, another option is fast_matrix_market's Eigen bindings. The Eigen's MarketIO.h loader is sequential, fast_matrix_market is parallel.
#include <fstream>
#include <Eigen/Sparse>
#include <fast_matrix_market/app/Eigen.hpp>
int main(){
typedef Eigen::SparseMatrix<float, Eigen::RowMajor>SMatrixXf;
SMatrixXf A;
std::ifstream file("B.mtx");
fast_matrix_market::read_matrix_market_eigen(file, A);
}

How to include C11 headers when compiling C++ with GCC?

In a C++ project, I'm using a C library which includes some C11 headers. It won't compile with GCC. See this simple code:
// main.cc
#include <stdatomic.h>
int main()
{
return 0;
}
Running gcc main.cc -lstdc++, it complains: error: ‘_Atomic’ does not name a type. However, clang main.cc -lstdc++ works like a charm.
I'm wondering what makes the difference, and how can I compile it with gcc?
To wrap C headers that use atomics, you may use the other spelling of _Atomic and define a macro that transforms this to valid C++:
#ifndef __cplusplus
# include <stdatomic.h>
#else
# include <atomic>
# define _Atomic(X) std::atomic< X >
#endif
int foo(_Atomic(unsigned)* toto);
Both atomics interfaces have been developed in sync between the two committees, so besides syntax problems these should be binary compatible on any reasonable platform that provides C and C++.

OpenCV3.10 core.hpp must be compiled in C++

i have installed OpenCV 3.10 and the linked the opencv_world310.lib to release and opencv_world310d.lib to debug.
Moreover I put the compiler options in search directory to ...opencv\build\include. I got a undefined reference error when i left out #include <opencv2/highgui.hpp. Now that i have included it my code looks like this:
#include <stdio.h>
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include <opencv2/highgui.hpp>
int main(void){
printf("HALLO!");
return 0;
}
When i try to build it core.hpp opens and the error: core.hpp must be compiled in C++ occurs.
I am using the GNU GCC Compiler in Codeblocks.
What should i do to solve the problem?
Check you compiler options. Open CV 3.10 C++ API requires code to be compiled as C++, but not C. You can use answer to "CodeBlocks: change project language c and c++" question to change the options.
Also use the new Open CV 3.10 API
#include <opencv2/opencv.hpp>`
instead of all the other Open CV header files. This header includes core functionality. To enable highgui module you need to define HAVE_OPENCV_HIGHGUI in your project settings.

geany: C++ Including libraries and headers

I'm very new in Ubuntu and programming C++ on Ubuntu using Geany.
The problem I have here is that:
the classes i want to iclude to my project will receive an error,
I type,
#include <vector>
the error given here is,
fatal error: vector: No such file or directory
also I cannot use namespace std,
typing using namespace std returns the following error,
error: unknown type name 'using'
Here is the code:
#include <stdio.h> //no problem here
#include "stdlib.h" //no problem here
#include <vector> //this is a problem (lets say it returns error 1)
using namespace std; //this is a problem (lets say it returns error 2)
int main(int argc, char **argv)
{
return 0;
}
This sounds like you are using the wrong compiler to compile your C++ code. For example, by invoking gcc test.cpp the C++ file is actually compiled as C and you receive errors such as the one you posted - there is no vector header in C and there is also no using keyword.
If you are using gcc, the correct way to invoke the compiler to compile C++ is via the g++ symlink, i.e. g++ test.cpp
If you are using clang, the executable is called clang++ instead.
Both compilers support the -x parameter to manually change the language to C++, although in that case you also have to specify that the compiler needs to link your files with the C++ standard library. For example: gcc -x c++ test.cpp -lstdc++

Compiler error: memset was not declared in this scope

I am trying to compile my C program in Ubuntu 9.10 (gcc 4.4.1).
I am getting this error:
Rect.cpp:344: error: ‘memset’ was not declared in this scope
But the problem is I have already included in my cpp file:
#include <stdio.h>
#include <stdlib.h>
And the same program compiles fine under Ubuntu 8.04 (gcc 4.2.4).
Please tell me what am I missing.
You should include <string.h> (or its C++ equivalent, <cstring>).
Whevever you get a problem like this just go to the man page for the function in question and it will tell you what header you are missing, e.g.
$ man memset
MEMSET(3) BSD Library Functions Manual MEMSET(3)
NAME
memset -- fill a byte string with a byte value
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <string.h>
void *
memset(void *b, int c, size_t len);
Note that for C++ it's generally preferable to use the proper equivalent C++ headers, <cstring>/<cstdio>/<cstdlib>/etc, rather than C's <string.h>/<stdio.h>/<stdlib.h>/etc.