[CMake] Shared library from ExternalProject

Tamás Kenéz tamas.kenez at gmail.com
Tue Sep 15 10:32:08 EDT 2015


> it would be nice to know if the usage of add_library(foo SHARED
> IMPORTED) is the way to go in this case.

I wouldn't think this is the way to go. One thing we love CMake for is that
it separates the concerns of the library and the consumer target so it's
best to stick to find_package.

This is the usage pattern of ExternalProject with find_package:
Create a super project which contains the library and the consumer target
in 2 separate ExternalProjects so the library can be installed before the
main target's CMakeLists is configured.

In certain cases you can also clone, build and install the library in an
init shell script of your project, outside of the main CMakeLists.txt. This
can be achieved either in a separate CMakeLists.txt (e.g.
./dependencies/CMakeLists.txt) or by raw shell commands (calling curl, git,
cmake, cmake --build directly)

Tamas

On Tue, Sep 15, 2015 at 2:07 PM, Tino Mettler <tino.mettler at alcnetworx.de>
wrote:

> Hi,
>
> it would be nice to know if the usage of add_library(foo SHARED
> IMPORTED) is the way to go in this case.
>
> find_package() seems to be useless, as it requires that the external
> library is already present when cmake is invoked, which is not the case
> with an external project.
>
> So it seems like I have to define everything via set_target_properies().
>
> What I see is that cmake uses rpath for linking using gcc/binutils. Is
> there a way to just use -L here? In the final installation (an embedded
> device), the libraries will be placed in the standard search path of the
> dynamic linker, so the rpath is just build system specific and not
> needed for the resulting binary.
>
> I also discovered that the build of the external project is not updated
> when the filesystem timestamp as well as the contents of external.tar.gz
> were changed. How can I achieve this?
>
> Here is my minimized example:
>
> cmake_minimum_required(VERSION 3.0.0)
> project (hello)
> add_executable(hello helloworld.cpp)
> include(ExternalProject)
> ExternalProject_Add(externalproject URL ../../../external.tar.gz)
> ExternalProject_Get_Property(externalproject binary_dir)
> add_library(externallib SHARED IMPORTED)
> set_target_properties(externallib PROPERTIES IMPORTED_LOCATION
> ${binary_dir}/install/lib/${CMAKE_SHARED_LIBRARY_PREFIX}mylib${CMAKE_SHARED_LIBRARY_SUFFIX}
> )
> include_directories(${binary_dir}/install/include/)
> target_link_libraries(hello externallib)
> add_dependencies(hello externalproject)
>
> Regards,
> Tino
>
> --
>
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20150915/e8c1501b/attachment.html>


More information about the CMake mailing list