<div class="gmail_quote">On Fri, Jan 23, 2009 at 1:11 PM, Wade Williams <span dir="ltr">&lt;<a href="mailto:wadesworld@mac.com">wadesworld@mac.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div style=""><font face="Monaco">I have a project structure that looks like this:</font><div><font face="Monaco"><br></font></div><div><font face="Monaco">project</font></div><div><font face="Monaco">|</font></div><div><font face="Monaco">| - lib</font></div>
<div><font face="Monaco">| ----bin</font></div><div><font face="Monaco">| ----util</font></div><div><font face="Monaco">| --------inc</font></div><div><font face="Monaco">| --------src</font></div><div><font face="Monaco">| - app</font></div>
<div><font face="Monaco">| ----bin</font></div><div><font face="Monaco">| ----inc</font></div><div><font face="Monaco">| ----src</font></div><div><font face="Monaco"><br></font></div><div><font face="Monaco">In short, each library in the project lives under project/lib. &nbsp;Built libraries are stored in project/lib/bin. &nbsp;Each library subdirectory (i.e. util) has a CMakeLists.txt to build that particular library. &nbsp;In the lib folder is a CMakeLists.txt which does add_subdirectory() on all the subdirectories. &nbsp;So far, so good - works like a charm.</font></div>
<div><font face="Monaco"><br></font></div><div><font face="Monaco">The executable lives under project/app. &nbsp;The built executable is stored in project/app/bin. &nbsp;There is a CMakeLists.txt in project/app to build the application executable.</font></div>
<div><font face="Monaco"><br></font></div><div><font face="Monaco">Now for the problem. &nbsp;How do I add the libraries as dependencies?</font></div><div><br></div><div><font face="Monaco">I can&#39;t use add_dependencies() because the app/CMakeLists.txt doesn&#39;t know about the targets in the library directories. &nbsp;I suppose I could do:</font></div>
<div><font face="Monaco"><br></font></div><div><font face="Monaco">add_subdirectory(../../lib ../../lib/bin)</font></div><div><font face="Monaco"><br></font></div><div><font face="Monaco">so it would know about those targets, but that doesn&#39;t seem the right solution.</font></div>
<div><font face="Monaco"><br></font></div><div><font face="Monaco">I can certainly use find_library() to add the built libraries to my executable, but then my executable is not dependent upon being them and linking will fail if make was not executed in lib first.</font></div>
<div><font face="Monaco"><br></font></div><div><font face="Monaco">Now, I certainly plan to have a project/CMakeLists.txt file that does add_subdirectory() on lib and app. &nbsp;Thus, if someone builds from that top level, it should all work if I use find_library() at the application level. &nbsp;However, I&#39;d like it also to work if someone cd&#39;s into the application directory and types &quot;cmake . ; make&quot;.&nbsp;</font></div>
<div><font face="Monaco"><br></font></div><div><font face="Monaco">Any thoughts?</font></div><div><font face="Monaco"></font></div></div></blockquote></div><br>You shouldn&#39;t need to use find_library() to detect internally built libraries.&nbsp; This was not the intention of that command which is instead meant to facilitate linking against externally provided libraries.&nbsp; You should instead be using target_link_libraries() which handles dependencies automatically.<br>
<br>If someone types &quot;make&quot; in the app directory after typing &quot;cmake . ; make&quot; from the toplevel here is what should happen:<br><br>1. All targets added at the app folder or beneath will be queued for building by default<br>
<br>2. Presuming you have created a myapp target which is linked to a mylib library in the &quot;lib&quot; folder using target_link_libraries(), make will first compile and link mylib.&nbsp; Then make will compile and link myapp.<br>
<br>To get this functionality simply call<br>&nbsp;&nbsp; target_link_libraries(myapp mylib)<br>in app/CMakeLists.txt<br><br>You&#39;ll also need to ensure that you call &quot;ADD_SUBDIRECTORY(lib)&quot; before the &quot;ADD_SUBDIRECTORY(app)&quot; otherwise the mylib target won&#39;t exist and you&#39;ll get a CMake error.<br>
<br>As for ADD_SUBDIRECTORY(../lib) I wouldn&#39;t recommend it although I believe it will work.&nbsp; The simplest approach is to do your ADD_SUBDIRECTORY() at the toplevel and assume the user will always configure from the toplevel.&nbsp; If you have some reason to think they might want to configure from a subdirectory check your premises first.&nbsp; Usually if you want only certain subdirectories to build you can facilitate this with OPTION()<br>
<br>OPTION(BUILD_MAIN_APP &quot;Build the main application&quot; ON)<br>IF(BUILD_MAIN_APP)<br>&nbsp;&nbsp; ADD_SUBDIRECTORY(app)<br>ENDIF()<br><br><br>-- <br>Philip Lowman<br>