C++ exceptions binary compatibility

独自空忆成欢 提交于 2019-12-12 14:16:43

问题


my project uses 2 different C++ compilers, g++ and nvcc (cuda compiler). I have noticed exception thrown from nvcc object files are not caught in g++ object files.

are C++ exceptions supposed to be binary compatible in the same machine? what can cause such behavior?

try { kernel_= new cuda:: Kernel(); }
catch (...) { kernel_= NULL; }

// nvcc object
cuda:: Kernel:: Kernel () {
  ...
  if (! impl_) throw;
}

everything else seems to work (C++ objects, operators). To be honest I do not know exceptions very well so maybe there is mistake in the code above.


回答1:


nvcc is a wrapper around a normal c++ compiler, and is basically a preprocessor to convert the cuda syntax into something compilable. You can see what compiler it uses with the --verbose flag.

For instance on my machine compiling

// test.cpp
int main(){return 0;}

with nvcc -v gives

#$ _SPACE_= 
#$ _MODE_=DEVICE
#$ _HERE_=/usr/local/cuda/bin
#$ _THERE_=/usr/local/cuda/bin
#$ TOP=/usr/local/cuda/bin/..
#$ PATH=/usr/local/cuda/bin/../open64/bin:/usr/local/cuda/bin/../bin:/Library/Frameworks/Python.framework/Versions/Current/bin:/Users/me/bin:/usr/local/aspell/bin:/usr/local/noweb:/usr/local/icon/bin:/usr/local/dmd/bin:/usr/local/cuda/bin:/usr/local/sed/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
#$ INCLUDES="-I/usr/local/cuda/bin/../include"  
#$ LIBRARIES=  "-L/usr/local/cuda/bin/../lib" -lcudart
#$ CUDAFE_FLAGS=
#$ OPENCC_FLAGS=
#$ PTXAS_FLAGS=
#$ gcc -c -x c++ "-I/usr/local/cuda/bin/../include"   -I. -m32 -malign-double -o "/tmp/tmpxft_000010af_00000000-1_test.o" "test.cpp" 
#$ g++ -m32 -malign-double -o "a.out" "/tmp/tmpxft_000010af_00000000-1_test.o"   "-L/usr/local/cuda/bin/../lib" -lcudart

Using the same compiler/flags as listed here should give you binary compatibility




回答2:


Sorry to give you two "no" answers in a single night, but "No", C++ exceptions (or classes for that matter) have no standard binary layout. Attempting to use C++ classes/exceptions between two differing compilers breaks the One Definition Rule.

You can work around this by only allowing a C API between the object files (because C has a standard ABI - Application Binary Interface), or you can compile all your code with one compiler or the other. I'm not sure if the last bit is possible with NVCC, however.

In response to question edit: everything else seems to work (C++ objects, operators): There are lots of things that seem to work in the vast majority of cases. That does not mean they do not invoke undefined behavior.




回答3:


The C++ standard doesn't specify the binary form of anything, let alone exceptions. There's no reason you should expect this to work.



来源:https://stackoverflow.com/questions/2619630/c-exceptions-binary-compatibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!