Compilation using Boost Test Unit in std c++11 - c++

I'm trying to compile a very simple program using Boost Test Unit
#define BOOST_TEST_MODULE My Test
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_CASE(first_test) { int i = 1; BOOST_CHECK(i == 1); }
If I compile this small program with no parameters,
g++ test1.cpp
there's no problem. But, if I try to use C++11 standard,
g++ test1.cpp -std=c++11
I get some errors:
In file included from /usr/include/boost/test/included/unit_test.hpp:19:0,
from test1.cpp:2: /usr/include/boost/test/impl/debug.ipp: En la función ‘const char* boost::debug::{anónimo}::prepare_gdb_cmnd_file(const boost::debug::dbg_startup_info&)’: /usr/include/boost/test/impl/debug.ipp:426:23: error: ‘::mkstemp’ no se ha declarado
fd_holder cmd_fd( ::mkstemp( cmd_file_name ) );
^ In file included from /usr/include/boost/test/included/unit_test.hpp:19:0,
from test1.cpp:2: /usr/include/boost/test/impl/debug.ipp: En la función ‘bool boost::debug::attach_debugger(bool)’: /usr/include/boost/test/impl/debug.ipp:863:34: error: ‘::mkstemp’ no se ha declarado
fd_holder init_done_lock_fd( ::mkstemp( init_done_lock_fn ) );
^ In file included from /usr/include/boost/test/utils/runtime/cla/dual_name_parameter.hpp:19:0,
from /usr/include/boost/test/impl/unit_test_parameters.ipp:31,
from /usr/include/boost/test/included/unit_test.hpp:33,
from test1.cpp:2: /usr/include/boost/test/utils/runtime/config.hpp: En la función ‘void boost::runtime::putenv_impl(boost::runtime::cstring, boost::runtime::cstring)’: /usr/include/boost/test/utils/runtime/config.hpp:95:51: error: ‘putenv’ no se declaró en este ámbito
putenv( const_cast<char*>( fs.str().c_str() ) );
(The compiler is in spanish)
I'm using:
Cygwin 64 bits
Cygwin's Boost 1.59
Cygwin's G++ 4.9.3
Any help will be welcome. Thanks.
José.-

Looks like it is a Cygwin thing. I could not reproduce it on OpenSUSE 13.2 i586 with Boost 1.54, but got the same result as yours on Cygwin Win32 with Boost 1.57. And, as Bo Persson suggested, also tried std=gnu+11.
As the compiler sayd “not declared” — even if you explicitly include <stdlib.h> which declares both mkstemp and putenv, — it seemed doubtful to me that it was all about C++ language extensions, but rather more like header file issue. Indeed, in Linux we have:
#if defined __USE_MISC || defined __USE_XOPEN_EXTENDED \
|| defined __USE_XOPEN2K8
# ifndef __USE_FILE_OFFSET64
extern int mkstemp (char *__template) __nonnull ((1)) __wur;
# else
# ifdef __REDIRECT
extern int __REDIRECT (mkstemp, (char *__template), mkstemp64)
__nonnull ((1)) __wur;
# else
# define mkstemp mkstemp64
# endif
# endif
# ifdef __USE_LARGEFILE64
extern int mkstemp64 (char *__template) __nonnull ((1)) __wur;
# endif
#endif
But in Cygwin:
#ifndef __STRICT_ANSI__
#ifndef _REENT_ONLY
int _EXFUN(mkstemp,(char *));
#endif
int _EXFUN(_mkstemp_r, (struct _reent *, char *));
#endif
Then I added a couple of #undefs to your program:
#undef __STRICT_ANSI__
#undef _REENT_ONLY
#define BOOST_TEST_MODULE My Test
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_CASE(first_test) { int i = 1; BOOST_CHECK(i == 1); }
And could compile it fine with std=c++11. I have no idea how incorrect and stupid this may be, but at least it produced very similar exe file that only differs by 20 bytes (aside from fingerprint).

Related

Linux C++ compilation error <emmintrin.h>

