[CMake] g++ -frepo and cmake

Brad King brad.king at kitware.com
Wed Jun 14 09:56:48 EDT 2006


Manuel Klimek wrote:
> Hi,
> 
> i'm trying to use cmake with g++ to build template using code
> with -frepo for shared libraries.
> With -frepo g++ automatically includes the object code only for
> template instantiations that are needed at link time. If
> you link an executable with g++, say
> g++ -frepo -o hello hello.o main.o
> and hello.cc uses templates, g++ will insert missing template
> instantiations into hello.o until the executable links.
> Now this doesn't work for shared libraries:
> g++ -frepo -shared -o libhello.so hello.o
> will simply compile without adding the extra template instantions
> into hello.o.
> The gcc mailing list suggests the following solution:
> First compile a dummy 'closure' executable, which fails:
> g++ -frepo -o libhello_closure hello.cc
> (fails complaining about missing symbols)
> Afterwards, hello.o contains all the required symbols.
> Then
> g++ -frepo -shared -o libhello.so hello.o
> will yield a library with all symbols as expected.
> At the moment I implement this solution using a wrapper
> script around g++, which simply looks for -shared, and in
> case of -shared first performs the closure step and than
> compiles the whole executable.
> Because an additional shell indirection slows down my
> build process, I wondered if I can integrate the -frepo
> concept into my cmake process.
> I tried:
> - using ADD_EXECUTABLE (with a simple closure.cpp containing a main(...)):
>   fails, because in linux symbols are only required at executable
>   link time, not at library link time
> - using ADD_CUSTOM_COMMAND:
>   I don't know how to get the command line cmake would use to create
>   a library, so I can't write a custom command - and because g++ would
>   return an error code, the build wouldn't work anyways, would it?
> 
> So the question is: do you think using a wrapper script is the
> only way, or did I miss something and I can do it with cmake?

Look at the current setting of CMAKE_CXX_CREATE_SHARED_LIBRARY:

   MESSAGE("CMAKE_CXX_CREATE_SHARED_LIBRARY = 
[${CMAKE_CXX_CREATE_SHARED_LIBRARY}]")

This is the variable CMake uses to construct the link rule for the 
shared library.  You could try adding your wrapper shell script to it:

SET(CMAKE_CXX_CREATE_SHARED_LIBRARY
   "/path/to/wrapper/script ${CMAKE_CXX_CREATE_SHARED_LIBRARY}")

Then your script would be called only for linking instead of for every 
object file built.

Otherwise you could get some improvement by replacing your wrapper 
script with a small C program that would load faster than a shell script 
and then exec g++.

-Brad


More information about the CMake mailing list