$ 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
inak ked vyhodis ten header a do mainu includujes sqlite.cpp tak to ide. ale nie je to ono
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
samostatny preklad v poho siel len som zabudol ze triedy narozdiel od funkcii sa iba v hlavickovych suboroch deklaruju...