Re-declaration of cmath functions in CUDA's math_functions.h - c++

I included “cuda_runtime.h” in my project. It then raises compilation error:
In file included from /usr/local/cuda/include/common_functions.h:235:0,
from /usr/local/cuda/include/cuda_runtime.h:116,
from /usr/local/include/caffe2/core/common_gpu.h:7,
from /home/vpe.cripac/projects/LaS-VPE-Platform/src/native/DeepMAR_deploy/src/DeepMARCaffe2Utils.cpp:8:
/usr/local/cuda/include/math_functions.h:9421:99: error: redeclaration ‘float std::tanh(float)’ differs in ‘constexpr’
extern __DEVICE_FUNCTIONS_DECL__ __cudart_builtin__ __CUDA_CONSTEXPR__ float __cdecl tanh(float);
In file included from /usr/local/cuda/include/math_functions.h:8809:0,
from /usr/local/cuda/include/common_functions.h:235,
from /usr/local/cuda/include/cuda_runtime.h:116,
from /usr/local/include/caffe2/core/common_gpu.h:7,
from /home/vpe.cripac/projects/LaS-VPE-Platform/src/native/DeepMAR_deploy/src/DeepMARCaffe2Utils.cpp:8:
/usr/include/c++/4.8.2/cmath:520:3: error: from previous declaration ‘constexpr float std::tanh(float)’
tanh(float __x)
This happens both on Ubuntu and CentOS 7, using GCC 4.8.5 or 5.3.1.
Should I include any other header or define any macro before “cuda_runtime.h”?

You should never import math_functions.h into plain host code, and with g++ 4.8.5, I get an #error generated from within the header file if I try to do so. If you import cuda_runtime.h into your host code correctly, it will never import math_functions.h incorrectly:
$ cat test_math.cpp
#include <iostream>
#include <cmath>
#ifdef __BREAK_ME__
#include <math_functions.h>
#else
#include <cuda_runtime.h>
#endif
int main()
{
const float val = 0.123456789f;
std::cout << "tanh(" << val << ")=" << std::tanh(val) << std::endl;
return 0;
}
$ g++ -I/opt/cuda-8.0/include test_math.cpp
$ g++ -I/opt/cuda-8.0/include -D__BREAK_ME__ test_math.cpp
In file included from /opt/cuda-8.0/include/math_functions.h:10055:0,
from test_math.cpp:5:
/opt/cuda-8.0/include/crt/func_macro.h:50:2: error: #error -- incorrect inclusion of a cudart header file
#error -- incorrect inclusion of a cudart header file
^

Related

extern main declaration from bsplib returns error

Im setting up the bsplib (https://github.com/Zefiros-Software/BSPLib) on a windows system (in VS Code) using WSL. When compiling I get the error message:
test.cpp:4:5: error: conflicting declaration of C function ‘int main()’
4 | int main()
| ^~~~
In file included from /mnt/d/study/software/bsp/include/bsp/bspExt.h:30,
from /mnt/d/study/software/bsp/include/bsp/bsp.h:34,
from test.cpp:2:
/mnt/d/study/software/bsp/include/bsp/bspClass.h:59:12: note: previous declaration ‘int main(int, char**)’
59 | extern int main(int argc, char **argv);
The program is used is just a bare example for BSP:
#include <iostream>
#include "bsp/bsp.h"
int main()
{
bsp_begin(bsp_nprocs());
int s = bsp_pid();
int p = bsp_nprocs();
printf("Hello World from processor %d / %d", s, p);
bsp_end();
return 0;
}
Compiled with:
g++ -I/mnt/d/study/software/bsp/include -g -lpthread -o main test.cpp
To my (quite limited) knowledge, the 'extern' in the header file should prevent the compiler from labelling the main as 'duplicate' of some sort. Im mostly interested in some of BSPs functionalities as part of a class of mine, that sadly does not include any support on the installation. What I've done so far:
Copied the include files from the repo
Added the include path to the compilation (-I Flag) and the -lpthread as instructed by the class script
Added the include path to the configuration (c_cpp_properties.json) [tested both with and without this, no difference]
Due to the many possible sources of that error (program, compiler, wsl, library, configuration, vs code, my stupidity) I cant determine where I am mistaken, nor am I able to find online resources to that combination.

cpp error while testing new boost install in cygwin

I have installed boost by downloading and unzipping the boost_1_67_0.tar.bz2 file in the following location:
/cygdrive/c/no_admin_programs/boost_1_67_0
I have entered the following into a file test.cpp:
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(
in(std::cin), in(), std::cout << (_1 * 3) << " ");
}
And entered the following in the terminal:
c++ -I /cygdrive/c/no_admin_programs/boost_1_67_0 test.cpp
As a result, the following error is shown:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/cstdlib:75:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_algo.h:59,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/algorithm:62,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/core/swap.hpp:25,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/utility/swap.hpp:15,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/tuple/detail/tuple_basic.hpp:40,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/tuple/tuple.hpp:28,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/lambda/core.hpp:30,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/lambda/lambda.hpp:14,
from test.cpp:1:
/usr/include/stdlib.h:91:7: error: expected initializer before ‘__alloc_size2’
__alloc_size2(1, 2) _NOTHROW;
^~~~~~~~~~~~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/cstdlib:75:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_algo.h:59,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/algorithm:62,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/core/swap.hpp:25,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/utility/swap.hpp:15,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/tuple/detail/tuple_basic.hpp:40,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/tuple/tuple.hpp:28,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/lambda/core.hpp:30,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/lambda/lambda.hpp:14,
from test.cpp:1:
/usr/include/stdlib.h:144:63: error: expected initializer before ‘__alloc_size2’
void *reallocarray(void *, size_t, size_t) __result_use_check __alloc_size2(2, 3);
^~~~~~~~~~~~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_algo.h:59:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/algorithm:62,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/core/swap.hpp:25,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/utility/swap.hpp:15,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/tuple/detail/tuple_basic.hpp:40,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/tuple/tuple.hpp:28,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/lambda/core.hpp:30,
from /cygdrive/c/no_admin_programs/boost_1_67_0/boost/lambda/lambda.hpp:14,
from test.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/cstdlib:138:11: error: ‘::calloc’ has not been declared
using ::calloc;
^~~~~~
I've tried including <cstdlib> to no avail. Any assistance would be much appreciated.
if you also install the boost package alread provided on cygwin
there is no need to try to use boost_1_67_0.tar.bz2
$ cygcheck -c libboost-devel
Cygwin Package Information
Package Version Status
libboost-devel 1.66.0-1 OK
your program works fine with just
$ c++ test.cpp
$ ./test.exe
4
12 5
15 7
21 9
27

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

