Handling unit test with C++ visibility=hidden

Recently I started working on the libbash project. I will write several articles talking about it in future. Now I’d just like to write something about the problem we encountered during development.

Our project will be a shared library. The C++ visibility support could improve the overall performance. Put simply, it hides most of the ELF symbols which would have previously (and unnecessarily) been public. Well it’s good for the library, it’s not good for unit tests(namely gtest) because they need to know the symbols.

Of course we don’t want  the unit tests to be part of our library. So we need to find some way to let the unit test know the symbols and separate them into different automake targets. Our first solution is to use hidden visibility for the library and create an internal target with default visibility for the unit test. However, that requires compiling the source code twice. Finally Petteri Räty come with a solution (He is too busy to write a post :P):

lib_LTLIBRARIES = libcppbash.la
libcppbash_la_SOURCES = blah
libcppbash_la_CXXFLAGS = $(AM_CXXFLAGS) -fvisibility=hidden -fvisibility-inlines-hidden

cppunittests_SOURCES = blah
cppunittests_LDADD = libcppbash.la $(GTEST_LIBS)
cppunittests_LDFLAGS = -static

Here’s his explanation:
libtool by default builds both a shared and a static library for our project. This is why you see it building things twice (with PIC and without). Giving -static to libtool is just telling it to use the static version. Using the static version means everything ends up in the unit test binaries.


, , ,

  1. #1 by Pacquiao Mosley on April 20, 2011 - 12:03 pm

    5 star! wonderful codes…

  2. #2 by Greger on April 27, 2011 - 7:26 pm

    Thanks. Works excellent!

  3. #3 by Asp.net Programmer on March 19, 2012 - 12:42 am

    Thanks. Works for me 2.

  4. #4 by Dunia Komputer on March 29, 2012 - 5:52 am

    very useful coding.. Thanks anyway

  5. #5 by useful on July 6, 2013 - 11:02 pm

    very useful.

  6. #6 by Jasa Pembuatan Website on July 6, 2013 - 11:04 pm

    Thanks for share..

    Invite my website: Jasa Pembuatan Website

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: