七、CMake练习 PROJECT(CMakeProj) 4、在src文件夹中,建立mylibs和myapps文件夹 mylib.h
//---------------------------------------------------------------------------------------------------- #ifndef _MYLIB_H #define _MYLIB_H class MyLib{ public: void DoSomething(); }; #endif //---------------------------------------------------------------------------------------------------- mylib.cpp //---------------------------------------------------------------------------------------------------- #include "mylib.h" #include <iostream> void MyLib::DoSomething() { std::cout << "hi~! 我是一条来自MyLib中DoSomething的消息" << std::endl; } //---------------------------------------------------------------------------------------------------- 在mylibs文件夹中的CMakeLists.txt文件中写CMake指令: ADD_LIBRARY指令的第一个参数为库文件的名称,这里指定为mylibs,将来make之后,会生成一个名称为libmylibs.a的库文件,第二个参数为源码,可以只写文件名mylib,也可以写mylib.h空格mylib.cpp,如果头文件和源文件的名称一致简写为mylib就可以了。 myapp.cpp
#include <iostream> #include "mylib.h" int main(int argc, const char *argv[]) { MyLib.DoSomething(); return 0; } 在myapps文件夹中的CMakeLists.txt文件中写CMake指令: 6、构建
八、CMake练习的深入
九、使用其他库文件 (二)MySQL++ #INCLUDE_DIRECTORIES #LINK_DIRECTORIES 最后别忘了告诉编译器mysqlpp的依赖项 开始写一些代码试试看,已经可以很顺利的make了。 #include <cstdlib>
#include <cstdio> #include <iostream> #include <sqlite3.h> #include <mysql++.h> #include "mylib.h" using namespace std; // sqlite callback static int callback(void *NotUsed, int argc, char **argv, char **azColName){ int i; for(i=0; i<argc; i++){ printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); } printf("\n"); return 0; } // end sqlite callback int main(int argc, const char *argv[]) { // mylib MyLib().DoSomething(); // sqlite sqlite3 *db; char *zErrMsg = 0; int rc; char sql[1024]=""; sprintf(sql, "SELECT * FROM table1;"); rc = sqlite3_open("test.db", &db); if (rc) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); system("PAUSE"); exit(1); } rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); if ( rc!=SQLITE_OK ) { fprintf(stderr, "SQL error: %s\n", zErrMsg); } sqlite3_close(db); // end sqlite // mysql mysqlpp::Connection con(false); con.set_option(new mysqlpp::SetCharsetNameOption("gbk")); cout << "请输入数据库(root用户)连接密码:"; string pwd; getline(cin, pwd); if (!con.connect("mytable", "localhost", "root", pwd.c_str())) { cout << "无法连接,请检查密码是否正确!" << endl; return -1; } else { cout << "shit.终于连上了。" << endl; mysqlpp::Query query = con.query("select mycol from firsttable"); if (mysqlpp::StoreQueryResult res = query.store()) { cout << "We have:" << endl; mysqlpp::StoreQueryResult::const_iterator it; for (it = res.begin(); it != res.end(); ++it) { mysqlpp::Row row = *it; cout << '\t' << row["mycol"] << endl; // 或者使用列索引 //cout << '\t' << row[0] << endl; } } else { cerr << "Failed to get mycol list: " << query.error() << endl; return 1; } } // end mysql system("PAUSE"); return EXIT_SUCCESS; } (三)wxWidgets 之后: 下面修改myapp.cpp的代码,写一个wxWidgets的hello world,wxFrame窗口已经呈现在眼前。 myapp.cpp的代码就是wxWidgets官方网站的helloword: /*
* hworld.cpp */ #include "wx/wx.h" class MyApp: public wxApp { virtual bool OnInit(); }; class MyFrame: public wxFrame { public: MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); void OnQuit(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); DECLARE_EVENT_TABLE() }; enum { ID_Quit = 1, ID_About, }; BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(ID_Quit, MyFrame::OnQuit) EVT_MENU(ID_About, MyFrame::OnAbout) END_EVENT_TABLE() IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { MyFrame *frame = new MyFrame( _("Hello World"), wxPoint(50, 50), wxSize(450,340) ); frame->Show(true); SetTopWindow(frame); return true; } MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame( NULL, -1, title, pos, size ) { wxMenu *menuFile = new wxMenu; menuFile->Append( ID_About, _("&About...") ); menuFile->AppendSeparator(); menuFile->Append( ID_Quit, _("E&xit") ); wxMenuBar *menuBar = new wxMenuBar; menuBar->Append( menuFile, _("&File") ); SetMenuBar( menuBar ); CreateStatusBar(); SetStatusText( _("Welcome to wxWidgets!") ); } void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(TRUE); } void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) { wxMessageBox( _("This is a wxWidgets Hello world sample"), _("About Hello World"), wxOK | wxICON_INFORMATION, this); } 编译,运行。
总结: 以前喜欢用vc,vs,但vs越来越庞大,每一次的安装都有如梦魇,相信很多朋友和我一样都有同样的感受。为了有一个轻便的环境,我逐渐转移到Devc++,再到CodeBlocks,但总是会遇到这样或那样的问题没有办法解决。我知道这并不是IDE的问题,而是我不知如何解决。 学习和使用CMake,开始入手时比较困难,但我才经过总共不超过20个小时的学习,已经基本可以开始用CMake来完成大多数的日常工作了。但是还有一些迷惑,总不能不停的在命令行中敲命令吧,刚开始熟悉命令时多敲几次还过得去,命令已经非常熟悉了,而且使用频率也很高,不停的敲那也太麻烦了,在第三篇学习记录中解决这个问题,地址在这里: http://www.cnblogs.com/ode/archive/2011/08/04/2152251.html
参考资料:
|
|