使用Clang编写具有Readline支持的C程序

时间:2018-08-20 16:31:48

标签: c clang

试图使用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 个答案:

答案 0 :(得分:2)

您是否要使用readline库?如果是这样,您也需要将其链接。

LDFLAGS仅表示在何处查找库,这一点很重要,但并不会在链接过程中自动包括它们。

直接clangreadline链接到-l库:

clang -v simple-readline.c -lreadline