gcc report : incompatible implicit declaration of built-in function ‘round’

I have a simple C program with use round function. This is my simple code (save as main.c) :

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char **argv)
{
	float a, b;
	int c, d;

	a = 5.6;
	b = 5.3;

	c = round(a);
	d = round(b);
	printf("a=%2.2f , round = %i \n", a, c);
	printf("b=%2.2f , round = %i \n", b, d);

	return(1);
}

When I compile this code with command :

gcc main.c -lm -o tesround

I get this warning :

warning: incompatible implicit declaration of built-in function ‘round’

When I read GCC manual, I get this information :

By default, GCC provides some extensions to the C language that on rare occasions conflict with the C standard. See Extensions to the C Language Family. Use of the -std options listed above will disable these extensions where they conflict with the C standard version selected. You may also select an extended version of the C language explicitly with -std=gnu89 (for C89 with GNU extensions) or -std=gnu99 (for C99 with GNU extensions). The default, if no C language dialect options are given, is -std=gnu89; this will change to -std=gnu99 in some future release when the C99 support is complete. Some features that are part of the C99 standard are accepted as extensions in C89 mode.

We can solve this problem  ( warning: incompatible implicit declaration of built-in function ‘round’ ) with add FLAGS -std=c99 when compile this code. So, new command to compile this code become :

gcc main.c -lm -o tesround -std=c99

Or we can create our function to replace round function in math.h. Create new function with name roundfunc to replace round function. So, our code become :

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define roundfunc(x) ((int)((x)>0.0?(x)+0.5:(x)-0.5))
int main(int argc, char **argv)
{
	float a, b;
	int c, d;

	a = 5.6;
	b = 5.3;

	// METHOD I
	c = round(a);
	d = round(b);
	printf("METHOD I \n");
	printf("a=%2.2f , round = %i \n", a, c);
	printf("b=%2.2f , round = %i \n", b, d);

	// METHOD II
	c = roundfunc(a);
	d = roundfunc(b);
	printf("METHOD II \n");
	printf("a=%2.2f , round = %i \n", a, c);
	printf("b=%2.2f , round = %i \n", b, d);
	return(1);
}

Compile this code with command :

gcc main.c -lm -o tesround -std=c99

This is output from compared our round function with C round function :

METHOD I
a=5.60 , round = 6
b=5.30 , round = 5
METHOD II
a=5.60 , round = 6
b=5.30 , round = 5

This is a simple method how to solve warning warning: incompatible implicit declaration of built-in function ‘round’ when use round function in C. We can use -std=c99 when compile our code or create our function to replace round function.

Add a Comment

Your email address will not be published. Required fields are marked *