Answer the question
In order to leave comments, you need to log in
Why is it not connected to gcc?
Here is my code:
#include <stdio.h>
#include <math.h>
double time(float x){
return 5.0*sqrt(x*x + 36.0) + 4.0*(20.0 - x);
}
int main(){
double l = 0;
double r = 20;
for(int i = 0; i < 1000; i++){
double mid = (r+l)/2;
double tl = mid - mid/10;
double tr = mid + mid/10;
if(time(tl) < time(tr))
r = tr;
else
l = tl;
}
printf("x = %g; t = %g\n", (l+r)/2, time((l+r)/2));
return 0;
}
$ gcc ./cro.c
/tmp/ccqtObxh.o: In function `time':
cro.c:(.text+0x28): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
Answer the question
In order to leave comments, you need to log in
Library != header file.
The header file contains only declarations (ie descriptions). For example:
// time.h
#ifndef TIME_H
#define TIME_H
double time(float x);
#endif
// time.c
#include "time.h"
#include <math.h>
double time(float x){
return 5.0*sqrt(x*x + 36.0) + 4.0*(20.0 - x);
}
gcc -c time.c -o time.o # Создаём объектный файл (скомпилированый код)
ar rcs libtime.a time.o # Создаём статическую библиотеку (по сути, архив объектных файлов)
// main.c
#include <stdio.h>
#include <math.h>
#include "time.h"
int main(){
double l = 0;
double r = 20;
for(int i = 0; i < 1000; i++){
double mid = (r+l)/2;
double tl = mid - mid/10;
double tr = mid + mid/10;
if(time(tl) < time(tr))
r = tr;
else
l = tl;
}
printf("x = %g; t = %g\n", (l+r)/2, time((l+r)/2));
return 0;
}
-l
is short for link. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question