【问题标题】:How to link external library in Clion/Cmake? [duplicate]如何在 Clion/Cmake 中链接外部库? [复制]
【发布时间】:2019-09-05 01:44:41
【问题描述】:

我是 Cmake 新手,在链接外部库 (libtiff) 时遇到问题。我已经安装了 libtiff,它在我的 /usr/local/include 中。然后我在我的 Cmake 中使用了include_directories()target_linked_libraries()。但是,它仍然给了我

ld: library not found for -ltiff

main.c:

#include <stdio.h>
#include "tiffio.h"

int main() {
    printf("Hello, World!\n");
    return 0;
}

cmake 文件:

cmake_minimum_required(VERSION 3.13)

project(test2 C)

set(CMAKE_C_STANDARD 99)

include_directories(/usr/local/include)

add_executable(test2 main.c)

target_link_libraries(test2 tiff)

如果您能提供帮助,我将不胜感激!提前致谢!

【问题讨论】:

  • 使用export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:&lt;new_lib_directory&gt;
  • 应该 是 usr/local/include 还是 usr/local/lib?我在哪里添加它?谢谢! @MiguelÁngelRetamozoSanchez。
  • 使用find_library“找到”它。 CMake 在使用库时添加正确的标志,以便它可以链接并在运行时使用(对于动态库)。
  • 请注意,使用LD_LIBRARY_PATHnot always recommended

标签: c cmake clion


【解决方案1】:

导入库而不是链接目录。

# Your-external "mylib", add GLOBAL if the imported library is located in directories above the current.
    add_library( mylib SHARED IMPORTED )
    # You can define two import-locations: one for debug and one for release.
    set_target_properties( mylib PROPERTIES IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/res/mylib.so ) 

像这样链接库

TARGET_LINK_LIBRARIES(GLBall mylib)

请参阅此链接以了解导入 libaray https://cmake.org/cmake/help/v2.8.8/cmake.html#command:add_library

【讨论】:

    猜你喜欢
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 2021-09-17
    相关资源
    最近更新 更多