2011-11-23 11 views
6

Napisałem kod w pliku źródłowym sql_parse.cc z MySql 5.5.7rc. Tam użyłem vector, allocator itd., Ale kompilator nie jest połączony ze standardową biblioteką szablonów (STL). Czy ktoś może mi zasugerować, co mam zrobić?Jak połączyć STL w kod C++?

Oto msg błąd:

libsql.a(sql_parse.cc.o): In function `std::vector<std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > > >::push_back(std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > const&)': 
sql_parse.cc:(.text._ZNSt6vectorISsSaISsEE9push_backERKSs[std::vector<std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > > >::push_back(std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > const&)]+0x74): undefined reference to 
`std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 
std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > 
>::_M_insert_aux(__gnu_cxx::__normal_iterator<std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > > > >, std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > const&)' 

collect2: ld returned 1 exit status 
+1

Upewnij się, że używasz 'g ++' do połączenia. –

+0

"Jego pilne" nie pomogłoby wcale. –

+0

W zależności od tego, jak pilne jest! –

Odpowiedz

16

powinny Dałaś wiersza poleceń. Podejrzewam, że używasz gcc do łączenia, w takim przypadku powinieneś użyć opcji g++ lub dodać opcję -lstdc++.

+0

Nie próbowałem g ++ również, ale nie działa. Otrzymuje ten sam błąd. Podobnie jak to, co zrobiłem, to "sudo ./configure --prefix =/usr/local/mysql --with-plugin-partition --with-tcp-port = 3308 --with-unix-socket-path =/tmp/mysql1 .sock --with-debug CXX = g ++ " –

+0

nie, nie o to mi chodzi, miałem na myśli użycie' g ++ 'do łączenia. Śledzenie problemów związanych z procesem konfiguracji i kompilacji może być trudne. Możesz spróbować dodać 'LIBS = -lstdC++', aby skonfigurować linię, ale byłbym zaniepokojony całą rzeczą, nawet jeśli to działa ... –

+0

Hej, jak używać -lsdC++ ?? A gdzie to dodać? –

-2
#ifndef GLOBAL_H 
#define GLOBAL_H 

#include<iostream> 
#include<string> 
#include<vector> 
#include<algorithm> 
#include<cmath> 
using namespace std; 

#endif //GLOBAL_H 

to dobry początek

+1

'using namespace std' nigdy nie powinien pojawić się w nagłówku. I nie dotyczy to pytania o linkowanie. – Potatoswatter

0

undefined reference to std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > ::_M_insert_aux(__gnu_cxx::__normal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'

Ten symbol jest funkcją członkiem std::vector<std::string> i jest szablonem funkcji, więc powinno być instancja w swoim programie, a nie w C++ standardowa biblioteka.

Czy kompilujesz z opcją -fno-implicit-templates? Lub za pomocą jawnej deklaracji tworzenia instancji (np. extern) w kodzie i bez podania definicji? Uniemożliwiłoby to kompilatorowi utworzenie szablonu.

Jeśli mówisz, że kompilator nie zapewnić ukryte dawałaby następnie trzeba będzie dodać wyraźnie dla każdej instancji nieokreślonej odniesienia:

template void std::vector<std::string>::_M_insert_aux(std::vector<std::string>::iterator, std::string const&); 
0

Dostałem kod stl do kompilacji z poniższego

g++ -std=c++11 file.cpp -o prog