Boost logo

Boost Users :

Subject: [Boost-users] [Exception] Duplicate symbol error when using errinfo_api_function in static library
From: Georg Leidinger (georg.leidinger_at_[hidden])
Date: 2013-10-01 05:16:17


Dear readers,

I encountered a problem in my project when using *Boost.Exception* with the
predefined types errinfo_xyz in a library project. I have two classes
ClassA and ClassB in my library which both throw a boost::exception with
errinfo_api_function type in their implementation file (.cpp) These both
classes are compiled into a static library with the C/C++-Flags
-fvisibility=hidden.
Then I have a main.cpp which creates an instance of ClassA and ClassB and
links to the library which contains ClassA.o and ClassB.o. When I compile
the main.cpp I get the linker error duplicate symbol for the
errinfo_api_function object in ClassA.o and ClassB.o. If I remove the
visibility=hidden flag, it links without problems. I am not an expert on
the compiler flags, but I saw that many libraries use the visibility=hidden
flag so I think it would be good, if my lib could also use it.

For a better description I will show the sources for my project:

ClassA.h
-------------------------------------------------------------------------------------------------
#pragma once

#include <boost/exception/all.hpp>

struct my_errorA : virtual std::exception, virtual boost::exception {};

class ClassA
{
public:
  ClassA() {};
  virtual ~ClassA() {};

  virtual void doSomething();
};

ClassA.cpp
-------------------------------------------------------------------------------------------------
#include "ClassA.h"

void ClassA::doSomething()
{
  BOOST_THROW_EXCEPTION( my_errorA() <<
     boost::errinfo_api_function("doSomething") );
}

ClassB.h
-------------------------------------------------------------------------------------------------
#pragma once
#include <boost/exception/all.hpp>

struct my_errorB : virtual std::exception, virtual boost::exception {};

class ClassB
{
public:
  ClassB() {};
  virtual ~ ClassB() {};

  virtual void doSomething();
};

ClassB.cpp
-------------------------------------------------------------------------------------------------
#include "ClassB.h"

void ClassB::doSomething()
{
  BOOST_THROW_EXCEPTION( my_errorB() <<
     boost::errinfo_api_function("doSomething") );
}

main.cpp
-------------------------------------------------------------------------------------------------
#include "ClassA.h"
#include "ClassB.h"

int main(int argc, const char * argv[])
{
  ClassA A;
  ClassB B;

  return 0;
}

The Linker output:
-----------------------------------------------------------------------------------------------------------------
*Ld
/Users/georg/Library/Developer/Xcode/DerivedData/ExceptionTest-dcnvciwqbwqvciaokkjjaqchftll/Build/Products/Debug/ExceptionTest
normal i386
    cd /Users/georg/work/ExceptionTest
    setenv MACOSX_DEPLOYMENT_TARGET 10.8
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
-arch i386 -isysroot
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk
-L/Users/georg/Library/Developer/Xcode/DerivedData/ExceptionTest-dcnvciwqbwqvciaokkjjaqchftll/Build/Products/Debug
-F/Users/georg/Library/Developer/Xcode/DerivedData/ExceptionTest-dcnvciwqbwqvciaokkjjaqchftll/Build/Products/Debug
-filelist
/Users/georg/Library/Developer/Xcode/DerivedData/ExceptionTest-dcnvciwqbwqvciaokkjjaqchftll/Build/Intermediates/ExceptionTest.build/Debug/ExceptionTest.build/Objects-normal/i386/ExceptionTest.LinkFileList
-mmacosx-version-min=10.8
/Users/georg/Library/Developer/Xcode/DerivedData/ExceptionTest-dcnvciwqbwqvciaokkjjaqchftll/Build/Products/Debug/libExceptionLib.a
-o
/Users/georg/Library/Developer/Xcode/DerivedData/ExceptionTest-dcnvciwqbwqvciaokkjjaqchftll/Build/Products/Debug/ExceptionTest
duplicate symbol __ZTIPN5boost21errinfo_api_function_E in:

/Users/georg/Library/Developer/Xcode/DerivedData/ExceptionTest-dcnvciwqbwqvciaokkjjaqchftll/Build/Products/Debug/libExceptionLib.a(ClassA.o)

/Users/georg/Library/Developer/Xcode/DerivedData/ExceptionTest-dcnvciwqbwqvciaokkjjaqchftll/Build/Products/Debug/libExceptionLib.a(ClassB.o)
duplicate symbol __ZTSPN5boost21errinfo_api_function_E in:

/Users/georg/Library/Developer/Xcode/DerivedData/ExceptionTest-dcnvciwqbwqvciaokkjjaqchftll/Build/Products/Debug/libExceptionLib.a(ClassA.o)

/Users/georg/Library/Developer/Xcode/DerivedData/ExceptionTest-dcnvciwqbwqvciaokkjjaqchftll/Build/Products/Debug/libExceptionLib.a(ClassB.o)
ld: 2 duplicate symbols for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see
invocation)*
-----------------------------------------------------------------------------------------------------------------

I hope I described the problem well enough and I hope someone can explain
what the problem is? Thank a lot and

best regards
Georg



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net