我正在尝试逐步构建以下示例代码 :
#include <stdio.h>
int main(void) {
puts("hello, world");
}
这是我的Makefile
:
CC=clang
CFLAGS=-g
LD=ld
LDFLAGS=-macosx_version_min 10.12
LDLIBS=-L/usr/lib/system/
a.out: main.o
$(LD) $(LDFLAGS) $< $(LOADLIBES) $(LDLIBS)
main.o: main.c
$(CC) $(CFLAGS) $(CPPFLAGS) -c $<
创建main.o
没有问题,但在链接阶段引发错误:
$ make
ld -macosx_version_min 10.12 main.o -L/usr/lib/system/
Undefined symbols for architecture x86_64:
"_puts", referenced from:
_main in main.o
ld: symbol(s) not found for inferred architecture x86_64
make: *** [a.out] Error 1
ld
似乎无法找到C标准库。我做错了什么?
供您参考,这是clang main.c -v
的输出,它正确地进行链接:
$ clang main.c -v
Apple LLVM version 8.1.0 (clang-802.0.42)
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 main.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu penryn -target-linker-version 278.4 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir /Library/Developer/CommandLineTools/usr/bin/../lib/clang/8.1.0 -fdebug-compilation-dir /Users/sunqingyao/Projects/play-ground -ferror-limit 19 -fmessage-length 167 -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/x0/lrg8j6r535n4lvnfnj6lr2fr0000gn/T/main-f5cd6b.o -x c main.c
clang -cc1 version 8.1.0 (clang-802.0.42) default target x86_64-apple-darwin16.7.0
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/Library/Developer/CommandLineTools/usr/bin/../lib/clang/8.1.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/x0/lrg8j6r535n4lvnfnj6lr2fr0000gn/T/main-f5cd6b.o -lSystem /Library/Developer/CommandLineTools/usr/bin/../lib/clang/8.1.0/lib/darwin/libclang_rt.osx.a
我尝试将最后一行复制到Makefile
,只是为了得到file not found
错误:
$ make
/Library/Developer/CommandLineTools/usr/bin/ld -macosx_version_min 10.12 main.o -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/x0/lrg8j6r535n4lvnfnj6lr2fr0000gn/T/main-f5cd6b.o -lSystem /Library/Developer/CommandLineTools/usr/bin/../lib/clang/8.1.0/lib/darwin/libclang_rt.osx.a
ld: file not found: /var/folders/x0/lrg8j6r535n4lvnfnj6lr2fr0000gn/T/main-f5cd6b.o
make: *** [a.out] Error 1
答案 0 :(得分:6)
如今,使用ld
直接链接二进制文件是不寻常的,在macOS下,您需要添加-lc
以强制它包含标准C库。
通常,使用cc
来调用链接器会更好,并且它会安排传递正确的标记。
这个Makefile适合我:
CC=clang
CFLAGS=-g -mmacosx-version-min=10.12
main: main.o
$(CC) -o main $(CFLAGS) $(LDFLAGS) main.o