2017-09-01 4 views
1

Je suis en train de construire le code exemple suivant en mode étape par étape:ne peut pas lier à la bibliothèque standard C sur macOS

#include <stdio.h> 

int main(void) { 
    puts("hello, world"); 
} 

Voici mon 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 $< 

Il n'a pas des problèmes pour créer main.o, mais renvoie une erreur dans l'étape de liaison:

$ 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 

Il semble que ld ne trouve pas la bibliothèque standard C. Qu'est-ce que je fais mal?


À titre de référence, ceci est la sortie de clang main.c -v, qui fait le lien correctement:

$ 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 

J'ai essayé de copier la dernière ligne dans le Makefile, pour obtenir une erreur 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 
+3

"Il semble que' ld' ne peut pas trouver la bibliothèque standard C." Mais ce n'est pas le travail de 'ld' * pour trouver la bibliothèque C! 'ld' peut être utilisé avec plusieurs langues. Si vous utilisez "raw" 'ld' comme ceci, c'est votre travail de lui dire de quelles bibliothèques vous avez besoin. Ou (comme mentionné dans la réponse de @Alnitak), si vous utilisez 'cc' et laissez-le s'appeler' ld' pour vous, c'est le travail de 'cc'. (Ce qui est logique: il est évident que le compilateur C doit savoir où trouver la bibliothèque C!) –

Répondre

6

Il est rare de nos jours d'utiliser ld directement pour lier un binaire et sous Mac OS vous devez ajouter -lc pour le forcer à inclure la bibliothèque C standard.

Généralement, il est préférable d'utiliser cc pour appeler l'éditeur de liens, et il s'arrangera pour passer les bons drapeaux.

Ce Makefile fonctionne pour moi:

CC=clang 
CFLAGS=-g -mmacosx-version-min=10.12 

main: main.o 
    $(CC) -o main $(CFLAGS) $(LDFLAGS) main.o