2012-02-28 4 views
0

У меня есть этот пример кода.Не удается создать статическую файловую систему boost

#include <iostream> 
#include <boost/filesystem.hpp> 
using namespace std; 

int main() 
{ 
    return 0; 
} 

Это может быть правильно строить с: g++ -lboost_system-mt -lboost_filesystem-mt 1.cpp

Но если добавить -static, то он жалуется:

/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 

Как это исправить? Благодаря

ответ

1

Вы, вероятно, необходимо изменить порядок библиотек для статической привязки, чтобы добиться успеха, потому что boost_filesystem зависит от boost_system:

g++ 1.cpp -static -lboost_filesystem-mt -lboost_system-mt 

Это происходит потому, что во время выполнения линкер делает своего род топологической зависимости для распределения нагрузки библиотеки в правильном порядке, тогда как статическая связь не делает этого.

В качестве альтернативы, вы можете заставить статические ссылки, чтобы сделать несколько проходов по списку библиотек, чтобы попытаться решить оставшиеся неопределенные символы:

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. 

например:

g++ 1.cpp -static -Wl,--start-group -lboost_system-mt -lboost_filesystem-mt -Wl,--end-group 
Смежные вопросы