Why does gcc report “implicit declaration of function ‘round’”?

一个人想着一个人 提交于 2019-12-04 03:34:09

I see you're using gcc.

By default, gcc uses a standard similar to C89. You may want to "force" it to use the C99 standard (the parts it complies with)

gcc -std=c99 -pedantic ...

Quote from GCC Manual

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.

Something must be wrong with your gcc installation, system headers, or compilation options.

Try compiling with -E. That will show you what the preprocessor output -- including which headers are being included and what's in them. On my Ubuntu Linux system it's about 1000 lines of output, including this:

extern double round (double __x) __attribute__ ((__nothrow__)) __attribute__ ((__const__));

You need to tell gcc that you want C99, and that you want to link in libm:

gcc -std=c99 -lm round_test.c

The code you type compiles cleanly on MacOS X 10.5.8 with GCC 4.0.1. If prodded with options '-Wall -Wextra', it complains about unused parameters argc and argv - not material.

Have you looked in <math.h> on your machine?

Have you tried with options such as '-stc=c99'?

C99 was the answer, but the full story is a little more complicated. The reason I'd been playing with this at all was that I was trying to compile a library written for Windows, which had its own "optimised" definition of round(). I got a linker error telling me that the definition conflicted with the built-in, so I removed the definition (and declaration). Once I'd done that I started to get the "implicit declaration error".

It seems that the default compile mode (without the -std=c99 flag) is neither conforming C89 nor C99: if it were conforming C89, you should be able to provide a custom definition of round() without conflicting, and if it were conforming C99 the declaration should be in math.h.

you need to link with the math library. So when you compile, be sure to add the -lm flag.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!