Why does my c++ compiler seem to be compiling in c

I am using geany (code::blocks wouldnt run my programs) as a compiler to compile a simple c++ program with one class. I am on Linux Mint 17 on a Dell Vostro 1500. Compiling works fine with both .cpp files, but the header file gives this error:
gcc -Wall "Morgan.h" (in directory: /home/luke/Documents/Coding/Intro#2)
Morgan.h:5:1: error: unknown type name ‘class’
class Morgan
^
Morgan.h:6:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
Compilation failed.
This is the main.cpp :
#include <iostream>
#include "Morgan.h"
using namespace std;
int main()
{
Morgan morgObject;
morgObject.sayStuff();
return 0;
}
This is the Header file (Morgan.h):
#ifndef MORGAN_H
#define MORGAN_H
class Morgan
{
public:
Morgan();
void sayStuff();
protected:
private:
};
#endif // MORGAN_H
And this is the class (Morgan.cpp):
#include <iostream>
#include "Morgan.h"
using namespace std;
Morgan::Morgan()
{
}
void Morgan::sayStuff(){
cout << "Blah Blah Blah" << endl;
}
I really do not know what is going wrong, so any help would be appreciated. I copy and pasted the same code into a windows compiler and it worked fine, so it might just be the linux.
also when I run the main.cpp this is what shows:
"./geany_run_script.sh: 5: ./geany_run_script.sh: ./main: not found"
You don't compile .h files. Try g++ -Wall main.cpp Morgan.cpp
Your issue is that you are compiling C++ code with a C compiler (GCC). The command you are looking for is g++. The complete command that would compile your code is:
g++ -Wall -o run.me main.cpp Morgan.cpp
If a file is included (In your case the Morgan.h file, you do not need to explicitly compile it. )

Variable has incomplete type in class definition?

I've got this class definition in a .h file, and the implementation in a .cpp file. When I try to compile this, the header file gives some errors and warnings:
/home/don/BerthaApex/apex/src/lib/apexmain/apexloader.h:6: error: variable 'APEX_EXPORT ApexLoader' has initializer but incomplete type
class APEX_EXPORT ApexLoader
^
/home/don/BerthaApex/apex/src/lib/apexmain/apexloader.h:6: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
/home/don/BerthaApex/apex/src/lib/apexmain/apexloader.h:9: error: expected primary-expression before 'public'
public:
The code in which this error occurs is:
#ifndef _APEXLOADER_H
#define _APEXLOADER_H
#include "global.h"
class APEX_EXPORT ApexLoader
{
public:
int Load( int argc, char *argv[]);
};
#endif
With the "class APEX_EXPORT ApexLoader" being the line with the error and the warning.
The APEX_EXPORT is defined in a header file that is included from this same file.
EDIT:
The APEX_EXPORT is defined in "global.h" as follows:
#ifdef APEX_MAKEDLL
#define APEX_EXPORT APEX_EXPORT_DECL
#else
#define APEX_EXPORT APEX_IMPORT_DECL
#endif
Does anyone know why these errors are there? And how can I get rid of them?
Thank you in advance!
Compiler: gcc 4.8.4
OS: Ubuntu 14.04
My psychic debugging skills tell me that APEX_EXPORT isn't #defined and thus the compiler thinks you're trying to declare a variable of that type.
If you think you've included all the right headers the best way to go is to just run the preprocessor on your source file and see what it generates (for example g++ -E).