【问题标题】:Why can’t Clang find the library I specified on the command line?为什么 Clang 找不到我在命令行指定的库?
【发布时间】:2013-12-12 20:56:33
【问题描述】:

我正在为 OS X 上的动态库编写一些单元测试。动态库位于 ../MathDll。该库本身是通过以下 Makefile 构建的:

all: math.dylib

math.dylib: MathDll.cpp MathDll.h
    clang -dynamiclib MathDll.cpp -o math.dylib

clean:
    rm math.dylib

运行file math.dylib 给出

math.dylib: Mach-O 64-bit dynamically linked shared library x86_64

我有一个简单的程序test1.cpp,它将调用这个库中的一个函数。我试图用

编译它
clang -I../MathDll -L../MathDll -lmath test1.cpp

但这给了我

ld: library not found for -lmath
clang: error: linker command failed with exit code 1 (use -v to see invocation)

当我使用-v 调用 Clang 时,输出为

Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.9.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name test1.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 224.1 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.0 -I ../MathDll -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir "/Users/bdesham/Projects/New GUI/math_library/osx_test" -ferror-limit 19 -fmessage-length 118 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.9.0 -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/gc/4w1rdgzx3zz2dbxf0m_yd67c0000gn/T/test1-sL5rwc.o -x c++ test1.cpp
clang -cc1 version 5.0 based upon LLVM 3.3svn default target x86_64-apple-darwin13.0.0
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
 ../MathDll
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.0/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.9.0 -o a.out -L../MathDll -lmath /var/folders/gc/4w1rdgzx3zz2dbxf0m_yd67c0000gn/T/test1-sL5rwc.o -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.0/lib/darwin/libclang_rt.osx.a
ld: library not found for -lmath
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我一定是在这里做些傻事。我需要做什么才能让链接器找到我的库?

【问题讨论】:

  • 只是为了确保,math.dylib 是在MathDll 中生成的? (还可以考虑使用目录的绝对路径)
  • @zneak 是的:math.dylib../MathDll 中。我尝试使用 -I-L 的绝对路径运行 clang,但得到了相同的结果。

标签: macos linker clang


【解决方案1】:

您的库需要被称为 libma​​th.dylib,而不仅仅是 math.dylib。

【讨论】:

    猜你喜欢
    • 2014-01-30
    • 2016-05-27
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 2017-03-14
    • 2012-12-20
    相关资源
    最近更新 更多