I am trying to load images using stb_image.h but I am getting two compiler errors in the version of <emmintrin.h> provided by gcc. I figure that there is probably a compiler option that is needed but I haven't been able to find what it is.
Error codes:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/emmintrin.h:1230:10: error: the last argument must be an 8-bit immediate
1230 | return (__m128i)__builtin_ia32_pslldqi128 (__A, __N * 8);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include/emmintrin.h:1224:10: error: the last argument must be an 8-bit immediate
1224 | return (__m128i)__builtin_ia32_psrldqi128 (__A, __N * 8);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Relevant code from <emmintrin.h>:
extern __inline __m128i __attribute__((__gnu_inline__, __always_inline__, __artificial__))
_mm_srli_si128 (__m128i __A, const int __N)
{
return (__m128i)__builtin_ia32_psrldqi128 (__A, __N * 8);
}
extern __inline __m128i __attribute__((__gnu_inline__, __always_inline__, __artificial__))
_mm_slli_si128 (__m128i __A, const int __N)
{
return (__m128i)__builtin_ia32_pslldqi128 (__A, __N * 8);
}
Edit:
It has something to do with spdlog. I removed all includes of spdlog and changed my logging macros to nothing and it compiled successfully
Minimum reproducible example:
main.cpp
#include "pch.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
int main() {
return 0;
}
pch.h:
#include <spdlog/spdlog.h>
cmakelists.txt:
cmake_minimum_required(VERSION 3.22)
project(untitled2)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_COMPILER /usr/bin/g++)
add_executable(untitled2 main.cpp stb_image.h pch.h)
target_precompile_headers(untitled2 PUBLIC pch.h)
add_subdirectory(spdlog)
target_link_libraries(untitled2 spdlog)
It happens when your program isn't compiling with optimizations but emmintrin.h is selecting the optimized versions of _mm_srli_si128 and _mm_slli_si128 because fmt (a dependency of spdlog) defines __OPTIMIZE__ here.
For more information, take a look at https://github.com/nothings/stb/discussions/1432#discussioncomment-4595273.
Possible workarounds:
Disable precompiled headers for the translation unit that is implementing stb_image (for example, moving it to another CMake target (if CMake is being used))
Undefine __OPTIMIZE__ on the translation unit that is implementing stb_image:
#if defined(__GNUC__) && !defined(NDEBUG) && defined(__OPTIMIZE__)
#warning "Undefing __OPTIMIZE__ because of fmt"
#undef __OPTIMIZE__
#endif
Feel free to remove the generated warning if you want.

How to undefine a defined macro to avoid redefinition error?

