linux - 了解 target_link_libraries

标签 linux ubuntu cmake

我是 Linux 新手。我在 CMakeLists.txt 中看到以下内容;

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

我的问题/理解是;通过这样做

  1. 我们是在告诉 CMake 工具构建 wiringPi、序列化程序等并链接到应用程序(这是在我的代码中创建的可执行文件)吗?
  2. 所有这些图书馆位于何处?
  3. 当我做 ldconfig -p | grep <libraryname> , 对于上述库 有时我会找到如下所示的输出,有时什么也找不到,这是为什么?
  4. target_link_libraries 是否足够聪明,可以在 子目录也是?我的意思是我看到有些就在下面 user/lib 和一些在下一层,例如 /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

最佳答案

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

不完全是。该命令告诉 CMake 库 wiringPi , serializer , etc. 必须在该目标的链接阶段链接到目标“app”。它没有提到自己构建库,通常它们应该已经可用而不是构建。

  1. Where are all these libraries located?

它可以变化。编译器有一个默认的目录列表,它在其中查找库。其他 CMake 命令可以添加链接选项,将目录添加到该列表。

  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报告动态 链接器已知的库。这与编译时运行的链接器不同,两者所知道的目录和库不一定相同。原因ldconfig可能不会列出给定的库包括:

  1. 库未安装。
  2. 只安装了库的静态版本。
  3. 该库不在动态链接器默认检查的任何位置(在程序启动时至少可以通过两种不同的方式指定其他目录)。
  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

这不是 CMake 的功能,而是所选工具链及其配置的功能(在 Linux 上,通常是 GNU 工具链,具有 GCC)。通常可以安全地假设工具链默认使用所有正确的标准 库目录。有时也可以成功地指示 CMake 在其他可能的地方搜索特定的库,但是 target_link_libraries不是其中的一部分。

关于linux - 了解 target_link_libraries,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52192619/

相关文章:

c++ - CMake:从 cpp 文件自动生成 c-wrapper 文件

c++ - CMake configure_file 未包含在静态库中

cmake - 如何抑制 cmake `Generating XXX` 的 `add_custom_command` 消息?

c - 如何为多个进程实现管道?

linux - wget 中的 HTTP 请求占用了大部分时间

bash - tr 和 xargs 不删除空格字符

apache - 从 access.log 获取前 50 个 IP,包括 IPV6

c - Linux C : error of redefinition of in include headers

php: DateTime() 和 time() 有不同的时间

android - 在 Ubuntu 上安装 Cordova 会给出“...cordova/node_modules/q/q.js :126: error