Το πρόγραμμα:
#include <stdio.h>
// does-not-compile
int main(void) {
printf("%.2f\n", sqrt(3));
return 0;
}
$ gcc -o main main.c
main.c: In function 'main':
main.c:4:20: error: implicit declaration of function 'sqrt'
[-Wimplicit-function-declaration]
main.c:2:1: note: include '<math.h>' or provide a declaration of 'sqrt'
Προσθέτουμε #include <math.h> και ξανατρέχουμε:
$ gcc -o main main.c
/usr/bin/ld: /tmp/ccaw9D8e.o: in function `main':
main.c:(.text+0x1f): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
Τι είδους error είναι αυτό τώρα; Παρατηρήστε ότι, σε αντίθεση με τον compiler, το μήνυμα δεν είναι και πολύ βοηθητικό. Έχει κανείς ιδέα γιατί;
./main να τυπώσει 1.73;Ποιο πρόγραμμα βγάζει κάθε μήνυμα (δείτε τη λέξη ld); Ένα αρχείο .h δίνει
δηλώσεις, όχι υλοποιήσεις: σκεφτείτε σε ποιο object file ή βιβλιοθήκη βρίσκεται η
sqrt και πώς το λέτε στον linker.
Αριθμός στον οδηγό: Α26.4
(στο κεφάλαιο) ·
Μόνιμο αναγνωριστικό: slides-lecmake-sqrt-errors ·
Σύνδεσμος: https://progintro.github.io/study/questions/slides/slides-lecmake-sqrt-errors.html ·
Markdown (GitHub)