【问题标题】:CMake: library not found during make build [duplicate]CMake:在构建过程中找不到库[重复]
【发布时间】:2020-02-23 13:04:28
【问题描述】:

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

运行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

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

【问题讨论】:

  • 你是如何把 GLib 放到你的机器上的?您是否以某种方式安装了它?它已经安装了吗?
  • 您忘记指定库所在的目录:link_directories(${GLIB_LIBRARY_DIRS})。请注意,此调用应在之前 add_executable 发出。另请参阅重复问题的其他答案,例如the one 使用 IMPORTED 目标。
  • @squareskittles 我通过自制软件安装
  • @Tsyvarev 照你说的做为我解决了这个问题。谢谢。

标签: c makefile cmake glib


【解决方案1】:

这是 Static Library 的编译时错误,由 Static Linker 处理

ld: library not found for -l<Library_name>

当您没有包含指向 Library Search Paths 的库路径时,您可能会收到错误 Library not found for

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

Build Settings -> Search Paths -> Library Search Paths

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 2018-06-18
    • 2019-12-29
    • 2022-12-04
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多