This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Symbol visibility
- From: DÃniel Varga <vargad88 at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Sat, 12 Jan 2013 11:44:45 +0100
- Subject: Symbol visibility
Hi,
I try to understand symbol visibility better. The GCC Wiki
(http://gcc.gnu.org/wiki/Visibility) has a section about "Problems
with C++ exceptions". According to GCC Wiki it is possible the have
_runtime_ error because of not exported exceptions. Runtime errors
without compile time error/warning is quite dangerous so I tried to
understand the problem better. I made some experiments but I still
can't reproduce it. Any ideas how to reproduce the problem?
The Wiki mentions three library using each other, so I made three small library.
I run the following commands:
Exception class without vtable (works as expected):
make
./dsouser
Exception class with vtable but it does not exported (does not even compile):
make HAS_VIRTUAL=1
Exception class exported vtable (works as expected):
make HAS_VIRTUAL=1 EXCEPTION_VISIBLE=1
./dsouser
Makefile:
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;
}
Thanks,
Dani