我是在 linux 上编程和在终端中编译的新手。我有三个文件:
sql.h
#ifndef SQL_H
#define SQL_H
#include "sqlite3.h"
#include <string>
class sqlite{
private:
sqlite3 *db;
sqlite3 *statement;
public:
sqlite(const char* filename);
void create_table();
};
#endif
sql.cpp
#include "sql.h"
#include <iostream>
sqlite::sqlite(const char* filename){
if((sqlite3_open_v2(filename, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)) == SQLITE_OK) //fájl létrehozása
std::cout << "Database has been created successfully!" << std::endl;
else{
std::cout << "Oops, something went wrong, please try again!" << std::endl;
}
}
void sqlite::create_table(/*const std::string &tableName, const std::string &columnNames*/){
//std::string command = "CREATE TABLE " + tableName + columnNames;
sqlite3_prepare_v2(db, "CREATE TABLE a (a INTEGER, b INTEGER)", -1, &statement, NULL);
sqlite3_step(statement);
sqlite3_finalize(statement);
sqlite3_close(db);
}
主文件
#include "sql.h"
#include <string>
int main(){
sqlite s = sqlite("database.db");
s.create_table();
return 0;
}
如果我尝试用 command 编译它 g++ -Wall -Werror main.cpp -lsqlite3 -o sqlite_program
,我得到了错误:
/tmp/ccKtrrtg.o: In function `main':
main.cpp:(.text+0x15): undefined reference to `sqlite::sqlite(char const*)'
main.cpp:(.text+0x21): undefined reference to `sqlite::create_table()'
这是我第一次尝试使用自定义标头编译 cpp。也许我应该用不同的命令来做到这一点?
更新:我已经更新了代码,它是错误的。:) 现在可以了!