<div class="gmail_quote">On Thu, Dec 2, 2010 at 5:40 AM, Michael Hertling <span dir="ltr"><<a href="mailto:mhertling@online.de">mhertling@online.de</a>></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>
> Hi Michael,<br>
><br>
><br>
> On Thu, Dec 2, 2010 at 01:03, Michael Hertling <<a href="mailto:mhertling@online.de">mhertling@online.de</a>> wrote:<br>
>> On 12/01/2010 08:18 AM, Raymond Wan wrote:<br>
>>> Hi all,<br>
>>><br>
>>> I'm having a problem understanding how I can link to an archive in<br>
>>> another directory which is not a subdirectory. For example:<br>
>>><br>
>>> myproj<br>
>>> +-- main<br>
>>> +-- CMakeLists.txt<br>
>>> +-- source files for main program<br>
>>> +-- dir-A<br>
>>> +-- CMakeLists.txt<br>
>>> +-- source files for sub-program A<br>
>>> +-- dir-B<br>
>>> +-- CmakeLists.txt<br>
>>> +-- source files for sub-program B<br>
>><br>
>> This error is caused by the fact that the directory "dir-A" is outside<br>
>> the "main" directory tree, so dir-A's binary directory would likewise<br>
>> be outside main's binary tree, and this is reasonably forbidden when<br>
>> configuring "main". You might specify dir-A's binary directory in<br>
>> main/CMakeLists.txt explicitly, e.g.:<br>
>><br>
>> ADD_SUBDIRECTORY(../dir-A ${CMAKE_CURRENT_BINARY_DIR}/dir-A)<br>
>><br>
>> Hence, subprogA_ar will be built within the binary tree's "dir-A".<br>
><br>
><br>
> Ah! I see now. I didn't realize I could prepend a path like that. I<br>
> was trying to find some kind of "ADD_DIRECTORY" command which had this<br>
> restriction removed. But then I realized if I'm looking for something<br>
> like that, there might be a bigger problem in terms of how I'm<br>
> organizing things...<br>
><br>
><br>
>>> myproj<br>
>>> +-- main<br>
>>> +-- CMakeLists.txt<br>
>>> +-- source files for main program<br>
>>> +-- dir-A<br>
>>> +-- source files for sub-program A<br>
>>> +-- dir-B<br>
>>> +-- source files for sub-program B<br>
>>><br>
>>><br>
>>> Is this what I should have done??<br>
>><br>
>> As an alternative, consider to add a CMakeLists.txt in "myproj"<br>
>><br>
>> CMAKE_MINIMUM_REQUIRED(...)<br>
>> PROJECT(myproj)<br>
>> ADD_SUBDIRECTORY(dir-A)<br>
>> ADD_SUBDIRECTORY(dir-B)<br>
>> ADD_SUBDIRECTORY(main)<br>
>><br>
>> and subsequently, configure this directory instead of "main".<br>
><br>
><br>
> Ah! I see. Then is it recommended that this top-level CMakeLists.txt<br>
> have just these lines, or should I move the ADD_EXECUTABLE, etc. lines<br>
> here as well? Or is this really "up to me" and there isn't a<br>
> 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'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 "../dir-A"; 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 "subprogA_ar". Due to CMake's capabilities to track the<br>
dependencies among targets, it doesn't matter where dir-A is placed<br>
within the project's source tree. So, in short, don't move the<br>
ADD_EXECUTABLE() etc. to the top-level CMakeLists.txt.<br>
<div class="im"><br>
> I guess I chose my first directory layout because the source in a<br>
> directory (i.e., dir-A) can be several dependencies down and it can<br>
> even be used by two targets in two different directories. So, I<br>
> figured that keeping them all at the "same directory level" would be<br>
> best. But it seems what you're suggesting here seems better -- never<br>
> thought of it; the "myproj" directory has no files in it...just<br>
> 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're<br>
enough things gathering in a project'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're exceptions</blockquote><div><br>Exceptions? I can't think of any... I think you'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'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>