如何避免在cmake中直接链接到库文件? [英] How can I avoid linking directly to library files in cmake?

查看:109
本文介绍了如何避免在cmake中直接链接到库文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Mac上使用cmake编写一个c项目。我用自制的。我通过自制软件安装了libpqxx。我有以下CMakeLists.txt。

I'm writing a c project using cmake on a mac. I use homebrew. I installed libpqxx via homebrew. I have the following CMakeLists.txt.

cmake_minimum_required(VERSION 3.11)
project(imagedb)

set(CMAKE_CXX_STANDARD 14)
add_executable(imagedb main.cpp)

target_link_libraries( imagedb /usr/local/lib/libpqxx.dylib)

虽然构建了此版本,但我想避免在此处使用绝对路径。假设 / usr / local 已经在路径前缀中,我如何使用cmake做到这一点?

While this builds I would like to avoid using an absolute path here. How can I do this using cmake given that /usr/local is already in the path prefix?

推荐答案

通常,您应该使用 find_package 查找依赖项。

Usually you should use find_package to find dependencies.

查看 libpqxx 的存储库,您可以看到它们提供了CMake配置文件。

Looking at libpqxx's repo, you can see that they provide a CMake config file.

因此,在您的 CMakeLists.txt 文件中,您只需执行以下操作:

So in your CMakeLists.txt file you just have to do this:

find_package(libpqxx REQUIRED)
target_link_libraries(imagedb libpqxx::pqxx_shared)

如果由于某些原因 libpqxx-config.cmake 未随自制版本一起提供,则必须编写查找文件

If for some reason libpqxx-config.cmake is not shipped with the homebrew version, you have to write a find file.

这篇关于如何避免在cmake中直接链接到库文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