如何在Clion / Cmake中链接外部库?

时间:2019-04-15 05:27:25

标签: c cmake clion

我是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)

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

1 个答案:

答案 0 :(得分: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