我是新手,在linux上编程,并在终端编译。我有三个文件:Linux g ++编译
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 + culomnNames;
sqlite3_prepare_v2(db, "CREATE TABLE a (a INTEGER, b INTEGER)", -1, &statement, NULL);
sqlite3_step(statement);
sqlite3_finalize(statement);
sqlite3_close(db);
}
的main.cpp
#include "sql.h"
#include <string>
int main(){
sqlite s = sqlite("database.db");
s.create_table();
return 0;
}
如果我尝试用命令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。也许我应该用不同的命令来做到这一点?
更新:我更新了代码,它是越野车。 :)现在它的作品!
编译器并不神奇,你必须告诉它你正在编译什么(或者在这种情况下是链接)。 – 2013-07-22 21:00:46
[Undefined reference C++]的可能重复(http://stackoverflow.com/questions/6284720/undefined-reference-c) – 2013-07-22 21:02:18