line 12 , you say "void A( );" say instead: void A(){};
That solved it for me, using gcc7.2 On Sun, Dec 3, 2017 at 4:56 PM, chengjian (D) <cj.chengj...@huawei.com> wrote: > I have written a simple code like this > > ```c > #include <stdlib.h> > #include <stdio.h> > > //#define CONFIG_TARGET_X86_64 > > #ifdef CONFIG_TARGET_X86_64 > static void A( ) > { > printf("A\n"); > } > #else > void A( ); > #endif > > static void B( ) > { > printf("B\n"); > } > > > static int xx( ) > { > #ifdef CONFIG_TARGET_X86_64 > return 1; > #else > return 0; > #endif > } > > int main(void) > { > if (xx( )) /* define CONFIG_TARGET_X86_64 */ > A( ); > else > B( ); > } > ``` > > If we don't define the CONFIG_TARGET_X86_64, xx( ) will always return FALSE, > so functiopn A which is only declared, but not implemented will never be > called(dead code). > > compile it with gcc -O0 > > ```cpp > /tmp/cctSpgGk.o: In function `main': > 1.c:(.text+0x34): undefined reference to `A' > collect2: error: ld returned 1 exit status > ``` > > But it can be compiled by -O1 or higher. > > use GCC V6.1.0. > > I can declare A as weak: > > ```cpp > void A (void) __attribute__ ((weak)); > ``` > > Then the linker will ignore the undefined symbol reference, but a call to > this function will lead to a crash > > > So my question is : > It seems that one of the optimization options in the -O1 option eliminates > the dead code, I have seen the optimize doccument about GCC > > https://gcc.gnu.org/onlinedocs/gcc-6.4.0/gcc/Optimize-Options.html > > but I can't find it. > > So if I just want to compile this code under the -O0 option, Is it possible > ? Are there some optimization flags help me to do this? > > Thanks. >