GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Feb-2012, 04:56
dominover dominover is offline
New Member
 
Join Date: Feb 2012
Posts: 4
dominover is on a distinguished road

Compiling C & C++ files?


If I am using SQLite (which is written in C) and want to amalgamate it into a C++ application can I do this using a C/C++ compiler (meaning a IDE/compiler which compiles both languages)?

AND

Is this what a wrapper class is for? To link the C (SQLite sourcefiles) with those application source code files written in C++.

Thanks
dominover
  #2  
Old 23-Feb-2012, 08:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Compiling C & C++ files?


Quote:
Originally Posted by dominover
...which is written in C...into a C++ application...

Wrapper classes can add functionality, and may be worth evaluating.

However...

The header file used for compiling applications using sqlite3 functions already has the "magic" to make it possible to call the sqlite3 functions from a C++ program.

CPP / C++ / C Code:
//
// Tutorial example from [url]http://www.sqlite.org/quickstart.html[/url]
//
// Original example can be saved as a .cpp file and compiled
// the "normal" way as a C++ program.
//
// Just for kicks, I replaced printf statements with C++ I/O
// to prove that it's OK.
//
//
// Compiled with GNU compiler (version 4.1.2) on my Linux system
// with the following command line:
//
// g++ -Wall -W -pedantic sqlite3Example.cpp -osqlite3Example -lsqlite3
//
// davekw7x
//
#include <iostream>
#include <sqlite3.h>

using namespace std;

static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
    int i;
    for (i = 0; i < argc; i++) {
        cout << azColName[i] << (argv[i] ? argv[i] : "NULL") << endl;
        //printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    cout << endl;
    //printf("\n");
    return 0;
}

int main(int argc, char **argv)
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;

    if (argc != 3) {
        cerr << "Usage: " << argv[0] << " DATABASE SQL-STATEMENT" << endl;
        //fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);
        return (1);
    }
    rc = sqlite3_open(argv[1], &db);
    if (rc) {
        cerr << "Can't open database: " << sqlite3_errmsg(db) << endl;
        //fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        return (1);
    }
    rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg);
    if (rc != SQLITE_OK) {
        cerr << "SQL error: " << zErrMsg << endl;
        //fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
    }
    sqlite3_close(db);
    return 0;
}


Regards,

Dave

Footnote:
It is, in general, very straightforward to put stuff in header files so that previously-compiled C library functions can be linked into C++ programs. There are only a couple of minor (and quite obscure) 'issues" that could possibly create problems. These are non-issues with sqlite3.

Going the other way (trying to write programs that call C++ library functions from C programs) is, in general, not very practical.
__________________
Sometimes I just can't help myself...
  #3  
Old 23-Feb-2012, 10:54
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Compiling C & C++ files?


Quote:
Originally Posted by davekw7x
... possible to call the sqlite3 functions from a C++ program...
A slightly more interesting example with "barefoot" SQLite API functions (without wrappers). See Footnote.

CPP / C++ / C Code:
// testSqlite3.cpp
//
//  Prepare a data base from a command line with the following:
//    sqlite3 sqtest.db
//    sqlite> create table clients (name text);
//    sqlite> insert into clients (name) values('John Galt');
//    sqlite> insert into clients (name) values('Zaphod Beeblebrox');
//    sqlite> insert into clients (name) values('Elroy Q. Shagnasti');
//    sqlite> .quit
//
//    Then run this program to show the table entries.
//
//    This program compiled with GNU g++ as follows:
//
//    g++ -Wall -W -pedantic testSqlite3.cpp lsqlite3 -o testSqlite3
//
//  
//  davekw7x
//
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <sqlite3.h>

using namespace std;

int main(int argc, char **argv)
{

    const char *defaultName = "sqtest.db";
    const char *defaultTable = "Clients";

    const char *dbName;

    // Can optionally give data base name on command line.
    if (argc > 1) {
        dbName = argv[1];
    }
    else {
        dbName = defaultName;
    }

    // Can optionally give table name on command line.
    string cmd("Select * FROM ");
    if (argc > 2) {
        cmd += string(argv[2]);
    }
    else {
        cmd += defaultTable;
    }
    const char *sel = cmd.c_str();

    int status;
    sqlite3 *db;

    if ((status = sqlite3_open(dbName, &db)) != 0) {
        cerr << "sqlite3_open(" << dbName << ",...) failed with status " 
             << status << ": " << sqlite3_errmsg(db) << endl;
        exit(EXIT_FAILURE);
    }
    
    sqlite3_stmt *stmt;

    if ((status = sqlite3_prepare_v2(db, sel, strlen(sel) + 1, &stmt, __null)) != 0) {
        cerr << "sqlite3_prepare_v2(...," << sel << ",...) failed with status "
             << status << ": " << sqlite3_errmsg(db) << endl;
        exit(EXIT_FAILURE);
    } 
    

    int stepVal;
    int row = 0;
    while ((stepVal = sqlite3_step(stmt)) != SQLITE_DONE) {
        if (stepVal == SQLITE_ROW) {
            ++row;
            const unsigned char *name  = sqlite3_column_text(stmt, 0);
            cout << "Row " << row << ": " << name << endl;
            // Do something with this name???
        }
        else {
            cerr << "Hmmm...sqlite3_step() failed." << endl;
            exit(EXIT_FAILURE);
        }
    }
    cout << endl;
    cout << "Number of entries in the table = " << row << endl;
    return 0;
}

Here's a run, including preparation of the data base from the command line, as indicated in the comments of the program:
Code:
[dave@starkid sqlite]$ sqlite3 sqtest.db SQLite version 3.6.20 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> create table clients (name text); sqlite> insert into clients (name) values('John Galt'); sqlite> insert into clients (name) values('Zaphod Beeblebrox'); sqlite> insert into clients (name) values('Elroy Q. Shagnasti'); sqlite> .quit [dave@starkid sqlite]$ g++ -Wall -W -pedantic testSqlite3.cpp -lsqlite3 -o testSqlite3 [dave@starkid sqlite]$ ./testSqlite3 Row 1: John Galt Row 2: Zaphod Beeblebrox Row 3: Elroy Q. Shagnasti Number of entries in the table = 3

Regards,

Dave

Footnote:
This program is simply a C program transliterated to use C++ I/O, and uses none of the extra power of C++. It is only an example, but, I hope, it illustrates the point that you can use the pre-compiled SQLite3 C language library in a C++ program.
__________________
Sometimes I just can't help myself...
Last edited by davekw7x : 23-Feb-2012 at 11:49.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache newbie serving files outside documentroot Davidpcs Apache Web Server Forum 1 09-May-2011 16:56
FLTK Not installing AJk101 FLTK Forum 1 05-Feb-2011 15:43
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 06:19
Tutorial: Adding Music to your Site, WAV, MP3, Flash? abacomedia Web Design Forum 1 20-May-2004 15:10
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 02:26

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:45.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.