0

I'm new to programming on linux, and compiling in the terminal. I have three files:

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);

}

main.cpp

#include "sql.h"
#include <string>
int main(){
    
    sqlite s = sqlite("database.db");
    s.create_table();
    return 0;
}

And if I try to compile it with the command g++ -Wall -Werror main.cpp -lsqlite3 -o sqlite_program, I got the errors:

/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()'

This is the first time, I try to compile a cpp with a custom header. Maybe I should do that with a different command?

update: I've updated the code, it was buggy. :) Now it works!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
erbal
  • 727
  • 3
  • 12
  • 32
  • The compiler is not magic, you have to tell it what you are trying to compile (or link, in this case). –  Jul 22 '13 at 21:00
  • possible duplicate of [Undefined reference C++](http://stackoverflow.com/questions/6284720/undefined-reference-c) –  Jul 22 '13 at 21:02

2 Answers2

4

Try to run this

g++ -Wall -Werror main.cpp sql.cpp -lsqlite3 -o sqlite

This will compile your sql.cpp file and link it to the executable.

austin
  • 5,816
  • 2
  • 32
  • 40
  • (What you, erbal, were missing was the input file, sql.cpp, that contained the definition of the function it was looking for.) – Sniggerfardimungus Jul 22 '13 at 20:57
  • I got this error with this: sql.cpp: In constructor ‘sqlite::sqlite(const char*)’: sql.cpp:5:3: error: ‘cout’ is not a member of ‘std’ sql.cpp:5:61: error: ‘endl’ is not a member of ‘std’ sql.cpp:8:3: error: ‘cout’ is not a member of ‘std’ sql.cpp:8:67: error: ‘endl’ is not a member of ‘std’ sql.cpp: In member function ‘void sqlite::create_table()’: .... – erbal Jul 22 '13 at 21:02
  • In your sql.cpp file (or the header file), you need to use `#include `, which has the definitions of `std::cout` and `std::endl`. – austin Jul 22 '13 at 21:09
4

you have multiple input files, which will all form a single binary.

the usual way (which scales well for larger number of files) is to compile each source-file into a binary object file, and then link all the object files into the final binary.

g++ -Wall -Werror -o main.o -c main.cpp 
g++ -Wall -Werror -o sql.o  -c sql.cpp
g++               -o sqlite    main.o sql.o -lsqlite3
umläute
  • 28,885
  • 9
  • 68
  • 122
  • The second line gives me the errors: https://www.dropbox.com/s/vqg8he8rcuf1736/show.txt?m – erbal Jul 22 '13 at 21:07
  • @erbal include in your sql.cpp – Amadeus Jul 22 '13 at 21:08
  • 1
    @erbal, that's because your code is buggy; you must include in sql.cpp, the type of the `statement` member should be `sqlite3_stmt` rather than `sqlite3` and so on. – umläute Jul 22 '13 at 21:08