首页 > 解决方案 > CMake:在构建过程中找不到库

问题描述

我正在尝试设置一个可以试用 GLib 教程的工作区。我是 C 编程的新手,也是构建的新手。

make运行命令时出现以下错误。

/Applications/CMake.app/Contents/bin/cmake -S/Users/foo/workspaces/personal/c/glib_tut/hello_world -B/Users/foo/workspaces/personal/c/glib_tut/hello_world --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/CMake.app/Contents/bin/cmake -E cmake_progress_start /Users/foo/workspaces/personal/c/glib_tut/hello_world/CMakeFiles /Users/foo/workspaces/personal/c/glib_tut/hello_world/CMakeFiles/progress.marks
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/Makefile2 all
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/depend
cd /Users/foo/workspaces/personal/c/glib_tut/hello_world && /Applications/CMake.app/Contents/bin/cmake -E cmake_depends "Unix Makefiles" /Users/foo/workspaces/personal/c/glib_tut/hello_world /Users/foo/workspaces/personal/c/glib_tut/hello_world /Users/foo/workspaces/personal/c/glib_tut/hello_world /Users/foo/workspaces/personal/c/glib_tut/hello_world /Users/foo/workspaces/personal/c/glib_tut/hello_world/CMakeFiles/hello.dir/DependInfo.cmake --color=
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/build
[ 50%] Linking C executable hello
/Applications/CMake.app/Contents/bin/cmake -E cmake_link_script CMakeFiles/hello.dir/link.txt --verbose=1
/Library/Developer/CommandLineTools/usr/bin/cc  -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/hello.dir/src/hello.c.o  -o hello  -lglib-2.0 -lintl
ld: library not found for -lglib-2.0
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [hello] Error 1
make[1]: *** [CMakeFiles/hello.dir/all] Error 2
make: *** [all] Error 2

以下是我的 CmakeLists.txt

cmake_minimum_required(VERSION 3.10)

project (hello_world_project)
include_directories(include)

find_package(PkgConfig REQUIRED)
pkg_check_modules(GLIB REQUIRED glib-2.0)

message(INFO "libs:" ${GLIB_LIBRARIES})
message(INFO "includes:" ${GLIB_INCLUDE_DIRS})

file(GLOB SOURCES "src/*.c")

#Create an executable
add_executable(hello ${SOURCES})

target_include_directories(hello PUBLIC 
    ${GLIB_INCLUDE_DIRS}
)

target_link_libraries(hello PUBLIC 
    ${GLIB_LIBRARIES}
)

我的 cmake 命令使用以下日志成功完成:

-- The C compiler identification is AppleClang 10.0.1.10010046
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: /usr/local/bin/pkg-config (found version "0.29.2")
-- Checking for module 'glib-2.0'
--   Found glib-2.0, version 2.62.5
INFOlibs:glib-2.0intl
INFOincludes:/usr/local/Cellar/glib/2.62.5/include/glib-2.0/usr/local/Cellar/glib/2.62.5/lib/glib-2.0/include/usr/local/opt/gettext/include/usr/local/Cellar/pcre/8.44/include
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/foo/workspaces/personal/c/glib_tut/hello_world

有人可以指导我解决这个问题吗?提前致谢

标签: cmakefilecmakeglib

解决方案


这是 a 的编译时错误,Static LibraryStatic Linker

ld: library not found for -l<Library_name>

当您Library not found for没有包含指向Library Search Paths

ld表示Static Linker找不到库的位置。作为开发人员,您应该帮助链接器并指出Library Search Paths

Build Settings -> Search Paths -> Library Search Paths

推荐阅读