In the following example, we have
cdb.cpp and
cdb.h
1a) Compile it into an object file: g++ -c cdb.cpp -o cdb.o
1b) If MySQL++ is installed in a non-standard path: g++ -c cdb.cpp -o cdb.o -I/usr/include/mysql++
2) Create a static library: ar rcs libcdb.a cdb.o
This gives us:
libcdb.a (a reusable library) and cdb.h (which gets included when needed).
Example usage (main.cpp);
#include "cdb.h"
int main()
{
Cdb db;
db.set_db((char*)"MY_DB");
if (db.is_connected)
{
std::cout << "Connected!" << std::endl;
}
return 0;
}
3a) Compile the above with:
g++ main.cpp -L. -lcdb -lmysqlpp -o myapp
or sometimes -lmysqlclient is also required:
g++ main.cpp -L. -lcdb -lmysqlpp -lmysqlclient -o myapp
3b) Alternatively - Compile the above example (main.cpp), without creating a library with:
g++ main.cpp cdb.cpp -lmysqlpp -o myapp
or, again sometimes -lmysqlclient is also required:
g++ main.cpp cdb.cpp -lmysqlpp -lmysqlclient -o myapp
WORKING EXAMPLE
With the following files:
main.cpp - from above.
cdb.cpp - modified from Dalzell source, this version on my GitHub.
cdb.h - modified from Dalzell source, this version on my GitHub.
SimpleIni.h - from Dalzell source (used by cdb,h).
ConvertUTF.h - from Dalzell source (used by SimpleIni.h).
We can build a test with the following (similar to step 3b above):
g++ main.cpp cdb.cpp -I/usr/include/mysql++ -I/usr/include/mysql -L/usr/lib64 -lmysqlpp -lmysqlclient -o myapp
We can test with ./myapp which will probably give a Couldnt find ini file : db.ini
Note: while the above will test the environment, so further test, this also requires a db.ini file in the same directory, with the following format:
-- Some comment
[MY_DB]
host=192.168.254.198
user=some_user
pass=user_password
dbname=some_db
port=3306
The db.set_db(...) from main.cpp is used to read the settings from this db.ini file, without which we get a Couldnt find ini file : db.ini
If however we wanted to follow steps 1, 2 & 3a from above and create a library, we would do:
g++ -c cdb.cpp -I/usr/include/mysql++ -I/usr/include/mysql
then create a static library:
ar rcs libcdb.a cdb.o
then to use it: g++ main.cpp -L. -lcdb -lmysqlpp -lmysqlclient -o myapp
TBC
No comments:
Post a Comment
Note: only a member of this blog may post a comment.