1

I am new to Linux. I see in my CMakeLists.txt the following;

  target_link_libraries(app wiringPi
                          serializer
                          iothub_client
                          iothub_client_mqtt_transport
                          umqtt
                          aziotsharedutil
                          ssl
                          crypto
                          curl
                          pthread
                          m
                          ssl
                          crypto)

My question/understanding is; by doing this

  1. Are we telling CMake tool to build wiringPi, serializer etc. and link with app(which is an executable created in my code)?
  2. Where are all these libraries located?
  3. When I do ldconfig -p | grep <libraryname>, for the above libraries sometimes I find outputs like the following and sometimes nothing, why is that?
  4. Is target_link_libraries smart enough to look for libraries under the sub-directories too? I mean I see that some are just there under user/lib and some are one more level under such as /usr/lib/arm-linux-gnueabihf

pi@raspberrypi:~ $ ldconfig -p | grep curl
libcurl.so.4 (libc6,hard-float) => /usr/lib/arm-linux-gnueabihf/libcurl.so.4
libcurl-gnutls.so.4 (libc6,hard-float) => /usr/lib/arm-linux-gnueabihf/libcurl-gnutls.so.4

pi@raspberrypi:~ $ ldconfig -p | grep wiringPi
libwiringPiDev.so (libc6,hard-float) => /usr/local/lib/libwiringPiDev.so
libwiringPiDev.so (libc6,hard-float) => /usr/lib/libwiringPiDev.so
libwiringPi.so (libc6,hard-float) => /usr/local/lib/libwiringPi.so
libwiringPi.so (libc6,hard-float) => /usr/lib/libwiringPi.so

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Vivek Shukla
  • 767
  • 6
  • 21

2 Answers2

3
  1. Are we telling CMake tool to build wiringPi, serializer etc. and link with app(which is an executable created in my code)?

Not exactly. That command tells CMake that the libraries wiringPi, serializer, etc. must be linked to target 'app' during the link stage for that target. It says nothing about building the libraries themselves, and usually they are expected to already be available rather than built.

  1. Where are all these libraries located?

It can vary. The compiler has a default list of directories in which it looks for libraries. Other CMake commands can add link options that add directories to that list.

  1. When I do ldconfig -p | grep <libraryname>, for the above libraries sometimes I find outputs like the following and sometimes nothing, why is that?

ldconfig reports on libraries known to the dynamic linker. This is distinct from the linker that runs at compile time, and the directories and libraries the two know about are not necessarily the same. Reasons why ldconfig might not list a given library include:

  1. The library is not installed.
  2. Only a static version of the library is installed.
  3. The library is not in any of the locations that the dynamic linker checks by default (additional directories can be specified when a program is launched, in at least two different ways).
  1. Is target_link_libraries smart enough to look for libraries under the sub-directories too? I mean I see that some are just there under user/lib and some are one more level under such as /usr/lib/arm-linux-gnueabihf

This is not a function of CMake, but rather of the chosen toolchain and its configuration (on Linux, that's often the GNU toolchain, featuring GCC). It is usually safe to assume that the toolchain uses all the right standard library directories by default. CMake sometimes can successfully be instructed to search for specific libraries in other likely places, too, but target_link_libraries is not part of that.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
1

When you pass to target_link_libraries a plain name (not a path) which is not a target, CMake just transforms this name into the linker flag. E.g. on Linux this is flag

-l<library-name>

So questions about searching the library you may address directly to the linker - CMake is out of the game here.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • Thanks for the response, so I should look for say lcurl? Please elaborate the last sentence. – Vivek Shukla Sep 05 '18 at 20:41
  • 1
    When you specify `curl` as the `target_link_libraries` argument, CMake transforms in into `-lcurl` option to the linker. If you curious about where the library is searched, you need to find out how the linker (GNU ld in your case) works. – Tsyvarev Sep 05 '18 at 20:45
  • Do you please mind reviewing https://stackoverflow.com/q/52172272/2719527 question – Vivek Shukla Sep 05 '18 at 21:29
  • @Vivek_Shukla It might also do some name transformation, not just passing as a flag but adding something to the name. For example, on Windows it might add `.a` or `.lib` suffix (depends on CMake variable) to the library name before passing it to the linker. – ixSci Sep 06 '18 at 06:27