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
You probably need to reverse the order of your libraries for static linking to succeed, because
boost_filesystemdepends onboost_system: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:e.g: