代码之家  ›  专栏  ›  技术社区  ›  anquegi

用clang编译C程序的readline支持

  •  0
  • anquegi  · 技术社区  · 6 年前

    尝试使用clang在Mac OS X上编译:

    #include <stdio.h>
    #include <stdlib.h>
    #include <editline/readline.h>
    
    int
    main(int argc, char *argv[])
    {
        char *buffer = readline("> ");
        if (buffer) {
            printf("You entered: %s\n", buffer);
            free(buffer);
        }
    
        return 0;
    }
    

    并导出:

    export LDFLAGS="-L/usr/local/opt/readline/lib"
    

    我得到以下错误:

    clang -v simple-readline.c                                                                                                       Apple LLVM version 9.0.0 (clang-900.0.39.2)A
    Target: x86_64-apple-darwin16.7.0
    Thread model: posix
    InstalledDir: /Library/Developer/CommandLineTools/usr/bin
     "/Library/Developer/CommandLineTools/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.12.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name simple-readline.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fno-strict-return -masm-verbose -munwind-tables -target-cpu penryn -target-linker-version 305 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir /Library/Developer/CommandLineTools/usr/lib/clang/9.0.0 -fdebug-compilation-dir /Users/toni/learn/smalltalk/autocomplete -ferror-limit 19 -fmessage-length 158 -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.12.0 -fencode-extended-block-signature -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/zs/t9wnzpqj2bdgjjgjwb8pqxc80000gn/T/simple-readline-a88196.o -x c simple-readline.c
    clang -cc1 version 9.0.0 (clang-900.0.39.2) default target x86_64-apple-darwin16.7.0
    #include "..." search starts here:
    #include <...> search starts here:
     /usr/local/include
     /Library/Developer/CommandLineTools/usr/lib/clang/9.0.0/include
     /Library/Developer/CommandLineTools/usr/include
     /usr/include
     /System/Library/Frameworks (framework directory)
     /Library/Frameworks (framework directory)
    End of search list.
     "/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.12.0 -o a.out /var/folders/zs/t9wnzpqj2bdgjjgjwb8pqxc80000gn/T/simple-readline-a88196.o -lSystem /Library/Developer/CommandLineTools/usr/lib/clang/9.0.0/lib/darwin/libclang_rt.osx.a
    Undefined symbols for architecture x86_64:
      "_readline", referenced from:
          _main in simple-readline-a88196.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   tadman    6 年前

    你想使用阅读线库吗?如果是这样,你也需要把它联系起来。

    LDFLAGS 只说明在哪里查找库,虽然这很重要,但不会在链接过程中自动包含任何库。

    直接的 clang 链接到 readline 图书馆与 -l :

    clang -v simple-readline.c -lreadline