How can I avoid linking directly to library files

2019-08-17 07:41发布

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)

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?

标签: c++ cmake
1条回答
仙女界的扛把子
2楼-- · 2019-08-17 08:04

Usually you should use find_package to find dependencies.

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

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

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

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

查看更多
登录 后发表回答