我试图更好地理解符号可见性。GCC Wiki ( http://gcc.gnu.org/wiki/Visibility ) 有一个关于“C++ 异常问题”的部分。根据 GCC Wiki,由于未导出异常,可能会出现运行时错误。没有编译时错误/警告的运行时错误非常危险,所以我试图更好地理解这个问题。我做了一些实验,但我仍然无法重现它。任何想法如何重现问题?
Wiki 中提到了三个库相互使用,所以我做了三个小库。
我运行以下命令:
没有 vtable 的异常类(按预期工作):
make
./dsouser
带有 vtable 的异常类,但它不导出(甚至不编译):
make HAS_VIRTUAL=1
异常类导出的 vtable(按预期工作):
make HAS_VIRTUAL=1 EXCEPTION_VISIBLE=1
./dsouser
生成文件:
CXX=g++-4.7.1
CFLAGS=-ggdb -O0 -fvisibility=hidden
ifdef EXCEPTION_VISIBLE
CFLAGS+=-DEXCEPTION_VISIBLE
endif
ifdef HAS_VIRTUAL
CFLAGS+=-DHAS_VIRTUAL
endif
all: dsouser
libmydso.so: mydso.cpp mydso.h
$(CXX) $(CFLAGS) -fPIC -shared -Wl,-soname,$@ -o $@ $<
libmydso2.so: mydso2.cpp mydso.h mydso2.h libmydso.so
$(CXX) $(CFLAGS) -L. -fPIC -shared -Wl,-soname,$@ -o $@ $< -lmydso
libmydso3.so: mydso3.cpp mydso.h mydso2.h mydso3.h libmydso2.so
$(CXX) $(CFLAGS) -L. -fPIC -shared -Wl,-soname,$@ -o $@ $< -lmydso -lmydso2
dsouser: dsouser.cpp libmydso3.so
$(CXX) $< $(CFLAGS) -L. -o $@ -lmydso -lmydso2 -lmydso3
clean:
rm -f *.so *.o dsouser
.PHONY: all clean
mydso.h:
#ifndef DSO_H_INCLUDED
#define DSO_H_INCLUDED
#include <exception>
#define SYMBOL_VISIBLE __attribute__ ((visibility ("default")))
namespace dso
{
class
#ifdef EXCEPTION_VISIBLE
SYMBOL_VISIBLE
#endif
MyException : public std::exception
{
public:
#ifdef HAS_VIRTUAL
virtual void dump();
#endif
void SYMBOL_VISIBLE foo();
};
}
#endif
mydso.cpp:
#include <iostream>
#include "mydso.h"
namespace dso
{
#ifdef HAS_VIRTUAL
void MyException::dump()
{
}
#endif
void MyException::foo()
{
#ifdef HAS_VIRTUAL
dump();
#endif
}
}
mydso2.h:
#ifndef DSO2_H_INCLUDED
#define DSO2_H_INCLUDED
#define SYMBOL_VISIBLE __attribute__ ((visibility ("default")))
namespace dso2
{
void SYMBOL_VISIBLE some_func();
}
#endif
mydso2.cpp:
#include <iostream>
#include "mydso.h"
#include "mydso2.h"
namespace dso2
{
void some_func()
{
throw dso::MyException();
}
}
mydso3.h:
#ifndef DSO3_H_INCLUDED
#define DSO3_H_INCLUDED
#define SYMBOL_VISIBLE __attribute__ ((visibility ("default")))
namespace dso3
{
void SYMBOL_VISIBLE some_func();
}
#endif
mydso3.cpp:
#include <iostream>
#include "mydso2.h"
#include "mydso3.h"
#include <iostream>
namespace dso3
{
void some_func()
{
try
{
dso2::some_func();
} catch (std::exception e)
{
std::cout << "Got exception\n";
}
}
}
dsouser.cpp:
#include <iostream>
#include "mydso3.h"
int main()
{
dso3::some_func();
return 0;
}
谢谢,丹妮