Reputation: 15
I've this example code.
#include <iostream>
#include <boost/filesystem.hpp>
using namespace std;
int main()
{
return 0;
}
It can be correctly build with: g++ -lboost_system-mt -lboost_filesystem-mt 1.cpp
But if I add -static
, then it complains:
/tmp/cc1JEbRQ.o: In function `__static_initialization_and_destruction_0(int, int)':
1.cpp:(.text+0xb0): undefined reference to `boost::system::get_system_category()'
1.cpp:(.text+0xba): undefined reference to `boost::system::get_generic_category()'
1.cpp:(.text+0xc4): undefined reference to `boost::system::get_generic_category()'
1.cpp:(.text+0xce): undefined reference to `boost::system::get_generic_category()'
1.cpp:(.text+0xd8): undefined reference to `boost::system::get_system_category()'
collect2: ld returned 1 exit status
How do I fix that? Thanks
Upvotes: 0
Views: 666
Reputation: 136306
You probably need to reverse the order of your libraries for static linking to succeed, because boost_filesystem
depends on boost_system
:
g++ 1.cpp -static -lboost_filesystem-mt -lboost_system-mt
This is because the run-time linker does a topological dependency sort to load shared libraries in correct order, whereas static linking doesn't do that.
Alternatively, you can force static linking to do several passes over the list of libraries to try to resolve remaining undefined symbols:
man ld
:
-( archives -)
--start-group archives --end-group
The archives should be a list of archive files. They may be either
explicit file names, or -l options.
The specified archives are searched repeatedly until no new
undefined references are created. Normally, an archive is searched
only once in the order that it is specified on the command line.
If a symbol in that archive is needed to resolve an undefined
symbol referred to by an object in an archive that appears later on
the command line, the linker would not be able to resolve that
reference. By grouping the archives, they all be searched
repeatedly until all possible references are resolved.
Using this option has a significant performance cost. It is best
to use it only when there are unavoidable circular references
between two or more archives.
e.g:
g++ 1.cpp -static -Wl,--start-group -lboost_system-mt -lboost_filesystem-mt -Wl,--end-group
Upvotes: 1