<div dir="ltr">Yes I'm using 2.8.12-rc2 for the library and the example.<div><br></div><div>But my example is not in the same directory as my library.</div><div><br></div><div>I can see in your example you specify the full path in target_link_libraries of your library, which I could probably do.</div>
<div><br></div><div>The problem is, if later on my library I want to put the .dyld not in MyLib/lib but MyLib/testLib, then I will have (or users) to modify all their CMakeLists to modify path of the library.</div><div>That's why I would like not to have to hardcode the library path into my example.</div>
<div><br></div><div>I'm using find_package to find my library, and include the use file, which define the include_directories and link_directories.</div><div><br></div><div>I also checked the INSTALL(EXPORT ..), here <a href="http://www.cmake.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file">http://www.cmake.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file</a></div>
<div>but I don't understand what it's doing and how it works.</div><div><br></div><div>Thank you.</div><div>-Laurent</div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Sep 4, 2013 at 12:26 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 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 LINK_DIRECTORIES.<br>
> However, if I remove it, I have a linking error telling me it cannot find<br>
> the library.<br>
><br>
> Should I keep the library in Target_Link_Libraries, or with new flags,<br>
> dependency will automatically find the library ?<br>
<br>
</div>You can leave your LINK_DIRECTORIES as its not the cause of your runtime<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 ${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>
Referenced from: ..../app<br>
Reason: image not found<br>
Trace/BPT trap<br>
<br>
Clint<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
> Thank you.<br>
> -Laurent<br>
><br>
> On Wed, Sep 4, 2013 at 11:59 AM, Clinton Stimpson<br>
<<a href="mailto:clinton@elemtech.com">clinton@elemtech.com</a>>wrote:<br>
> > On Wednesday, September 04, 2013 11:53:45 AM Laurent Chauvin wrote:<br>
> > > Hello,<br>
> > ><br>
> > > I just tried to set the MACOSX_RPATH 1 in the top CMakeLists of 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 library<br>
> ><br>
> > at<br>
> ><br>
> > > runtime. When I do otool -L MyExample, I still have libMyLib.dyld, not<br>
> > > @rpath/libMyLib.dyld.<br>
> ><br>
> > Did you relink MyExamle against the new libMyLib.dylib? CMake doesn't<br>
> > modify<br>
> > the install names of libraries being linked against, so if otool -L<br>
> > MyExample<br>
> > still gives "libMyLib.dylib", I'm assuming you didn't relink it. Using a<br>
> > cmake generated export file would take care of setting up dependencies so<br>
> > the<br>
> > relink would happen simply by doing a make. If you use LINK_DIRECTORIES,<br>
> > you<br>
> > miss the relink dependency.<br>
> ><br>
> > Clint<br>
> ><br>
> > > And I have same error (Library not loaded, image not found) when trying<br>
> ><br>
> > to<br>
> ><br>
> > > start the example.<br>
> > ><br>
> > > Should I add something more (or remove something) from my CMakeLists<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 only for<br>
> ><br>
> > mac.<br>
> ><br>
> > > > Is there anything similar for Linux (and eventually Windows systems) ?<br>
> > > ><br>
> > > > Also, the problem to set the full path of the library is, if I compile<br>
> ><br>
> > on<br>
> ><br>
> > > > Windows the extension of library will be dll, on Mac it will be dyld,<br>
> ><br>
> > and<br>
> ><br>
> > > > .a on linux.<br>
> > > > I could make a condition to set the extension at the end of the name<br>
> > > > of<br>
> > > > the library like libMyLib.(dll, dyld, a) but, I feel like it's 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 wrote:<br>
> > > >> > Hello everyone,<br>
> > > >> ><br>
> > > >> > I'm working on a library, and I would like the users be able to<br>
> ><br>
> > create<br>
> ><br>
> > > >> their<br>
> > > >><br>
> > > >> > own program and liking to my library (by specifying path in 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, current<br>
> > > >> > version<br>
> > > >><br>
> > > >> 1.0.0)<br>
> > > >><br>
> > > >> > /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0,<br>
> ><br>
> > current<br>
> ><br>
> > > >> version<br>
> > > >><br>
> > > >> > 56.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0,<br>
> > > >> > current<br>
> > > >> > version 169.3.0)<br>
> > > >> ><br>
> > > >> > The problem is my example is not in the same directory as my<br>
> > > >> > library<br>
> > > >><br>
> > > >> (not<br>
> > > >><br>
> > > >> > even in a subdirectory). And it seems it's linking to my library<br>
> ><br>
> > with a<br>
> ><br>
> > > >> > relative path.<br>
> > > >> ><br>
> > > >> > If I add the path of my library in DYLD_LIBRARY_PATH it works, but<br>
> > > >> > I<br>
> > > >><br>
> > > >> would<br>
> > > >><br>
> > > >> > like the users to be able to compile and link straight 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 LINK_DIRECTORIES<br>
> ><br>
> > with<br>
> ><br>
> > > >> the<br>
> > > >><br>
> > > >> > absolute path of the library.<br>
> > > >> ><br>
> > > >> > Then I do add_executable and 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 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 generate an<br>
> > > >> export<br>
> > > >> file for you that can be included by your FindMyLib.cmake file.<br>
> > > >> That export file will use the full path, and include any 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 address<br>
> ><br>
> > exactly<br>
> ><br>
> > > >> that<br>
> > > >> 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>
> > > >><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 with<br>
> ><br>
> > your<br>
> ><br>
> > > >> library will be able to find the library, no matter where it is.<br>
> > > >><br>
> > > >> By the way, 2.8.12 is in a release candidate stage right now, and<br>
> ><br>
> > right<br>
> ><br>
> > > >> now<br>
> > > >> would be a good time for you to test the new feature that is meant to<br>
> > > >> solve<br>
> > > >> your problem.<br>
> > > >><br>
> > > >> Clint<br>
> > > ><br>
> > > > --<br>
> > > > Laurent Chauvin, MS<br>
> > > > Surgical Navigation and Robotics Laboratory, Radiology Department<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>
</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>