<div dir="ltr">Thank you for your help Clint.<div><br></div><div>I identified the problem. When I compile my library with CMAKE_MACOSX_RPATH, the library created is linked with @rpath/libMyLib.dylib.</div><div>When I was recompiling (make) it was updating the library in the build directory but it was not re-installing (copying in the MyLibrary-install directory).</div>
<div>That's why the link was keep failing.</div><div><br></div><div>Is there a way to automatically reinstall when recompiling in Cmake ?</div><div><br></div><div>Thank you very much.</div><div>-Laurent</div></div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Wed, Sep 4, 2013 at 2:16 PM, Clinton Stimpson <span dir="ltr"><<a href="mailto:clinton@elemtech.com" target="_blank">clinton@elemtech.com</a>></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 01:22:11 PM Laurent Chauvin wrote:<br>
> Does your example still works if you replace this line:<br>
><br>
> target_link_libraries(app ${CMAKE_CURRENT_LIST_DIR}/../lib/liblib.dylib)<br>
><br>
> by this one:<br>
><br>
> target_link_libraries(app lib)<br>
<br>
</div>No. It doesn't work. I get "ld: library not found for -llib"<br>
<br>
If I add a link_directories() I can get it working again.<br>
If I were to use an export file, it would also work again.<br>
<br>
Feel free to try experiment with the example I gave you to verify that the<br>
library can be found at runtime without setting DYLD_LIBRARY_PATH. You only<br>
need to supply 2 .cpp files with some dummy functions. The library and<br>
executable are in two separate directories.<br>
<br>
Clint<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
> ?<br>
><br>
> Thank you.<br>
> -Laurent<br>
><br>
> On Wed, Sep 4, 2013 at 1:18 PM, Clinton Stimpson<br>
<<a href="mailto:clinton@elemtech.com">clinton@elemtech.com</a>>wrote:<br>
> > On Wednesday, September 04, 2013 12:48:41 PM Laurent Chauvin wrote:<br>
> > > Yes I'm using 2.8.12-rc2 for the library and the example.<br>
> > ><br>
> > > But my example is not in the same directory as my library.<br>
> ><br>
> > Neither was my example.<br>
> ><br>
> > > I can see in your example you specify the full path in<br>
> > > target_link_libraries of your library, which I could probably do.<br>
> > ><br>
> > > The problem is, if later on my library I want to put the .dyld not in<br>
> > > MyLib/lib but MyLib/testLib, then I will have (or users) to modify all<br>
> > > their CMakeLists to modify path of the library.<br>
> > > That's why I would like not to have to hardcode the library path into my<br>
> > > example.<br>
> ><br>
> > Of course the users shouldn't hard code it. My example was simply<br>
> > demonstrating the solution to the problem of locating the shared library<br>
> > at<br>
> > runtime, which works in 2.8.12-rc2, without setting DYLD_LIBRARY_PATH.<br>
> ><br>
> > Really, this problem/solution for finding the .dylib at runtime has<br>
> > nothing to<br>
> > do with whether your target_link_libraries() has a full path or not. Or<br>
> > whether you are using LINK_DIRECTORIES or not.<br>
> > I just tested with full path vs. LINK_DIRECTORIES, and it makes no<br>
> > difference<br>
> > to whether the library can be found at runtime.<br>
> ><br>
> > As an aside, I'm encouraging you to not use LINK_DIRECTORIES.<br>
> ><br>
> > Clint<br>
> ><br>
> > > I'm using find_package to find my library, and include the use file,<br>
> ><br>
> > which<br>
> ><br>
> > > define the include_directories and link_directories.<br>
> > ><br>
> > > I also checked the INSTALL(EXPORT ..), here<br>
> ><br>
> > <a href="http://www.cmake.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cm" target="_blank">http://www.cmake.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cm</a><br>
> > ak><br>
> > > e_file but I don't understand what it's doing and how it works.<br>
> > ><br>
> > > Thank you.<br>
> > > -Laurent<br>
> > ><br>
> > > On Wed, Sep 4, 2013 at 12:26 PM, Clinton Stimpson<br>
> ><br>
> > <<a href="mailto:clinton@elemtech.com">clinton@elemtech.com</a>>wrote:<br>
> > > > On Wednesday, September 04, 2013 12:02:55 PM Laurent Chauvin wrote:<br>
> > > > > Thank you for you help Clint.<br>
> > > > ><br>
> > > > > I actually relinked, but as you said I still had the<br>
> ><br>
> > LINK_DIRECTORIES.<br>
> ><br>
> > > > > However, if I remove it, I have a linking error telling me it cannot<br>
> > > > > find<br>
> > > > > the library.<br>
> > > > ><br>
> > > > > Should I keep the library in Target_Link_Libraries, or with new<br>
> ><br>
> > flags,<br>
> ><br>
> > > > > dependency will automatically find the library ?<br>
> > > ><br>
> > > > You can leave your LINK_DIRECTORIES as its not the cause of your<br>
> ><br>
> > runtime<br>
> ><br>
> > > > failure to find the library.<br>
> > > ><br>
> > > > For compiling MyExample, did you also use CMake 2.8.12 RC?<br>
> > > ><br>
> > > > Here's a minimal example that works for me.<br>
> > > ><br>
> > > > -- lib/CMakeLists.txt<br>
> > > > set(CMAKE_MACOSX_RPATH 1)<br>
> > > > add_library(lib SHARED lib.cpp)<br>
> > > ><br>
> > > > -- app/CMakeLists.txt<br>
> > > > add_executable(app app.cpp)<br>
> > > > target_link_libraries(app<br>
> ><br>
> > ${CMAKE_CURRENT_LIST_DIR}/../lib/liblib.dylib)<br>
> ><br>
> > > > In lib/<br>
> > > > I run "cmake . && make"<br>
> > > ><br>
> > > > In app/<br>
> > > > I run "cmake . && make && ./app"<br>
> > > ><br>
> > > > With 2.8.12, it works.<br>
> > > ><br>
> > > > With an older version, I get<br>
> > > > dyld: Library not loaded: @rpath/liblib.dylib<br>
> > > ><br>
> > > > Referenced from: ..../app<br>
> > > > Reason: image not found<br>
> > > ><br>
> > > > Trace/BPT trap<br>
> > > ><br>
> > > > Clint<br>
> > > ><br>
> > > > > Thank you.<br>
> > > > > -Laurent<br>
> > > > ><br>
> > > > > On Wed, Sep 4, 2013 at 11:59 AM, Clinton Stimpson<br>
> > > ><br>
> > > > <<a href="mailto:clinton@elemtech.com">clinton@elemtech.com</a>>wrote:<br>
> > > > > > On Wednesday, September 04, 2013 11:53:45 AM Laurent Chauvin<br>
wrote:<br>
> > > > > > > Hello,<br>
> > > > > > ><br>
> > > > > > > I just tried to set the MACOSX_RPATH 1 in the top CMakeLists of<br>
> > > ><br>
> > > > MyLib,<br>
> > > ><br>
> > > > > > and<br>
> > > > > ><br>
> > > > > > > when I now do otool -L libMyLib.dyld, I can indeed see<br>
> > > > > ><br>
> > > > > > @rpath/libMyLib.dyld.<br>
> > > > > ><br>
> > > > > > > However, my executable "MyExample" is still not able to find the<br>
> > > ><br>
> > > > library<br>
> > > ><br>
> > > > > > at<br>
> > > > > ><br>
> > > > > > > runtime. When I do otool -L MyExample, I still have<br>
> ><br>
> > libMyLib.dyld,<br>
> ><br>
> > > > not<br>
> > > ><br>
> > > > > > > @rpath/libMyLib.dyld.<br>
> > > > > ><br>
> > > > > > Did you relink MyExamle against the new libMyLib.dylib? CMake<br>
> ><br>
> > doesn't<br>
> ><br>
> > > > > > modify<br>
> > > > > > the install names of libraries being linked against, so if otool<br>
> > > > > > -L<br>
> > > > > > MyExample<br>
> > > > > > still gives "libMyLib.dylib", I'm assuming you didn't relink it.<br>
> > > ><br>
> > > > Using a<br>
> > > ><br>
> > > > > > cmake generated export file would take care of setting up<br>
> ><br>
> > dependencies<br>
> ><br>
> > > > so<br>
> > > ><br>
> > > > > > the<br>
> > > > > > relink would happen simply by doing a make. If you use<br>
> > > ><br>
> > > > LINK_DIRECTORIES,<br>
> > > ><br>
> > > > > > you<br>
> > > > > > miss the relink dependency.<br>
> > > > > ><br>
> > > > > > Clint<br>
> > > > > ><br>
> > > > > > > And I have same error (Library not loaded, image not found) when<br>
> > > ><br>
> > > > trying<br>
> > > ><br>
> > > > > > to<br>
> > > > > ><br>
> > > > > > > start the example.<br>
> > > > > > ><br>
> > > > > > > Should I add something more (or remove something) from my<br>
> ><br>
> > CMakeLists<br>
> ><br>
> > > > > > > example ?<br>
> > > > > > ><br>
> > > > > > > I'm sorry I'm not really familiar with these mechanisms.<br>
> > > > > > ><br>
> > > > > > > Thank you very much.<br>
> > > > > > > -Laurent<br>
> > > > > > ><br>
> > > > > > ><br>
> > > > > > > On Wed, Sep 4, 2013 at 11:30 AM, Laurent Chauvin<br>
> > > > > > ><br>
> > > > > > > <<a href="mailto:lchauvin@bwh.harvard.edu">lchauvin@bwh.harvard.edu</a>>wrote:<br>
> > > > > > > > Thank you very much for your answer.<br>
> > > > > > > ><br>
> > > > > > > > I will try to use the MACOSX_RPATH.<br>
> > > > > > > ><br>
> > > > > > > > However, I have some questions. As the flag suggests, it's<br>
> > > > > > > > only<br>
> > > > > > > > for<br>
> > > > > ><br>
> > > > > > mac.<br>
> > > > > ><br>
> > > > > > > > Is there anything similar for Linux (and eventually Windows<br>
> > > ><br>
> > > > systems) ?<br>
> > > ><br>
> > > > > > > > Also, the problem to set the full path of the library is, if I<br>
> > > ><br>
> > > > compile<br>
> > > ><br>
> > > > > > on<br>
> > > > > ><br>
> > > > > > > > Windows the extension of library will be dll, on Mac it will<br>
> > > > > > > > be<br>
> > > ><br>
> > > > dyld,<br>
> > > ><br>
> > > > > > and<br>
> > > > > ><br>
> > > > > > > > .a on linux.<br>
> > > > > > > > I could make a condition to set the extension at the end of<br>
> > > > > > > > the<br>
> > > ><br>
> > > > name<br>
> > > ><br>
> > > > > > > > of<br>
> > > > > > > > the library like libMyLib.(dll, dyld, a) but, I feel like it's<br>
> ><br>
> > not<br>
> ><br>
> > > > > > really<br>
> > > > > ><br>
> > > > > > > > a<br>
> > > > > > > > clean way to do it.<br>
> > > > > > > ><br>
> > > > > > > > Is there a better way to do it ?<br>
> > > > > > > ><br>
> > > > > > > > Thank you very much.<br>
> > > > > > > > -Laurent<br>
> > > > > > > ><br>
> > > > > > > > On Wed, Sep 4, 2013 at 10:57 AM, Clinton Stimpson<br>
> > > > > ><br>
> > > > > > <<a href="mailto:clinton@elemtech.com">clinton@elemtech.com</a>>wrote:<br>
> > > > > > > >> On Tuesday, September 03, 2013 09:47:45 PM Laurent Chauvin<br>
> ><br>
> > wrote:<br>
> > > > > > > >> > Hello everyone,<br>
> > > > > > > >> ><br>
> > > > > > > >> > I'm working on a library, and I would like the users be<br>
> ><br>
> > able to<br>
> ><br>
> > > > > > create<br>
> > > > > ><br>
> > > > > > > >> their<br>
> > > > > > > >><br>
> > > > > > > >> > own program and liking to my library (by specifying path in<br>
> > > ><br>
> > > > cmake).<br>
> > > ><br>
> > > > > > > >> > I created the library and an example to test it.<br>
> > > > > > > >> > Everything compiles.<br>
> > > > > > > >> ><br>
> > > > > > > >> > However, when I try to run my example I have this error:<br>
> > > > > > > >> ><br>
> > > > > > > >> > MyExample:<br>
> > > > > > > >> > libMyLib.1.dylib (compatibility version 1.0.0,<br>
> > > > > > > >> > current<br>
> > > > > > > >> > version<br>
> > > > > > > >><br>
> > > > > > > >> 1.0.0)<br>
> > > > > > > >><br>
> > > > > > > >> > /usr/lib/libstdc++.6.dylib (compatibility version<br>
> ><br>
> > 7.0.0,<br>
> ><br>
> > > > > > current<br>
> > > > > ><br>
> > > > > > > >> version<br>
> > > > > > > >><br>
> > > > > > > >> > 56.0.0) /usr/lib/libSystem.B.dylib (compatibility version<br>
> > > > > > > >> > 1.0.0,<br>
> > > > > > > >> > current<br>
> > > > > > > >> > version 169.3.0)<br>
> > > > > > > >> ><br>
> > > > > > > >> > The problem is my example is not in the same directory as<br>
> > > > > > > >> > my<br>
> > > > > > > >> > library<br>
> > > > > > > >><br>
> > > > > > > >> (not<br>
> > > > > > > >><br>
> > > > > > > >> > even in a subdirectory). And it seems it's linking to my<br>
> > > > > > > >> > library<br>
> > > > > ><br>
> > > > > > with a<br>
> > > > > ><br>
> > > > > > > >> > relative path.<br>
> > > > > > > >> ><br>
> > > > > > > >> > If I add the path of my library in DYLD_LIBRARY_PATH it<br>
> ><br>
> > works,<br>
> ><br>
> > > > but<br>
> > > ><br>
> > > > > > > >> > I<br>
> > > > > > > >><br>
> > > > > > > >> would<br>
> > > > > > > >><br>
> > > > > > > >> > like the users to be able to compile and link straight<br>
> ><br>
> > forward.<br>
> ><br>
> > > > > > > >> > To link the library to my example I use find_package(MyLib<br>
> > > > > > > >> > REQUIRED)<br>
> > > > > > > >> > and<br>
> > > > > > > >> > include(${MyLib_USE_FILE}) which basically do a<br>
> > > > > > > >> > LINK_DIRECTORIES<br>
> > > > > ><br>
> > > > > > with<br>
> > > > > ><br>
> > > > > > > >> the<br>
> > > > > > > >><br>
> > > > > > > >> > absolute path of the library.<br>
> > > > > > > >> ><br>
> > > > > > > >> > Then I do add_executable and<br>
> > > > > > > >> > target_link_libraries(MyExample<br>
> > > > > > > >> > ${MyLib_LIBRARIES})<br>
> > > > > > > >> ><br>
> > > > > > > >> > Compilation is working fine. There is these options:<br>
> > > > > > > >> ><br>
> > > > > > > >> > -L/Absolute/Path/To/MyLib -lMyLib<br>
> > > > > > > >> ><br>
> > > > > > > >> > But at runtime, library cannot be found.<br>
> > > > > > > >> ><br>
> > > > > > > >> > Would it be possible to put the full path of my library<br>
> > > > > > > >> > when<br>
> > > > > ><br>
> > > > > > linking in<br>
> > > > > ><br>
> > > > > > > >> the<br>
> > > > > > > >><br>
> > > > > > > >> > CMakeLists ?<br>
> > > > > > > >><br>
> > > > > > > >> You should use the full path to the library instead of<br>
> > > > > ><br>
> > > > > > LINK_DIRECTORIES.<br>
> > > > > ><br>
> > > > > > > >> Its easiest if you use install(EXPORT ...) to have CMake<br>
> ><br>
> > generate<br>
> ><br>
> > > > an<br>
> > > ><br>
> > > > > > > >> export<br>
> > > > > > > >> file for you that can be included by your FindMyLib.cmake<br>
> ><br>
> > file.<br>
> ><br>
> > > > > > > >> That export file will use the full path, and include any<br>
> > > > > > > >> other<br>
> > > > > ><br>
> > > > > > necessary<br>
> > > > > ><br>
> > > > > > > >> information about the library.<br>
> > > > > > > >><br>
> > > > > > > >> But to solve the problem of finding the library at runtime<br>
> > > > > ><br>
> > > > > > (specifying a<br>
> > > > > ><br>
> > > > > > > >> full<br>
> > > > > > > >> path won't solve it), CMake 2.8.12 has a new feature to<br>
> ><br>
> > address<br>
> ><br>
> > > > > > exactly<br>
> > > > > ><br>
> > > > > > > >> that<br>
> ><br>
> > > > > > > >> problem. For details:<br>
> > <a href="http://www.kitware.com/blog/home/post/510" target="_blank">http://www.kitware.com/blog/home/post/510</a><br>
> ><br>
> > > > > > > >> If you can use CMake 2.8.12, then you need to put<br>
> > > > > > > >><br>
> > > > > > > >> set(MACOSX_RPATH 1)<br>
> > > > > > > >><br>
> > > > > > > >> in the CMakeLists.txt of MyLib.<br>
> > > > > > > >><br>
> > > > > > > >> If the user is using 2.8.12, then any executable they compile<br>
> > > > > > > >> with<br>
> > > > > ><br>
> > > > > > your<br>
> > > > > ><br>
> > > > > > > >> library will be able to find the library, no matter where it<br>
> ><br>
> > is.<br>
> ><br>
> > > > > > > >> By the way, 2.8.12 is in a release candidate stage right now,<br>
> ><br>
> > and<br>
> ><br>
> > > > > > right<br>
> > > > > ><br>
> > > > > > > >> now<br>
> > > > > > > >> would be a good time for you to test the new feature that is<br>
> > > ><br>
> > > > meant to<br>
> > > ><br>
> > > > > > > >> solve<br>
> > > > > > > >> your problem.<br>
> > > > > > > >><br>
> > > > > > > >> Clint<br>
> > > > > > > ><br>
> > > > > > > > --<br>
> > > > > > > > Laurent Chauvin, MS<br>
> > > > > > > > Surgical Navigation and Robotics Laboratory, Radiology<br>
> ><br>
> > Department<br>
> ><br>
> > > > > > > > Brigham And Women's Hospital, Harvard Medical School<br>
> > > > > > > > <a href="http://wiki.ncigt.org/index.php/User:Lchauvin" target="_blank">http://wiki.ncigt.org/index.php/User:Lchauvin</a><br>
> > > > > ><br>
> > > > > > --<br>
> ><br>
> > --<br>
> > 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>
--<br>
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'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>