Oddeleny preklad C++

Sekcia: Programovanie 05.11.2009 | 00:50
Avatar Ondrej Danko Debian | Fedora  Používateľ
caute mam jednu triedu a chcel by som ju presunut do samostatneho suboru...len stale vyhadzuje chybu:

$ g++ -o sql sqlite.cpp main.cpp -lsqlite3
/tmp/ccTUdNXD.o: In function `main':
main.cpp:(.text+0x6b): undefined reference to `sqlite::get(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::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> > > >&)'
collect2: ld returned 1 exit status


program vyzera takto:

main.cpp

#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
#include <exception>
#include "sqlite.h"
using namespace std;

int main() {
try {

vector<string> head, data;
sqlite sql;
sql.get("SELECT * FROM subjects", head, data);

// ERRORS
} catch(domain_error& e) {
cout << e.what() << "!!!" << endl;
cout << "END" << endl;
} catch(...) {
cout << "UNKNOWN ERROR!!!" << endl;
cout << "END" << endl;
}
return 0;
}

sqlite.h

#ifndef h_sqlite
#define h_sqlite
#include <vector>
#include <string>

class sqlite {
public:
int get(std::string qu, std::vector<std::string>& vcol_head, std::vector<std::string>& vdata);
};

#endif

sqlite.cpp

#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
#include <exception>
#include <sqlite3.h>
using namespace std;

class sqlite {
private:
sqlite3 *db;
char *ErrMsg, **result;
int rc, nrow, ncol;
const string file;
public:
sqlite() : ErrMsg(0), rc(0), file("data.db") {
if ((sqlite3_open(file.c_str(), &db)) == SQLITE_ERROR)
throw domain_error("DATABASE OPEN ERROR");
}
int get(string qu, vector<string>& vcol_head, vector<string>& vdata) {
rc = sqlite3_get_table(db, qu.c_str(), &result, &nrow, &ncol, &ErrMsg);

if (vcol_head.size() > 0)
vcol_head.clear();
if (vdata.size() > 0)
vdata.clear();

if (rc == SQLITE_OK) {
for (int i=0; i < ncol; i++)
vcol_head.push_back(result[i]);
for (int i=0; i < ncol*nrow; i++)
vdata.push_back(result[ncol+i]);
}

sqlite3_free_table(result);
return rc;
}
~sqlite() {
sqlite3_close(db);
}
};


jediny problem je s linkovanim...samostatne to ide prelozit
    • Re: Oddeleny preklad C++ 05.11.2009 | 01:04
      Avatar borg Fedora  Administrátor
      triedu deklaruj v headri a nie v cpp.
      inak ked vyhodis ten header a do mainu includujes sqlite.cpp tak to ide. ale nie je to ono
      • Re: Oddeleny preklad C++ 05.11.2009 | 01:22
        Avatar Ondrej Danko Debian | Fedora  Používateľ
        dik za helfnutie ;-)
    • Re: Oddeleny preklad C++ 05.11.2009 | 11:08
      Avatar Michal Nánási Ubuntu 11.04  Používateľ
      Najlepsie to je spravit takto:
      g++ -c sqlite.cpp -o sqlite.o
      g++ main.cpp -o main sqlite.o


      Inymi slovami, najskor skompilujes zvlast sqlite.cpp, a potom main.cpp a prilinkujes k tomu sqlite.o
      Hi! I'm a .signature virus! Copy me into your ~/.signature to help me spread!
      • Re: Oddeleny preklad C++ 06.11.2009 | 17:53
        Avatar Ondrej Danko Debian | Fedora  Používateľ
        najlepsie riesenie je kompilovat pomocou make :D
        samostatny preklad v poho siel len som zabudol ze triedy narozdiel od funkcii sa iba v hlavickovych suboroch deklaruju...