4

I'm using GCC version 4.7.2. I create a static library having two files "ctest1.cpp" and "ctest2.cpp".

ctest1.cpp

#include <stdio.h>
#include "ctest2.h"
void ctest1()
{
        printf("In ctest1");
        ctest2();
}

ctest2.cpp

#include <stdio.h>
void ctest2()
{
        printf("In ctest2");
}

The header file "ctest2.h" is,

void ctest2();

And similarly the file "ctest1.h",

void ctest1();

This static library is linked to the following main file "in_test.cpp",

in_test.cpp

#include <stdio.h>
#include "ctest1.h"

using namespace std;

int main()
{
   ctest1();
   printf("InMain\n");
   return 0;
}

I was expecting that after providing proper feedback and enabling -flto, the compiler should have inlined the function call ctest2() in file "ctest1.cpp"(and even call ctest1() in "in_test.cpp"), but it doesn't. Following are the compilation steps that I follow:

g++ -Wall -c -g -O3 -fprofile-generate -ftest-coverage ctest2.cpp ctest1.cpp
ar -rcsv libtest.a ctest2.o ctest1.o
g++ -Wall -g -O3 -fprofile-generate -ftest-coverage in_test.cpp -o checking libtest.a

For training I run the executable n times, then

g++ -Wall -c -g -O3 -flto -fwhole-program -fprofile-use ctest2.cpp ctest1.cpp
ar -rcsv libtest.a ctest2.o ctest1.o
g++ -Wall -g -O3 -flto -fwhole-program -fprofile-use in_test.cpp -o checking libtest.a

I have also tried -fuse-linker-plugin where I gave the path to the gold linker in the compilation command, but the functions were not inlined. I would also like to bring to your notice that when I try this experiment without creation of a static library (with flto and feedback), the compiler inlines the function calls. In that case, I just create one executable out of all the files. This is the reason I was expecting it to work in case of static libraries. Can someone please tell me what's going wrong here ?

Akanksha
  • 41
  • 1
  • Inline optimization is always a suggestion. The compiler doesn't have to do it, if it doesn't think it would be worth and even cannot do so. Since you are building ctest2.o your execution code is held there. Up to this point I'm sure. Why -flto does not work I cannot tell. I guess there is something that will not allow reducing the code in your function. Did you try a more recent GCC? – Gerhard Stein Sep 27 '15 at 17:55

0 Answers0