cmake 的link_libraries和target_link_libraries

  • Post author:
  • Post category:其他



文档

中说

link_libraries

已经被废弃了

Deprecated. Use the target_link_libraries() command instead.

也就是这个两个函数的作用应该差不多

一个区别是:

target_link_libraries 要在 add_executable 之后

link_libraries 要在 add_executable 之前

附,三个有效的cmakelist.txt的例子

  • target_link_libraries 版
cmake_minimum_required(VERSION 3.12)

include_directories("/usr/local/Cellar/vips/8.6.0/include")
include_directories("/usr/local/Cellar/glib/2.54.2/lib/glib-2.0/include")
include_directories("/usr/local/Cellar/glib/2.54.2/include/glib-2.0")


link_directories("/usr/local/Cellar/glib/2.54.2/lib")
link_directories("/usr/local/opt/gettext/lib")
link_directories("/usr/local/Cellar/vips/8.6.0/lib")

add_executable(vipc main.c)

target_link_libraries(vipc vips)
target_link_libraries(vipc glib-2.0)
target_link_libraries(vipc gobject-2.0)
  • link_libraries版
cmake_minimum_required(VERSION 3.12)

include_directories("/usr/local/Cellar/vips/8.6.0/include")
include_directories("/usr/local/Cellar/glib/2.54.2/lib/glib-2.0/include")
include_directories("/usr/local/Cellar/glib/2.54.2/include/glib-2.0")


link_directories("/usr/local/Cellar/glib/2.54.2/lib")
link_directories("/usr/local/opt/gettext/lib")
link_directories("/usr/local/Cellar/vips/8.6.0/lib")

link_libraries(vips)
link_libraries(glib-2.0)
link_libraries(gobject-2.0)

add_executable(vipc main.c)
  • pkg-config 版
# 查看编译参数
pkg-config vips --cflags --libs
# 使用
gcc main.c `pkg-config vips --cflags --libs`

参考:


http://www.it1352.com/516453.html



https://cmake.org/cmake/help/latest/module/FindPNG.html

find_package(pkgconfig)
pkg_search_module(VIPS required vips)

# 不知道为啥这个dir 一定要单独指定才行,按理说应该是不用的,但是如果注释掉这句,链接时会报找不到 lintl
link_directories("/usr/local/opt/gettext/lib") 

add_definitions(${VIPS_DEFINITIONS})

add_executable(vipc main.c)

target_link_libraries(vipc ${VIPS_LIBRARIES})
target_include_directories(vipc PUBLIC ${VIPS_INCLUDE_DIRS})

参考:


https://www.cnblogs.com/binbinjx/p/5626916.html



https://stackoverflow.com/questions/39598323/how-to-properly-link-libraries-with-cmake



版权声明:本文为harryhare原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。