Jonathan
Jonathan

Reputation: 734

Cmake: how to mark library as dependent on a system library

I have a C++ library that is dependent on the linux system library util. I can compile my library with the g++ command "g++ lib.cpp -lutil", and it compiles just fine.

When I added my library to our larger project is when I ran into troubles. I can go to each target that includes our library and use the 'target_link_libraries' command to have it include util. The problem is that there are many targets that depend on this library. It would be much better if I could just edit the CMakeLists.txt of the library and say that it depends on util. But I can't find a way of doing that.

Is it possible to mark my library as being dependent on util, so that any target including my library will also link in util?

Upvotes: 2

Views: 1009

Answers (2)

If you make a shared library, you can always link some other libraries to it. So if s1.cc and s2.cc are the sources of your library (sharing a common header.h) you could compile the library with

  g++ -Wall -fPIC s1.cc -c -o s1.pic.o
  g++ -Wall -fPIC s2.cc -c -o s2.pic.o
  g++ -Wall -shared s1.pic.o s2.pic.o -lutil -o libyours.so

(or have appropriate make or cmake rules doing the above). Then all the users of libyours.so would link also /usr/lib/libutil.so.

However, what you really are caring about is package dependency. Make a package (.deb on Debian or Ubuntu, ....) for your libyours and that package will depend appropriately on other packages.

You could also use, and have your users of libyours use, pkg-config. Then write a libyours.pc file describing the dependencies.

Upvotes: 0

ToniBig
ToniBig

Reputation: 836

All you have to do is to link your library lib to util using target_link_libraries. Now whenever other targets (executables or libraries) link to lib they are also linked to util.

Upvotes: 4

Related Questions