<div dir="ltr">Thank you for you help Clint.<div><br></div><div>I actually relinked, but as you said I still had the LINK_DIRECTORIES.</div><div>However, if I remove it, I have a linking error telling me it cannot find the library.</div>
<div><br></div><div>Should I keep the library in Target_Link_Libraries, or with new flags, dependency will automatically find the library ?</div><div><br></div><div>Thank you.</div><div>-Laurent</div></div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Wed, Sep 4, 2013 at 11:59 AM, Clinton Stimpson <span dir="ltr">&lt;<a href="mailto:clinton@elemtech.com" target="_blank">clinton@elemtech.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On Wednesday, September 04, 2013 11:53:45 AM Laurent Chauvin wrote:<br>
&gt; Hello,<br>
&gt;<br>
&gt; I just tried to set the MACOSX_RPATH 1 in the top CMakeLists of MyLib, and<br>
&gt; when I now do otool -L libMyLib.dyld, I can indeed see @rpath/libMyLib.dyld.<br>
&gt; However, my executable &quot;MyExample&quot; is still not able to find the library at<br>
&gt; runtime. When I do otool -L MyExample, I still have libMyLib.dyld, not<br>
&gt; @rpath/libMyLib.dyld.<br>
<br>
</div>Did you relink MyExamle against the new libMyLib.dylib?  CMake doesn&#39;t modify<br>
the install names of libraries being linked against, so if otool -L MyExample<br>
still gives &quot;libMyLib.dylib&quot;, I&#39;m assuming you didn&#39;t relink it.  Using a<br>
cmake generated export file would take care of setting up dependencies so the<br>
relink would happen simply by doing a make.  If you use LINK_DIRECTORIES, you<br>
miss the relink dependency.<br>
<br>
Clint<br>
<div class="HOEnZb"><div class="h5"><br>
&gt; And I have same error (Library not loaded, image not found) when trying to<br>
&gt; start the example.<br>
&gt;<br>
&gt; Should I add something more (or remove something) from my CMakeLists<br>
&gt; example ?<br>
&gt;<br>
&gt; I&#39;m sorry I&#39;m not really familiar with these mechanisms.<br>
&gt;<br>
&gt; Thank you very much.<br>
&gt; -Laurent<br>
&gt;<br>
&gt;<br>
&gt; On Wed, Sep 4, 2013 at 11:30 AM, Laurent Chauvin<br>
&gt;<br>
&gt; &lt;<a href="mailto:lchauvin@bwh.harvard.edu">lchauvin@bwh.harvard.edu</a>&gt;wrote:<br>
&gt; &gt; Thank you very much for your answer.<br>
&gt; &gt;<br>
&gt; &gt; I will try to use the MACOSX_RPATH.<br>
&gt; &gt;<br>
&gt; &gt; However, I have some questions. As the flag suggests, it&#39;s only for mac.<br>
&gt; &gt; Is there anything similar for Linux (and eventually Windows systems) ?<br>
&gt; &gt;<br>
&gt; &gt; Also, the problem to set the full path of the library is, if I compile on<br>
&gt; &gt; Windows the extension of library will be dll, on Mac it will be dyld, and<br>
&gt; &gt; .a on linux.<br>
&gt; &gt; I could make a condition to set the extension at the end of the name of<br>
&gt; &gt; the library like libMyLib.(dll, dyld, a) but, I feel like it&#39;s not really<br>
&gt; &gt; a<br>
&gt; &gt; clean way to do it.<br>
&gt; &gt;<br>
&gt; &gt; Is there a better way to do it ?<br>
&gt; &gt;<br>
&gt; &gt; Thank you very much.<br>
&gt; &gt; -Laurent<br>
&gt; &gt;<br>
&gt; &gt; On Wed, Sep 4, 2013 at 10:57 AM, Clinton Stimpson<br>
&lt;<a href="mailto:clinton@elemtech.com">clinton@elemtech.com</a>&gt;wrote:<br>
&gt; &gt;&gt; On Tuesday, September 03, 2013 09:47:45 PM Laurent Chauvin wrote:<br>
&gt; &gt;&gt; &gt; Hello everyone,<br>
&gt; &gt;&gt; &gt;<br>
&gt; &gt;&gt; &gt; I&#39;m working on a library, and I would like the users be able to create<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; their<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; &gt; own program and liking to my library (by specifying path in cmake).<br>
&gt; &gt;&gt; &gt;<br>
&gt; &gt;&gt; &gt; I created the library and an example to test it.<br>
&gt; &gt;&gt; &gt; Everything compiles.<br>
&gt; &gt;&gt; &gt;<br>
&gt; &gt;&gt; &gt; However, when I try to run my example I have this error:<br>
&gt; &gt;&gt; &gt;<br>
&gt; &gt;&gt; &gt; MyExample:<br>
&gt; &gt;&gt; &gt;       libMyLib.1.dylib (compatibility version 1.0.0, current version<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; 1.0.0)<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; &gt;       /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; version<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; &gt; 56.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0,<br>
&gt; &gt;&gt; &gt; current<br>
&gt; &gt;&gt; &gt; version 169.3.0)<br>
&gt; &gt;&gt; &gt;<br>
&gt; &gt;&gt; &gt; The problem is my example is not in the same directory as my library<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; (not<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; &gt; even in a subdirectory). And it seems it&#39;s linking to my library with a<br>
&gt; &gt;&gt; &gt; relative path.<br>
&gt; &gt;&gt; &gt;<br>
&gt; &gt;&gt; &gt; If I add the path of my library in DYLD_LIBRARY_PATH it works, but I<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; would<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; &gt; like the users to be able to compile and link straight forward.<br>
&gt; &gt;&gt; &gt;<br>
&gt; &gt;&gt; &gt; To link the library to my example I use find_package(MyLib REQUIRED)<br>
&gt; &gt;&gt; &gt; and<br>
&gt; &gt;&gt; &gt; include(${MyLib_USE_FILE}) which basically do a LINK_DIRECTORIES with<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; the<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; &gt; absolute path of the library.<br>
&gt; &gt;&gt; &gt;<br>
&gt; &gt;&gt; &gt; Then I do add_executable and target_link_libraries(MyExample<br>
&gt; &gt;&gt; &gt; ${MyLib_LIBRARIES})<br>
&gt; &gt;&gt; &gt;<br>
&gt; &gt;&gt; &gt; Compilation is working fine. There is these options:<br>
&gt; &gt;&gt; &gt;<br>
&gt; &gt;&gt; &gt; -L/Absolute/Path/To/MyLib -lMyLib<br>
&gt; &gt;&gt; &gt;<br>
&gt; &gt;&gt; &gt; But at runtime, library cannot be found.<br>
&gt; &gt;&gt; &gt;<br>
&gt; &gt;&gt; &gt; Would it be possible to put the full path of my library when linking in<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; the<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; &gt; CMakeLists ?<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; You should use the full path to the library instead of LINK_DIRECTORIES.<br>
&gt; &gt;&gt; Its easiest if you use install(EXPORT ...) to have CMake generate an<br>
&gt; &gt;&gt; export<br>
&gt; &gt;&gt; file for you that can be included by your FindMyLib.cmake file.<br>
&gt; &gt;&gt; That export file will use the full path, and include any other necessary<br>
&gt; &gt;&gt; information about the library.<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; But to solve the problem of finding the library at runtime (specifying a<br>
&gt; &gt;&gt; full<br>
&gt; &gt;&gt; path won&#39;t solve it), CMake 2.8.12 has a new feature to address exactly<br>
&gt; &gt;&gt; that<br>
&gt; &gt;&gt; problem.  For details: <a href="http://www.kitware.com/blog/home/post/510" target="_blank">http://www.kitware.com/blog/home/post/510</a><br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; If you can use CMake 2.8.12, then you need to put<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt;  set(MACOSX_RPATH 1)<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; in the CMakeLists.txt of MyLib.<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; If the user is using 2.8.12, then any executable they compile with your<br>
&gt; &gt;&gt; library will be able to find the library, no matter where it is.<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; By the way, 2.8.12 is in a release candidate stage right now, and right<br>
&gt; &gt;&gt; now<br>
&gt; &gt;&gt; would be a good time for you to test the new feature that is meant to<br>
&gt; &gt;&gt; solve<br>
&gt; &gt;&gt; your problem.<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; Clint<br>
&gt; &gt;<br>
&gt; &gt; --<br>
&gt; &gt; Laurent Chauvin, MS<br>
&gt; &gt; Surgical Navigation and Robotics Laboratory, Radiology Department<br>
&gt; &gt; Brigham And Women&#39;s Hospital, Harvard Medical School<br>
&gt; &gt; <a href="http://wiki.ncigt.org/index.php/User:Lchauvin" target="_blank">http://wiki.ncigt.org/index.php/User:Lchauvin</a><br>
--<br>
</div></div><div class="HOEnZb"><div class="h5">Clinton Stimpson<br>
Elemental Technologies, Inc<br>
Computational Simulation Software, LLC<br>
<a href="http://www.csimsoft.com" target="_blank">www.csimsoft.com</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div dir="ltr"><div>Laurent Chauvin, MS</div><div>Surgical Navigation and Robotics Laboratory, Radiology Department</div><div>Brigham And Women&#39;s Hospital, Harvard Medical School<br>
</div><div><a href="http://wiki.ncigt.org/index.php/User:Lchauvin" target="_blank">http://wiki.ncigt.org/index.php/User:Lchauvin</a></div></div>
</div>