3

我的项目使用 2 个不同的 C++ 编译器,g++ 和 nvcc(cuda 编译器)。我注意到从 nvcc 对象文件抛出的异常没有在 g++ 对象文件中捕获。

C++ 异常在同一台机器上应该是二进制兼容的吗?什么会导致这种行为?

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

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

其他一切似乎都有效(C++ 对象、运算符)。老实说,我不太了解异常,所以上面的代码可能有错误。

4

3 回答 3

7

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.

于 2010-04-12T02:58:52.760 回答
6

nvcc 是普通 c++ 编译器的包装器,基本上是将 cuda 语法转换为可编译的东西的预处理器。您可以看到它与--verbose标志一起使用的编译器。

例如在我的机器上编译

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

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

使用此处列出的相同编译器/标志应该为您提供二进制兼容性

于 2010-04-12T03:34:12.987 回答
2

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

于 2010-04-12T03:00:41.163 回答