M
Milan Radovich
Hello,
I have 2 files:
a.cc:
---------------------------------------
int main()
{
return 0;
}
---------------------------------------
b.cc:
---------------------------------------
#include <iostream>
bool foo()
{
std::cout << "foo" << std::endl;
return true;
}
const bool bar = foo();
---------------------------------------
If I compile the application like that:
g++ -c a.cc b.cc
g++ a.o b.o -o a
then when application is executed, it outputs a string "foo".
But if I create a static library from b.cc and then link application
with that library, like that:
g++ -c a.cc b.cc
ar rcs libb.a b.o
g++ -L. -lb a.o -o a2
then when application is ran, it doesn't print anything.
After I searched for the reason why this happens, I found that
when linking with static libraries, linker omits code which is not
reachable through any execution path.
The question is:
1) Is it a correct behaviour according to a Standard?
2) Is there is a pattern which forces linker to add such a code to an
executable?
A case where such an idiom is very usefull is described in Chapter 8.3 of
Andrei Alexandrescu's "Modern C++ Design".
I have 2 files:
a.cc:
---------------------------------------
int main()
{
return 0;
}
---------------------------------------
b.cc:
---------------------------------------
#include <iostream>
bool foo()
{
std::cout << "foo" << std::endl;
return true;
}
const bool bar = foo();
---------------------------------------
If I compile the application like that:
g++ -c a.cc b.cc
g++ a.o b.o -o a
then when application is executed, it outputs a string "foo".
But if I create a static library from b.cc and then link application
with that library, like that:
g++ -c a.cc b.cc
ar rcs libb.a b.o
g++ -L. -lb a.o -o a2
then when application is ran, it doesn't print anything.
After I searched for the reason why this happens, I found that
when linking with static libraries, linker omits code which is not
reachable through any execution path.
The question is:
1) Is it a correct behaviour according to a Standard?
2) Is there is a pattern which forces linker to add such a code to an
executable?
A case where such an idiom is very usefull is described in Chapter 8.3 of
Andrei Alexandrescu's "Modern C++ Design".