I am trying to compile multiple thirdparty library gist and pthread on windows10 using mingw32_make.
However, i am getting redefinition error coming from line "#define PTW32_LEVEL 1".
Q1) Is it that the "#undef PTW32_LEVEL" didn't work at all? That doesn't sound right.
I have already looked at this and this "PTW32_LEVEL" isn't predefined by compiler as defined here.
Q2) what am i missing here? possible workarounds?
#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309
#undef PTW32_LEVEL
#define PTW32_LEVEL 1
/* Include 1b, 1c and 1d */
#endif
There are other redefinitions as well but the summary is same.
Error:
make
gcc -c -Wall -O3 -g gist.c -ID:\prjs\im-similarity\repo\pthreads-w32-2-9-1-release\Pre-built.2\include -DUSE_GIST -DSTANDALONE_GIST
In file included from gist.c:15:0:
D:\prjs\im-similarity\repo\pthreads-w32-2-9-1-release\Pre-built.2\include/pthread.h:108:0: warning: "PTW32_LEVEL" redefined
#define PTW32_LEVEL PTW32_LEVEL_MAX
D:\prjs\im-similarity\repo\pthreads-w32-2-9-1-release\Pre-built.2\include/pthread.h:95:0: note: this is the location of the previous definition
#define PTW32_LEVEL 1
In file included from D:\prjs\im-similarity\repo\pthreads-w32-2-9-1-release\Pre-built.2\include/pthread.h:299:0,
from gist.c:15:
D:\prjs\im-similarity\repo\pthreads-w32-2-9-1-release\Pre-built.2\include/sched.h:64:0: warning: "PTW32_SCHED_LEVEL" redefined
#define PTW32_SCHED_LEVEL PTW32_SCHED_LEVEL_MAX
D:\prjs\im-similarity\repo\pthreads-w32-2-9-1-release\Pre-built.2\include/sched.h:51:0: note: this is the location of the previous definition
#define PTW32_SCHED_LEVEL 1
In file included from gist.c:15:0:
D:\prjs\im-similarity\repo\pthreads-w32-2-9-1-release\Pre-built.2\include/pthread.h:320:8: error: redefinition of 'struct timespec'
struct timespec {
^~~~~~~~
In file included from D:\prjs\im-similarity\repo\pthreads-w32-2-9-1-release\Pre-built.2\include/pthread.h:219:0,
from gist.c:15:
c:\mingw\include\time.h:115:8: note: originally defined here
struct timespec
^~~~~~~~
Makefile:17: recipe for target 'gist.o' failed
make: *** [gist.o] Error 1
Header:
#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309
#undef PTW32_LEVEL
#define PTW32_LEVEL 1
/* Include 1b, 1c and 1d */
#endif
#if defined(INCLUDE_NP)
#undef PTW32_LEVEL
#define PTW32_LEVEL 2
/* Include Non-Portable extensions */
#endif
#define PTW32_LEVEL_MAX 3
#if ( defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112 ) || !defined(PTW32_LEVEL)
#define PTW32_LEVEL PTW32_LEVEL_MAX
/* Include everything */
#endif

Compilation error: `‘error_category’ does not name a type` with g++ 6.3.0

I try to compile this C++/Python library https://bitbucket.org/fluiddyn/fluidfft
If mpi4py is installed, it works well.
If mpi4py is not installed, code that does not use MPI cannot be compiled.
An error is raise during the compilation of a Cython file. The error is long and it starts by:
In file included from /usr/include/c++/6/bits/ios_base.h:46:0,
from /usr/include/c++/6/ios:42,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from src_cpp/base/base_fft.h:10,
from fluidfft/fft2d/fft2d_with_fftw1d.cpp:543:
/usr/include/c++/6/system_error:143:31: error: ‘error_category’ does not name a type
error_code(int __v, const error_category& __cat) noexcept
^~~~~~~~~~~~~~
/usr/include/c++/6/system_error:152:27: error: ‘error_category’ does not name a type
assign(int __v, const error_category& __cat) noexcept
^~~~~~~~~~~~~~
/usr/include/c++/6/system_error:172:11: error: ‘error_category’ does not name a type
const error_category&
^~~~~~~~~~~~~~
/usr/include/c++/6/system_error:191:11: error: ‘error_category’ does not name a type
const error_category* _M_cat;
[...]
I guess it could be a C++11 problem (http://en.cppreference.com/w/cpp/error/error_category) but I don't see how to solve the problem.
The compilation command is
g++ -pthread -fwrapv -O3 -Wall -Wno-unused-result -Wsign-compare -Wno-unused-result -Wsign-compare -fwrapv -O3 -Wall -fPIC -I/home/users/me/opt/miniconda3/include/python3.6m -I/home/users/me/opt/miniconda3/include -Isrc_cy -Ifluidfft/fft2d -Ifluidfft/fft3d -Isrc_cpp/base -Isrc_cpp/3d -Isrc_cpp/2d -Iinclude -I/home/users/me/opt/miniconda3/lib/python3.6/site-packages/numpy/core/include -c fluidfft/fft2d/fft2d_with_fftw1d.cpp -o build/temp.linux-x86_64-3.6/fluidfft/fft2d/fft2d_with_fftw1d.o
Edit Minimal, Complete, and Verifiable example
Thanks to Ashwin Vishnu (see https://bitbucket.org/fluiddyn/fluidfft/issues/7/fluidfft-installation-fails-without-mpi4py), I can post a minimal example
/* test.cpp */
#include <Python.h>
#include <string.h>
#include <stdio.h>
#include <cpu.h>
#include <sys/time.h>
#include <complex>
#include <iostream>
int main() {
std::cout<<"Hello world";
return 0;
}
compiled from fluidfft directory as follows:
g++ $(python-config --include) -Iinclude/ test.cpp
If we comment out cpu.h include, there are no errors.
The file cpu.h was taken from the pyfftw code: https://github.com/pyFFTW/pyFFTW/blob/master/include/cpu.h
This happens because the package fluidfft's Cython source files relied on a C++ header file cpu.h wherein the following preprocessor lines caused problems:
#if __STDC_VERSION__ >= 199901L
/* "inline" is a keyword */
#else
# define inline
#endif
My guess is the newer g++ compilers are strict on redefining reserved keywords. Following hints from an essay on inline functions, this block of code was replaced with:
#if __STDC_VERSION__ >= 199901L
/* "inline" is a keyword */
#else
# define INLINE
#endif
#ifndef INLINE
# if __GNUC__ && !__GNUC_STDC_INLINE__
# define INLINE static inline
# else
# define INLINE inline
# endif
#endif

Easylogging 8.91 not compling without c++11

I want to use the library without C++11 but it won't compile for me:
(Theoretically it should as per documentation #http://easylogging.muflihun.com:
"For lower version of C++ (non-C++11), please consider using Easylogging++ v8.91. ")
error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
File structure:
./Main.cpp
./logger/easylogging++.h
Contents of Main.cpp:
#include "logger/easylogging++.h"
_INITIALIZE_EASYLOGGINGPP
using namespace std;
int main(int argc, char* argv[]) {
LINFO << "This is my first log";
return 0;
}
../src/logger/easylogging++.h: In function ‘std::string easyloggingpp::internal::threading::getCurrentThreadId()’:
../src/logger/easylogging++.h:691:16: error: ‘std::this_thread’ has not been declared
ss << std::this_thread::get_id();
Compiler: gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1), OS: Ubuntu 14.04 LTS
As T.C. suggested in the solution is to change this section of code at the top of easylogging++.h:
#if defined(__GNUC__)
# define _ELPP_GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL__)
# if defined(__GXX_EXPERIMENTAL_CXX0X__)
# define _ELPP_CXX0X 1
# elif (_ELPP_GCC_VERSION >= 40801)
# define _ELPP_CXX11 1
# endif // defined(__GXX_EXPERIMENTAL_CXX0X__)
#endif // defined(__GNUC__)
Changing both _ELPP_CXX0X and _ELPP_CXX11 to 0 will fix the issue.

Compilation Error in g++ 4.3.4 compiler

#include <iostream>
#include <string.h>
char* basename(const char* filname);
int main()
{
return 0;
}
char *basename(const char* filename)
{
char* base = (char *)filename;
return base ;
}
compiling on g++ 4.1.2 20070115 (SUSE 10): No issue
compiling on g++ 4.3.4 (SUSE 11) gives following error
fileName : 9 :error:declaration of char* basename(const char*) throws different exception
fileName:3:error: from previous declaration char* basename(const char*) throw () .
Kindly tell me why this is happening , Is there is any Interface changed in g++ between these two release (if I remove inclusion of string.h then compilation success on both version of g++ ,Is any Interface change in string.h).
looks like basename already defined in string.h
# ifndef basename
/* Return the file name within directory of FILENAME. We don't
declare the function if the `basename' macro is available (defined
in <libgen.h>) which makes the XPG version of this function
available. */
# ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
extern "C++" char *basename (char *__filename)
__THROW __asm ("basename") __nonnull ((1));
extern "C++" __const char *basename (__const char *__filename)
__THROW __asm ("basename") __nonnull ((1));
# else
extern char *basename (__const char *__filename) __THROW __nonnull ((1));
# endif
# endif
It seems the name basename already exists in string.h:
http://ideone.com/Q9xSw - gives error (as it is exactly your code).
http://ideone.com/s0HIX compiles fine if you comment #include <string.h>