Re: Can't use -flto with -std=c99

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2013/7/2 Mac Mollison <mollison@xxxxxxxxxx>:
> My application builds fine with -flto, but only if I do not also specify
> -std=c99.
>
> If someone can help me, that would be wonderful. I have created a very
> simple test, below, to demonstrate the problem.
>
> main.c:
> #include <stdio.h>
> #include <stdlib.h>
> #include "foo.h"
> void main(int argc, char** argv) {
>         int input = atoi(argv[1]);
>         printf("%d\n", foo(input));
> }
>
>
> foo.h:
> inline int foo(int x);
>
>
> foo.c:
> #include "foo.h"
> inline int foo(int x) {
>         while (x < 900) {
>                 x += x;
>         }
>         return x;
> }
>

This is *inline definition* in C99.

It does not provide any physical definition for external use.
It just tells compiler how the inline function looks like so that
compiler can evaluate and perform inlining.

That's why you have "undefined reference to 'foo'" error message
when linking the program.

>
> Makefile:
> CFLAGS += -flto -std=c99
> LDFLAGS += -flto -std=c99
>
> main : main.o foo.o
> main.o : main.c foo.h
> foo.o : foo.c foo.h
>
> .PHONY : clean
>
> clean :
>         $(RM) main main.o foo.o
>
>
> Results of running make:
>
> cc -flto -std=c99   -c -o main.o main.c
> In file included from main.c:3:0:
> foo.h:1:12: warning: inline function ‘foo’ declared but never defined [enabled by default]
>  inline int foo(int x);
>             ^
> foo.h:1:12: warning: inline function ‘foo’ declared but never defined [enabled by default]
> cc -flto -std=c99   -c -o foo.o foo.c
> cc -flto -std=c99  main.o foo.o   -o main
> /tmp/ccTDIBGZ.ltrans0.ltrans.o:ccTDIBGZ.ltrans0.o:function main: error: undefined reference to 'foo'
> collect2: error: ld returned 1 exit status
> make: *** [main] Error 1
>
> Without the -std=c99 flags, make runs successfully and without warnings.

I had a simple case to demonstrate how to use *inline definition* under C99.
You can refer to this dicussion thread:
http://gcc.gnu.org/ml/gcc-help/2013-03/msg00173.html


Best regards,
jasonwucj





[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux