[CMake] installing source (or object) files

Kris Thielemans kris.f.thielemans at gmail.com
Thu Jan 19 16:23:49 EST 2017


Thanks Mike

The motivation for linking explicitly with the object file was in the PS at the bottom, although only very brief of course. 

The registry.cxx file has static variables with constructors with side effects (they add entries to a std:::map that registers "factories"). Therefore, registry.cxx refers to stuff in mylib, but neither wonderful_program.cxx nor mylib refers to stuff in registry.cxx. If I stick registry.cxx into a library (I could have used mylib for instance), the linker happily removes it from the wonderful_program executable (as it thinks its an irrelevant object file). Result: the std::map never gets initialised. The only way (that I know) to fix this is to link explicitly with registry.o. Then the linker keeps it, hence the variables get initialised, hence the std::map does.

This is I believe a relatively standard trick, although I'm not 100% happy with it. If anyone has a replacement for that, I'd be happy to know (but it's probably not for the CMake list).

Hope this clarifies why I need to export registry.cxx (or its object file).

Kris
 
-----Original Message-----
From: Michael Ellery 
Sent: 19 January 2017 16:07
To: Kris Thielemans 
Cc: cmake at cmake.org
Subject: Re: [CMake] installing source (or object) files

I think my initial reaction to installing a source (cxx) file is that you should install a static library instead (.a) - which is basically what you are suggesting. Just define a new static library target that includes the cxx file and install it…should work fine. If there are headers needed to use that static lib, you install those as well. There is a third type of library target called OBJECT, but those are usually just for use in the context of a build and not meant to be installed (if i recall correctly…).

HTH,
Mike Ellery

> On Jan 18, 2017, at 11:49 PM, Kris Thielemans wrote:
> 
> Hi
>  
> I haven’t had any replies to my message below, hopefully because it was sent at the wrong time of year… So I’m reposting, but with some  clarifications.
>  
> I wrote the message about include_flags only. I’ve currently solved 
> this by adding a variable
>  
>                Set(STIR_INCLUDE_DIRS blabla)
>  
> in my STIRConfig.cmake.in (which is at 
> https://github.com/UCL/UCL-STIR/blob/master/src/cmake/STIRConfig.cmake
> .in)
>  
> and tell the user of the library that they have to do
>  
> include_directories(${STIR_INCLUDE_DIRS})
>  
> This works but only for include directories, not for other compiler flags. I’ve currently solved that by putting all (most?) of these in a STIRConfig.h.in.
>  
> I don’t particularly like the include_directories solution however. I believe CMake’s eport facility is trying to get away from this. Indeed, for the library, its include path (and other compilation flags) get automatically exported, so the user doesn’t have to do anything else than target_link_libraries to inherit all necessary flags.
>  
> Also, my current solution relies on exporting the .cxx. It seems safer to export the object file as that is guaranteed to have been compiled with the correct flags, but I don’t know how to do that.
>  
> I hope my message below makes more sense now.
>  
> Thanks
>  
> Kris
> From: Kris Thielemans
> Sent: 27 December 2016 16:12
> To: cmake at cmake.org
> Subject: installing source (or object) files
>  
> Hi all
>  
> I have a non-standard set-up where I have a library that needs to be 
> linked together with some extra source files that contain 
> “registries”). So in my CMakeLists.txt I use
>  
> Add_executable(wonderful_program wonderful_program.cxx registry.cxx) 
> target_link_libraries(wonderful_program mylib)
>  
> registry.cxx needs to be compiled with some of my include files.
>  
> For that, I added
>  
> include_directories(“${PROJECT_SOURCE_DIR}/src/include”)
>  
> I guess I could just as well have added
>  
> SET_PROPERTY(SOURCE registry.cxx PROPERTY COMPILE_FLAG 
> "-I${STIR_INCLUDE_DIR}")
>  
>  
> This works fine, but now I want to export my library (relocatable). I 
> have created a config file etc, added mylib as an install target, 
> installed the include files etc. All of that works great for the 
> library. However, I have no idea how to install registry.cxx. I could 
> just
>  
> INSTALL(FILES registry.cxx …)
>  
> However, that doesn’t copy the properties of the source files anywhere. In particular, it means that a “client” doesn’t know what compilation flags to use.
>  
> Any ideas?
>  
> Best wishes for 2017!
>  
> Kris
>  
> PS: The reason that I need registry.cxx is that it contains some variables that have a funky constructor that initialises some registries somewhere.
> PS: Full source code is at https://github.com/UCL/STIR
> --
> 
> Powered by www.kitware.com
> 
> Please keep messages on-topic and check the CMake FAQ at: 
> http://www.cmake.org/Wiki/CMake_FAQ
> 
> Kitware offers various services to support the CMake community. For more information on each offering, please visit:
> 
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
> 
> Visit other Kitware open-source projects at 
> http://www.kitware.com/opensource/opensource.html
> 
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake




More information about the CMake mailing list