[CMake] Convert libtool convenience libraries to cmake

Andreas Pokorny andreas.pokorny at gmail.com
Wed Apr 15 04:01:10 EDT 2009


Hi,

2009/4/15 Yevgen Muntyan <ymuntyan at gmail.com>:
> How do you do it, do you manually add -fPIC or something to compiler
> flags? Could you
> show the CMakeLists.txt?

We only have special values for gcc:

IF(CMAKE_COMPILER_IS_GNUCC)

# PS: cannot remember why we do that?
   SET(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "")
   SET(CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG "")

## HACK
## We build static libraries, with the intention to link them into shared object
## files (google for convenience libraries). It would be better to add that
## flag only to those static libs that shall be part of dynamic libs. Therefore
## we would need a wrapper around ADD_LIBRARY, or something similar.
For now most
## of our code is part of shared object files and DLLs.

   FORCE_ADD_FLAGS(CMAKE_C_FLAGS "-Wall -fPIC")
   FORCE_ADD_FLAGS(CMAKE_CXX_FLAGS "-Wall -fPIC -fno-exceptions -fno-rtti")

ENDIF(CMAKE_COMPILER_IS_GNUCC)

the macro FORCE_ADD_FLAGS is taken from here:
http://www.cmake.org/Wiki/CMakeMacroForceAddFlags

We do not need special configuration values for msvc. Then we build
our components as static libraries lets say component_1 component_2
and component_3 shall end up in libFoo.so.

ADD_LIBRARY(FOO SHARED ${my_FOO_src})
TARGET_LINK_LIBRARIES(FOO component_1 component_2 component_3)

So now you only have to ensure that the symbols from the static libs
end up in your library. To achieve that, the source files in
my_FOO_src have to refer enough symbols in component_1, 2 and 3. We
have the luck that most of our library interfaces consist out of a set
of c-symbols which are used inside a plugin system. So my_FOO_src
contains a dummy.cpp, which looks like that:
int dummy()
{
    create_plugin();
    set_interface(NULL,0);
    destroy_plugin();
}

dummy is never called, it is just used to tell the linker that these
symbols are needed. Instead of that you could also intialize an array
of function or member method pointers or do other fancy stuff like
linker scripts...

Note that the order of static libraries is vital for the gcc linker.
If the static library component_2 has undefined symbols defined in
component_1, you have to swap these two libaries in the
TARGET_LINK_LIBRARIES command.

kind regards
Andreas Pokorny


More information about the CMake mailing list