CMake使用方法
用MinGW和CMake搭建便捷的C/C++开发环境(二)七、CMake练习 PROJECT(CMakeProj) 4、在src文件夹中,建立mylibs和myapps文件夹 // ---------------------------------------------------------------------------------------------------- #ifndef_MYLIB_H #define _MYLIB_H class MyLib{ public : void DoSomething(); }; #endif mylib.cpp #include " mylib.h " #include < iostream > 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就可以了。 #include #include int main( argc, const char * argv[]) { MyLib.DoSomething(); return 0 ; } 在myapps文件夹中的CMakeLists.txt文件中写CMake指令: 6、构建 八、CMake练习的深入 九、使用其他库文件 (二)MySQL++ #INCLUDE_DIRECTORIES #LINK_DIRECTORIES 最后别忘了告诉编译器mysqlpp的依赖项 开始写一些代码试试看,已经可以很顺利的make了。 #include cstdlib cstdio sqlite3.h#include mysql ++ .h #include using namespace std; sqlitecallback static callback( NotUsed,0)">** argv,0)">azColName){ i; for (i = ;i argc;i ){ printf( %s=%sn ,azColName[i],argv[i] ? argv[i]: NULL ); } printf( n ); ; } endsqlitecallback argv[]) { mylib MyLib().DoSomething(); sqlite sqlite3 db; zErrMsg = ; rc; sql[ 1024 ] "" ; sprintf(sql, SELECT*FROMtable1; ); rc sqlite3_open( test.db & db); if (rc) { fprintf(stderr,0)">Can'topendatabase:%sn PAUSE ); exit( 1 ); } rc sqlite3_exec(db,sql,callback,0)">zErrMsg); (rc != SQLITE_OK) { fprintf(stderr,0)">SQLerror:%sn endsqlite mysqlpp::Connectioncon( false ); con.set_option( new mysqlpp::SetCharsetNameOption( gbk )); cout 请输入数据库(root用户)连接密码: string pwd; getline(cin,pwd); ( ! con.connect( mytable localhost root 无法连接,请检查密码是否正确! endl; - else { cout shit.终于连上了。 endl; mysqlpp::Queryquery con.query( selectmycolfromfirsttable (mysqlpp::StoreQueryResultres query.store()){ cout Wehave: endl; mysqlpp::StoreQueryResult::const_iteratorit; (it res.begin();it res.end(); it){ mysqlpp::Rowrow it; cout ' t ' row[ mycol 或者使用列索引 cout<<'t'<<row[0]<<endl; } } { cerr Failedtogetmycollist: query.error() ; } } endmysql system( EXIT_SUCCESS; } (三)wxWidgets 之后: 下面修改myapp.cpp的代码,写一个wxWidgets的hello world,wxFrame窗口已经呈现在眼前。 myapp.cpp的代码就是wxWidgets官方网站的helloword: /**hworld.cpp */ wx/wx.h MyApp: wxApp { virtual bool OnInit(); }; MyFrame: wxFrame { : MyFrame( wxString title,0)">wxPoint pos,0)">wxSize size); OnQuit(wxCommandEvent event OnAbout(wxCommandEvent ); DECLARE_EVENT_TABLE() }; enum { ID_Quit MyApp::OnInit() { MyFrame frame MyFrame(_( HelloWorld ),wxPoint( 50 450 340 )); frame -> Show( true ); SetTopWindow(frame); ; } MyFrame::MyFrame( size) :wxFrame(NULL,title,pos,size) { wxMenu menuFile wxMenu; menuFile Append(ID_About,_( &About... )); menuFile AppendSeparator(); menuFile Append(ID_Quit,0)">E&xit )); wxMenuBar menuBar wxMenuBar; menuBar Append(menuFile,0)">&File )); SetMenuBar(menuBar); CreateStatusBar(); SetStatusText(_( WelcometowxWidgets! )); } MyFrame::OnQuit(wxCommandEvent WXUNUSED( )) { Close(TRUE); } MyFrame::OnAbout(wxCommandEvent )) { wxMessageBox(_( ThisisawxWidgetsHelloworldsample AboutHelloWorld | wxICON_INFORMATION,255)">this ); } 编译,运行。 总结: 以前喜欢用vc,vs,但vs越来越庞大,每一次的安装都有如梦魇,相信很多朋友和我一样都有同样的感受。为了有一个轻便的环境,我逐渐转移到Devc++,再到CodeBlocks,但总是会遇到这样或那样的问题没有办法解决。我知道这并不是IDE的问题,而是我不知如何解决。 学习和使用CMake,开始入手时比较困难,但我才经过总共不超过20个小时的学习,已经基本可以开始用CMake来完成大多数的日常工作了。但是还有一些迷惑,总不能不停的在命令行中敲命令吧,刚开始熟悉命令时多敲几次还过得去,命令已经非常熟悉了,而且使用频率也很高,不停的敲那也太麻烦了,在第三篇学习记录中解决这个问题,地址在这里: 来源:http://www.cnblogs.com/ode/archive/2011/08/04/2152251.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |