[CMake] Absolute Path linking library

Laurent Chauvin lchauvin at bwh.harvard.edu
Wed Sep 4 12:48:41 EDT 2013


Yes I'm using 2.8.12-rc2 for the library and the example.

But my example is not in the same directory as my library.

I can see in your example you specify the full path in
target_link_libraries of your library, which I could probably do.

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.
That's why I would like not to have to hardcode the library path into my
example.

I'm using find_package to find my library, and include the use file, which
define the include_directories and link_directories.

I also checked the INSTALL(EXPORT ..), here
http://www.cmake.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file
but I don't understand what it's doing and how it works.

Thank you.
-Laurent



On Wed, Sep 4, 2013 at 12:26 PM, Clinton Stimpson <clinton at elemtech.com>wrote:

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


-- 
Laurent Chauvin, MS
Surgical Navigation and Robotics Laboratory, Radiology Department
Brigham And Women's Hospital, Harvard Medical School
http://wiki.ncigt.org/index.php/User:Lchauvin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20130904/48341575/attachment-0001.htm>


More information about the CMake mailing list