<div class="gmail_quote">On Thu, Dec 2, 2010 at 5:40 AM, Michael Hertling <span dir="ltr">&lt;<a href="mailto:mhertling@online.de">mhertling@online.de</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div><div></div><div class="h5">On 12/01/2010 06:03 PM, Raymond Wan wrote:<br>
&gt; Hi Michael,<br>
&gt;<br>
&gt;<br>
&gt; On Thu, Dec 2, 2010 at 01:03, Michael Hertling &lt;<a href="mailto:mhertling@online.de">mhertling@online.de</a>&gt; wrote:<br>
&gt;&gt; On 12/01/2010 08:18 AM, Raymond Wan wrote:<br>
&gt;&gt;&gt; Hi all,<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; I&#39;m having a problem understanding how I can link to an archive in<br>
&gt;&gt;&gt; another directory which is not a subdirectory. For example:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; myproj<br>
&gt;&gt;&gt;   +-- main<br>
&gt;&gt;&gt;     +-- CMakeLists.txt<br>
&gt;&gt;&gt;     +-- source files for main program<br>
&gt;&gt;&gt;   +-- dir-A<br>
&gt;&gt;&gt;     +-- CMakeLists.txt<br>
&gt;&gt;&gt;     +-- source files for sub-program A<br>
&gt;&gt;&gt;   +-- dir-B<br>
&gt;&gt;&gt;     +-- CmakeLists.txt<br>
&gt;&gt;&gt;     +-- source files for sub-program B<br>
&gt;&gt;<br>
&gt;&gt; This error is caused by the fact that the directory &quot;dir-A&quot; is outside<br>
&gt;&gt; the &quot;main&quot; directory tree, so dir-A&#39;s binary directory would likewise<br>
&gt;&gt; be outside main&#39;s binary tree, and this is reasonably forbidden when<br>
&gt;&gt; configuring &quot;main&quot;. You might specify dir-A&#39;s binary directory in<br>
&gt;&gt; main/CMakeLists.txt explicitly, e.g.:<br>
&gt;&gt;<br>
&gt;&gt; ADD_SUBDIRECTORY(../dir-A ${CMAKE_CURRENT_BINARY_DIR}/dir-A)<br>
&gt;&gt;<br>
&gt;&gt; Hence, subprogA_ar will be built within the binary tree&#39;s &quot;dir-A&quot;.<br>
&gt;<br>
&gt;<br>
&gt; Ah!  I see now.  I didn&#39;t realize I could prepend a path like that.  I<br>
&gt; was trying to find some kind of &quot;ADD_DIRECTORY&quot; command which had this<br>
&gt; restriction removed.  But then I realized if I&#39;m looking for something<br>
&gt; like that, there might be a bigger problem in terms of how I&#39;m<br>
&gt; organizing things...<br>
&gt;<br>
&gt;<br>
&gt;&gt;&gt; myproj<br>
&gt;&gt;&gt;   +-- main<br>
&gt;&gt;&gt;     +-- CMakeLists.txt<br>
&gt;&gt;&gt;     +-- source files for main program<br>
&gt;&gt;&gt;     +-- dir-A<br>
&gt;&gt;&gt;       +-- source files for sub-program A<br>
&gt;&gt;&gt;     +-- dir-B<br>
&gt;&gt;&gt;       +-- source files for sub-program B<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Is this what I should have done??<br>
&gt;&gt;<br>
&gt;&gt; As an alternative, consider to add a CMakeLists.txt in &quot;myproj&quot;<br>
&gt;&gt;<br>
&gt;&gt; CMAKE_MINIMUM_REQUIRED(...)<br>
&gt;&gt; PROJECT(myproj)<br>
&gt;&gt; ADD_SUBDIRECTORY(dir-A)<br>
&gt;&gt; ADD_SUBDIRECTORY(dir-B)<br>
&gt;&gt; ADD_SUBDIRECTORY(main)<br>
&gt;&gt;<br>
&gt;&gt; and subsequently, configure this directory instead of &quot;main&quot;.<br>
&gt;<br>
&gt;<br>
&gt; Ah!  I see.  Then is it recommended that this top-level CMakeLists.txt<br>
&gt; have just these lines, or should I move the ADD_EXECUTABLE, etc. lines<br>
&gt; here as well?  Or is this really &quot;up to me&quot; and there isn&#39;t a<br>
&gt; suggested paradigm?<br>
<br>
</div></div>Basically, IMO, a CMakeLists.txt with ADD_EXECUTABLE() et al. should be<br>
placed in the same directory as the related source files unless there&#39;s<br>
a reason not to do so; this makes for modularity and a well organized<br>
code base. The above-mentioned directory structure with its top-level<br>
CMakeLists.txt containing just ADD_SUBDIRECTORY() commands keeps the<br>
modules main, dir-A and dir-B separate with minimal interconnections.<br>
In the previous structure, e.g., main/CMakeLists.txt had to know that<br>
dir-A resides in &quot;../dir-A&quot;; now, such information is concentrated in<br>
the top-level CMakeLists.txt, and the sole reference of main to dir-A<br>
is the target &quot;subprogA_ar&quot;. Due to CMake&#39;s capabilities to track the<br>
dependencies among targets, it doesn&#39;t matter where dir-A is placed<br>
within the project&#39;s source tree. So, in short, don&#39;t move the<br>
ADD_EXECUTABLE() etc. to the top-level CMakeLists.txt.<br>
<div class="im"><br>
&gt; I guess I chose my first directory layout because the source in a<br>
&gt; directory (i.e., dir-A) can be several dependencies down and it can<br>
&gt; even be used by two targets in two different directories.  So, I<br>
&gt; figured that keeping them all at the &quot;same directory level&quot; would be<br>
&gt; best.  But it seems what you&#39;re suggesting here seems better -- never<br>
&gt; thought of it; the &quot;myproj&quot; directory has no files in it...just<br>
&gt; directories right now...<br>
<br>
</div>The top-level directory of non-trivial projects should not contain any<br>
source files, perhaps a config.h header or the like. Usually, there&#39;re<br>
enough things gathering in a project&#39;s root, e.g. READMEs, subdirs for<br>
documentation, resources or CMake stuff, a CMakeLists.txt file ;) etc.<br>
The sources, however, should be organized in their own subdirectories,<br>
so the top-level CMakeLists.txt typically contains ADD_SUBDIRECTORY()<br>
commands along with configurational stuff like setting up compiler<br>
flags, processing options or investigating the underlying system,<br>
but no ADD_EXECUTABLE() or the like.<br>
<br>
Of course, there&#39;re exceptions</blockquote><div><br>Exceptions? I can&#39;t think of any... I think you&#39;re absolutely correct for any non-trivial project.<br> <br></div><div> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
and probably strong different opinions.<br></blockquote><div><br>If there are, then they&#39;re just plain wrong. :-)<br><br><br>Thanks, Michael, for your contributions to the CMake mailing list. You provide invaluable advice and assistance for many.<br>
<br>Cheers,<br>David<br><br><br> <br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<br>
Regards,<br>
<font color="#888888"><br>
Michael<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" target="_blank">http://www.cmake.org/Wiki/CMake_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://www.cmake.org/mailman/listinfo/cmake" target="_blank">http://www.cmake.org/mailman/listinfo/cmake</a><br>
</div></div></blockquote></div><br>