简体   繁体   English

C ++异常二进制兼容性

[英]C++ exceptions binary compatibility

my project uses 2 different C++ compilers, g++ and nvcc (cuda compiler). 我的项目使用2个不同的C ++编译器,g ++和nvcc(cuda编译器)。 I have noticed exception thrown from nvcc object files are not caught in g++ object files. 我注意到从nvcc对象文件抛出的异常没有被g ++对象文件捕获。

are C++ exceptions supposed to be binary compatible in the same machine? C ++异常是否应该在同一台机器上兼容二进制? 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). 其他一切似乎都有效(C ++对象,运算符)。 To be honest I do not know exceptions very well so maybe there is mistake in the code above. 说实话,我不太了解异常,所以上面的代码可能有错误。

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. 很抱歉在一个晚上给你两个“不”的答案,但是“否”,C ++异常(或那个类的)没有标准的二进制布局。 Attempting to use C++ classes/exceptions between two differing compilers breaks the One Definition Rule . 尝试在两个不同的编译器之间使用C ++类/异常会破坏一个定义规则

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. 您可以通过仅允许目标文件之间的C API(因为C 具有标准ABI - 应用程序二进制接口)来解决此问题,或者您可以使用一个编译器或另一个编译器编译所有代码。 I'm not sure if the last bit is possible with NVCC, however. 但是,我不确定NVCC是否可以使用最后一位。

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. 回答问题编辑: 其他一切似乎都有效(C ++对象,运算符) :在绝大多数情况下,有很多东西似乎都有效。 That does not mean they do not invoke undefined behavior. 这并不意味着他们不会调用未定义的行为。

nvcc is a wrapper around a normal c++ compiler, and is basically a preprocessor to convert the cuda syntax into something compilable. nvcc是普通c ++编译器的包装器,基本上是将cuda语法转换为可编译的东西的预处理器。 You can see what compiler it uses with the --verbose flag. 您可以使用--verbose标志查看它使用的编译器。

For instance on my machine compiling 例如在我的机器编译上

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

with nvcc -v gives nvcc -v给出

#$ _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 使用此处列出的相同编译器/标志应该为您提供二进制兼容性

The C++ standard doesn't specify the binary form of anything, let alone exceptions. C ++标准没有指定任何东西的二进制形式,更不用说异常了。 There's no reason you should expect this to work. 没有理由你期望这个工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM