boost - getting started
-
Hallo, ich möchte gerne Boost ml ausprobieren,
also hab ich mir die Dateien mal gezogen, den Ordner habe ich in meinem home-verzeichnis entpackt,
nunja nun wollte ich einfach mal etwas ausprobieren:
#include "boost/filesystem/operations.hpp" #include "boost/filesystem/fstream.hpp" #include <iostream> using namespace std; // Benutze fs für den Boost filesystem Namensraum namespace fs = boost::filesystem; int main() { fs::remove_all("foobar"); fs::create_directory("foobar"); ofstream file("foobar/cheeze.txt"); file << "tastes good!\n"; file.close(); if (!fs::exists("foobar/cheeze.txt")) cout << "Something is rotten in foobar\n"; return 0; }das funktioniert leider nicht ...
die compiler-einstellungen sind aber ok: -I /home/timo/boost
die includes sollte er also finden,
fehlermeldung:
/home/timo/boost/boost/system/error_code.hpp:205: undefined reference to `boost::system::get_system_category()' ... und so weiter und so fort ...das einzige beispiel das einfach so läuft ist:
#include <boost/config.hpp> #include <string> #include <iostream> #include <boost/foreach.hpp> #include <boost/bimap/bimap.hpp> #include <boost/bimap/list_of.hpp> #include <boost/bimap/support/lambda.hpp> using namespace boost::bimaps; int main() { //[ code_bimap_and_boost_foreach typedef bimap< std::string, list_of<int> > bm_type; bm_type bm; bm.insert( bm_type::value_type("1", 1) ); bm.insert( bm_type::value_type("2", 2) ); bm.insert( bm_type::value_type("3", 4) ); bm.insert( bm_type::value_type("4", 2) ); BOOST_FOREACH( bm_type::left_reference p, bm.left ) { ++p.second; /*< We can modify the right element because we have use a mutable collection type in the right side. >*/ } BOOST_FOREACH( bm_type::right_const_reference p, bm.right ) { std::cout << p.first << "-->" << p.second << std::endl; } //] // More examples BOOST_FOREACH( bm_type::right_reference p, bm.right ) { ++p.first; } BOOST_FOREACH( bm_type::left_const_reference p, bm.left ) { std::cout << p.first << "-->" << p.second << std::endl; } BOOST_FOREACH( bm_type::reference p, bm ) { ++p.right; } const bm_type & cbm = bm; BOOST_FOREACH( bm_type::const_reference p, cbm ) { std::cout << p.left << "-->" << p.right << std::endl; } BOOST_FOREACH( bm_type::const_reference p, bm ) { std::cout << p.left << "-->" << p.right << std::endl; } //[ code_bimap_and_boost_foreach_using_range BOOST_FOREACH( bm_type::left_reference p, ( bm.left.range( std::string("1") <= _key, _key < std::string("3") ) )) { ++p.second; } BOOST_FOREACH( bm_type::left_const_reference p, ( bm.left.range( std::string("1") <= _key, _key < std::string("3") ) )) { std::cout << p.first << "-->" << p.second << std::endl; } //] return 0; }wieso ist das so?
muss ich boost eventuell doch "installieren" und libs erzeugen?
(boost-jam macht mir irgendwie angst ...)
-
blooster schrieb:
muss ich boost eventuell doch "installieren" und libs erzeugen?
(boost-jam macht mir irgendwie angst ...)Das steht im "getting started" von boost alles drin. Die Mehrheit der Bibliotheken sind "header only", die funktionieren sofort. Einige müssen aber erst gebaut werden. Für Windows gibts auch schon vorgebaute.
-
du musst nicht nur die includes setzen, sondern auch gegen die boost::filesystem Bibliothek linken.
-
Warum haben die nicht eigentlich alle Libraries Header-only gemacht?
-
so, nun habe ich boost "installiert"
es existiert nun ein Verzeichnis
LIBS, in dem ich die Ordner include und lib habe.
folgendes Programm:
// Boost Dateien includieren #include "boost/filesystem/operations.hpp" #include "boost/filesystem/fstream.hpp" #include <iostream> using namespace std; // Benutze fs für den Boost filesystem Namensraum namespace fs = boost::filesystem; int main() { fs::remove_all("foobar"); fs::create_directory("foobar"); ofstream file("foobar/cheeze.txt"); file << "tastes good!\n"; file.close(); if (!fs::exists("foobar/cheeze.txt")) cout << "Something is rotten in foobar\n"; return 0; } [code] liefert folgende Fehler: [code] /home/timo/boost/LIBS/include/boost/system/error_code.hpp:205: undefined reference to `boost::system::get_system_category()' ... usw ...Compiler einstellung:
/home/timo/boost/LIBS/include
Linker:
/home/timo/boost/LIBS/lib/libboost_filesystem.sowieso funktioniert das jetzt wiederum nicht?
-
Also ich habe da einfach einen Ordner angegeben, wo die .dll's sind und ich gebe nicht jede dll einzeln an. boost holt sich da eigentlich das raus, was es braucht. Und darum sieht es bei dir so aus, als ob noch was fehlt..
-
gutefrage schrieb:
Warum haben die nicht eigentlich alle Libraries Header-only gemacht?
Weil die Kompilierzeit eben auch eine Rolle spielt. Viele Dinge in Boost sind als Templates realisiert, um Generizität zu ermöglichen. Wo das nicht nötig ist, ist eine Bibliothek angebrachter. Gewisse Dinge können zudem gar nicht im Header definiert werden.
-
es wird mir immer vernebelter ums geistige auge:
also nochmal:
compiler:
/home/timo/boost/LIBS/includeLinker:
/home/timo/boost/LIBS/lib/libboost_filesystem.so.1.40.0
/home/timo/boost/LIBS/lib/libboost_system.soError:
/home/timo/boost/boost: error while loading shared libraries: libboost_filesystem.so.1.40.0: cannot open shared object file: No such file or directoryDie libboost_filesystem.so.1.40.0 befindet sich aber in
/home/timo/boost/LIBS/lib/Was will er von mir?!
-
mal ganz ausführlich:
Eine Datei main.cpp
// Boost Dateien includieren #include "boost/filesystem/operations.hpp" #include "boost/filesystem/fstream.hpp" #include <iostream> using namespace std; // Benutze fs für den Boost filesystem Namensraum namespace fs = boost::filesystem; int main() { fs::remove_all("foobar"); fs::create_directory("foobar"); ofstream file("foobar/cheeze.txt"); file << "tastes good!\n"; file.close(); if (!fs::exists("foobar/cheeze.txt")) cout << "Something is rotten in foobar\n"; return 0; }aufruf in konsole:
g++ -o test -I /home/timo/boost/ -L /home/timo/boost/LIBS/lib/ main.cppfehlermeldungen:
ic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&)]+0x46): undefined reference to `boost::filesystem::detail::status_api(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::system::error_code&)' /tmp/ccr2qP2q.o: In function `boost::enable_if<boost::filesystem::is_basic_path<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >, bool>::type boost::filesystem::create_directory<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&)': main.cpp:(.text._ZN5boost10filesystem16create_directoryINS0_10basic_pathISsNS0_11path_traitsEEEEENS_9enable_ifINS0_13is_basic_pathIT_EEbE4typeERKS7_[boost::enable_if<boost::filesystem::is_basic_path<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >, bool>::type boost::filesystem::create_directory<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&)]+0x3a): undefined reference to `boost::filesystem::detail::create_directory_api(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' /tmp/ccr2qP2q.o: In function `bool boost::filesystem::detail::remove_aux<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&, boost::filesystem::file_status)': main.cpp:(.text._ZN5boost10filesystem6detail10remove_auxINS0_10basic_pathISsNS0_11path_traitsEEEEEbRKT_NS0_11file_statusE[bool boost::filesystem::detail::remove_aux<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&, boost::filesystem::file_status)]+0x4e): undefined reference to `boost::filesystem::detail::remove_api(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' /tmp/ccr2qP2q.o: In function `boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >::m_init(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&)': main.cpp:(.text._ZN5boost10filesystem24basic_directory_iteratorINS0_10basic_pathISsNS0_11path_traitsEEEE6m_initERKS4_[boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >::m_init(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&)]+0x3f): undefined reference to `boost::filesystem::detail::not_found_error()' main.cpp:(.text._ZN5boost10filesystem24basic_directory_iteratorINS0_10basic_pathISsNS0_11path_traitsEEEE6m_initERKS4_[boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >::m_init(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&)]+0x103): undefined reference to `boost::filesystem::detail::dir_itr_first(void*&, void*&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, boost::filesystem::file_status&, boost::filesystem::file_status&)' /tmp/ccr2qP2q.o: In function `boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >::increment()': main.cpp:(.text._ZN5boost10filesystem24basic_directory_iteratorINS0_10basic_pathISsNS0_11path_traitsEEEE9incrementEv[boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >::increment()]+0xf4): undefined reference to `boost::filesystem::detail::dir_itr_increment(void*&, void*&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, boost::filesystem::file_status&, boost::filesystem::file_status&)' /tmp/ccr2qP2q.o: In function `boost::filesystem::detail::dir_itr_imp<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >::~dir_itr_imp()': main.cpp:(.text._ZN5boost10filesystem6detail11dir_itr_impINS0_10basic_pathISsNS0_11path_traitsEEEED1Ev[boost::filesystem::detail::dir_itr_imp<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >::~dir_itr_imp()]+0x26): undefined reference to `boost::filesystem::detail::dir_itr_close(void*&, void*&)' collect2: ld returned 1 exit statuswas habe ich falsch gemacht?
-
So automatisch geht das natürlich nicht... Du musst dem G++ schon die libs mitgeben, gegen die gelinkt werden sollen, z.B.:
g++ -o test -I /home/timo/boost/ -L /home/timo/boost/LIBS/lib/ -lboost_filesystem main.cpp
-
ich kapier dieses linkerzeugs einfach nicht:
wieso funktioniert
-lboost_filesystem (in verbindung mit lboost_system)
aber mit /lib/libboost_filesystem.so lieferts jede menge fehler?
naja mit obigem klappt das kompilieren, aber das ausführen liefert:
./test: error while loading shared libraries: libboost_filesystem.so.1.40.0: cannot open shared object file: No such file or directory
selbst wenn die .so im selben verzeichnis liegt wie test
kann mir das bitte jemand so erklären dass ich mir das auch für andere fälle merken kann?
-
Weil deine Umgebung einfach keinen Schimmer hat, wo die libs liegen.Entweder
- du schreibst dir ein startup-Script, welches di Umgebungsvariable LDPATH um das DIR erweitert, wo deine boost-libs drin liegen
- in deiner .bashrc diese Erweiterung von LDPATH durchführst, dann kannst du dir das startup_script sparen.
- boost in deinem System global installierst, dass die libs + header in standrad-dirs (also /usr/Lib und /usr/include) liegen - am besten vertraust du deiner Distribution und installierst einfach die nötigen RPMs/DEBs/...
Ich empfehle 3.
Am besten mit nem ordentlichen Makefile-Generator wie cmake. Das ist dann am unproblematischsten.
-
und wieso klappt das linken mit dem exakten namen nicht?
/libboost_system.so
sollte doch eigentlich exakter wirken als -lboost_system?
-
blooster schrieb:
und wieso klappt das linken mit dem exakten namen nicht?
/libboost_system.so
sollte doch eigentlich exakter wirken als -lboost_system?
-> man gcc
-l will den Namen der Bibliothek, nicht den Dateinamen!
Wenn du das -l weglässt kannst du auch direkt einen Dateinamen angeben. Muss aber dann mit Pfadangabe passen, wird halt nicht (praktischerweise) in den link-directories nach der lib gesucht (was mit -l der Fall gewesen wäre).