如何通过MacOS上的pkg-config在cmake中链接ICU4C?

时间:2019-06-11 08:52:28

标签: cmake pkg-config icu4c

我在使用最新操作系统的Macbook上工作。通过自制软件安装了icu4c库。但是我无法正确链接libicuuc。如果是查找库,我将使用

export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/Cellar/icu4c/64.2/lib/pkgconfig

在我的〜/ .bashrc上,但是仍然出现链接错误。

这是一个非常简单的CMakeLists.txt:

cmake_minimum_required(VERSION 3.4)

project(xeditd CXX)

find_package(PkgConfig REQUIRED)
pkg_search_module(ICU_UC icu-uc)

set(LIB_DIR
    ${ICU_UC_LIBRARY_DIRS}
    )

set(LIB
    ${ICU_UC_LIBRARIES}
    )

set(INC_DIR
    ${ICU_UC_INCLUDE_DIRS}
    )

set(SRC_FILE
    xeditd.cpp
    )

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -o2 ${ICU_UC_CXXFLAGS_OTHER}")

message(${LIB_DIR})
message(${LIB})
message(${INC_DIR})
message(${CMAKE_CXX_FLAGS})

add_executable(xeditd ${SRC_FILE})
link_directories(${LIB_DIR})
target_link_libraries(xeditd ${LIB})
target_include_directories(xeditd PUBLIC ${INC_DIR})

它生成:

/usr/local/Cellar/icu4c/64.2/lib         (${LIB_DIR})
icuucicudata                             (${LIB})
/usr/local/Cellar/icu4c/64.2/include     (${INC_DIR})
-std=c++14 -std=c++14 -o2                (${CMAKE_CXX_FLAGS})
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/rolin/workspace/github/xedit/.bin
[ 50%] Linking CXX executable xedit
ld: library not found for -licuuc
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [xedit/xedit] Error 1
make[1]: *** [xedit/CMakeFiles/xedit.dir/all] Error 2
make: *** [all] Error 2
[ 50%] Linking CXX executable xeditd
ld: library not found for -licuuc
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [xeditd/xeditd] Error 1
make[1]: *** [xeditd/CMakeFiles/xeditd.dir/all] Error 2
make: *** [all] Error 2

我不知道为什么找不到libicuuc.dylib

1 个答案:

答案 0 :(得分:0)

link_directories仅影响之后的目标。因此,我在add_executable通话之后移动了link_directories