Note that there are some explanatory texts on larger screens.

plurals
  1. POCmake install target triggering
    primarykey
    data
    text
    <p>I'm writing build configuration with cmake: besides main project with own code there are some external libraries. For sake of easy updating of these libraries (zlib, libpng, ...) I don't want to modify its cmakelists files, but I need specific libraries targets (to use in target_link_libraries() for example). Another restrictions is that I can't just say that my code needs external libraries installed, all things must be located in one source code tree and must be built together. To keep everything libraries provide structured (libs, headers) I want to install (like <code>make install</code> does) libraries to local building folder and then include generated cmake-file to import needed targets to my project.</p> <p>I suppose flow as follows:</p> <ol> <li>build external library with <code>add_subdirectory()</code></li> <li>install external library files into local directory</li> <li>import targets using generated cmake-file</li> <li>use imported targets in primary project cmake files</li> </ol> <p>Problem is to automate step 2 (need to trigger <code>install</code> target after <code>add_subdiretory</code> inside main project CMakeLists.txt). I can build and install all libraries and then build own code, but this isn't convenient.</p> <p>So question is how can I tell cmake to do intermediate install during build?</p> <p>A little working example here:</p> <p>file structure:</p> <pre class="lang-bash prettyprint-override"><code>prj/CMakeLists.txt prj/src/main.cpp lib/CMakeLists.txt lib/include/libheader.h lib/src/libsource.cpp </code></pre> <p>prj/CMakeLists.txt</p> <pre class="lang-cmake prettyprint-override"><code>project(TestProject) cmake_minimum_required(VERSION 2.8) set(CMAKE_VERBOSE_MAKEFILE on) set(WORK_DIR ${CMAKE_CURRENT_SOURCE_DIR}) # supposed to use add_subdirectory here (with forced install). # and then include prespecified include-file as here. include_directories(${WORK_DIR}/../lib/build/install/include) include(${WORK_DIR}/../lib/build/install/lib/libtargets.cmake) add_executable(main ${WORK_DIR}/src/main.cpp) target_link_libraries(main library_target) </code></pre> <p>lib/CMakeLists.txt</p> <pre class="lang-cmake prettyprint-override"><code>project(TestLib) cmake_minimum_required(VERSION 2.8) set(CMAKE_VERBOSE_MAKEFILE on) set(WORK_DIR ${CMAKE_CURRENT_SOURCE_DIR}) include_directories(${WORK_DIR}/include) add_library(library_target STATIC ${WORK_DIR}/src/libsource.cpp) set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/install) install(FILES ${WORK_DIR}/include/libheader.h DESTINATION include) install(TARGETS library_target DESTINATION lib EXPORT libtargets) install(EXPORT libtargets DESTINATION lib) </code></pre> <p>prj/src/main.cpp</p> <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt; #include "libheader.h" using std::cout; int main() { cout &lt;&lt; getRandomNumber() &lt;&lt; "\n"; return 0; } </code></pre> <p>lib/include/libheader.h</p> <pre class="lang-cpp prettyprint-override"><code>#ifndef _LIBHEADER_H_ #define _LIBHEADER_H_ int getRandomNumber(); #endif </code></pre> <p>lib/src/libsource.cpp</p> <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt; #include "libheader.h" int getRandomNumber() { return 4; // guaranteed to be random. } </code></pre> <p>You can use following command to build all:</p> <pre class="lang-bash prettyprint-override"><code>pushd . &amp;&amp; mkdir lib/build &amp;&amp; cd lib/build &amp;&amp; cmake .. &amp;&amp; make &amp;&amp; make install &amp;&amp; popd &amp;&amp; pushd . &amp;&amp; mkdir prj/build &amp;&amp; cd prj/build &amp;&amp; cmake .. &amp;&amp; make || popd </code></pre> <p>EDIT: desired cmakelists in main project:</p> <pre class="lang-cmake prettyprint-override"><code>set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/install) add_subdirectory(${WORK_DIR}/../lib ${CMAKE_CURRENT_BINARY_DIR}/lib.d) # force install here, somehow # because if not we will get "include could not find load file" here. include(${CMAKE_CURRENT_BINARY_DIR}/install/lib/libtargets.cmake) include_directories(${CMAKE_CURRENT_BINARY_DIR}/install/include) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload