Glam Prestige Journal

Bright entertainment trends with youth appeal.

i don't understand how to compile this.

I've didnt put all of the functions that i've made in this library because all of them work properly, and it's the first time that i have to use math.h

Until now i've compiled like this without issues:

gcc -c -g f.c
gcc -c -g main.c
gcc -o main main.o f.o

I've tried to insert -lm but i don't get how and where it has to be putted.

//header

#include<math.h>
#define MAX 5
typedef enum {FALSE, TRUE} bool;
typedef enum {ERROR=-1, OK=1} status;
status parse_int(char s[], int *val);

//function

#include<stdio.h>
#include<math.h>
#include <stdlib.h>
#include"f.h"
status parse_int(char s[], int *val) { int l, val_convertito = 0, val_momentaneo = 0; for(l = 0; s[l] != '\0'; l++); for(int i = 0; s[i] != '\0'; i++) { if(s[i] >= '0' && s[i] <= '9') { val_momentaneo = ((int) (s[i]-48)) * ((int)exp10((double)l--)); val_convertito += val_momentaneo; *val = val_convertito; } else return ERROR; } return OK;
}

//main

#include<stdio.h>
#include<math.h>
#include <stdlib.h>
#include"f.h"
int main() { int val_con, *val, ls; char s_int[ls]; printf("Inserisci la lunghezza della stringa: "); scanf("%d", &ls); printf("\n"); printf("Inserisci l'intero da convertire: \n"); scanf("%s", s_int); val = &val_con; status F8 = parse_int(s_int, val); switch(F8) { case OK: printf("Valore convertito %d\n", val_con); break; case ERROR: printf("E' presente un carattere non numerico.\n"); break; }
}
1

1 Answer

From the exp10 manual page:

SYNOPSIS #define _GNU_SOURCE /* See feature_test_macros(7) */ #include <math.h> Link with -lm.
CONFORMING TO These functions are GNU extensions.

Since these aren't standard functions, you not only need to have #include <math.h>, but you must have the line #define _GNU_SOURCE before the #include <math.h> line.

You also need to add -lm on the link line, so you'd have

gcc -o main main.o f.o -lm

The -l options normally come after all the .o files.

The easiest way to do all this is to use make. It already knows how to compile and link C files, so you just need to tell it which files depend on which other files, and change some variables to add customizations like -g and -lm.

Create a file named Makefile with these contents:

CFLAGS += -g
LDLIBS += -lm
main: main.o f.o
main.o: main.c f.h
f.o: f.c f.h

And then just type make:

$ make
cc -g -c -o main.o main.c
cc -g -c -o f.o f.c
cc main.o f.o -lm -o main
0