{"id":15,"date":"2014-04-18T11:28:11","date_gmt":"2014-04-18T10:28:11","guid":{"rendered":"http:\/\/code.sualdea.com\/?p=15"},"modified":"2014-04-18T12:10:17","modified_gmt":"2014-04-18T11:10:17","slug":"cmake-sample","status":"publish","type":"post","link":"https:\/\/code.sualdea.com\/?p=15","title":{"rendered":"Cmake sample"},"content":{"rendered":"<p>Minimum cmake sample. It will create an executable for sample.cpp.<\/p>\n<h2>Windows<\/h2>\n<p>Download cmake-gui from <a title=\"cmake\" href=\"http:\/\/www.cmake.org\/\" target=\"_blank\">http:\/\/www.cmake.org\/<\/a>. Execute using path as follows<\/p>\n<p><a href=\"http:\/\/code.sualdea.com\/wp-content\/uploads\/2014\/04\/cmake-win2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16 size-full\" src=\"http:\/\/code.sualdea.com\/wp-content\/uploads\/2014\/04\/cmake-win2.png\" alt=\"cmake-win2\" width=\"699\" height=\"420\" srcset=\"https:\/\/code.sualdea.com\/wp-content\/uploads\/2014\/04\/cmake-win2.png 699w, https:\/\/code.sualdea.com\/wp-content\/uploads\/2014\/04\/cmake-win2-300x180.png 300w\" sizes=\"auto, (max-width: 699px) 100vw, 699px\" \/><\/a><\/p>\n<p>Configure and Generate.Choose build chain (compiler, linker &#8230;). Remeber that Visual Studio 11 means Visual Studio 2012. It will create solution and project files in build directory. Open and use it.<\/p>\n<h2>Linux<\/h2>\n<p>cd to the directory and<br \/>\n[cc lang=&#8221;bash&#8221; width=&#8221;200&#8243;]<br \/>\n$ cd build\/<br \/>\n$ cmake ..<br \/>\n$ make<br \/>\n[\/cc]<\/p>\n<p>Also ccmake can be used to configure options and debug and release configurations (remember to use first char with upper case e.g. Debug)<\/p>\n<h2>The CMake files<\/h2>\n<p>The one at main dir<br \/>\n[cc lang=&#8221;cmake&#8221;]<br \/>\ncmake_minimum_required(VERSION 2.8)<\/p>\n<p>set(BINARY_NAME &#8220;sample&#8221;)<br \/>\nset(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})<\/p>\n<p>#link static<br \/>\nset(BUILD_SHARED_LIBS OFF)<\/p>\n<p>set(CMAKE_CXX_FLAGS_DEBUG &#8220;-D_DEBUG ${CMAKE_CXX_FLAGS_DEBUG}&#8221;)<br \/>\nif(WIN32)<br \/>\n\t#windows linker settings<br \/>\nelse(WIN32)<br \/>\n\t#linux linker settings<br \/>\n\tset(CMAKE_EXE_LINKER_FLAGS &#8220;-static-libgcc -static-libstdc++ ${CMAKE_EXE_LINKER_FLAGS}&#8221;)<br \/>\nendif(WIN32)<\/p>\n<p>if(CMAKE_COMPILER_IS_GNUCC)<br \/>\n\tset(CMAKE_C_FLAGS &#8220;${CMAKE_C_FLAGS} &#8211;std=c++0x -funsigned-char -frounding-math -fsignaling-nans -ffloat-store -mfpmath=sse -msse2 -fmessage-length=0 -Wall -Wextra -Wno-unknown-pragmas -Wno-unused-variable -Wno-unused-parameter -Wno-comment&#8221;)<br \/>\nendif(CMAKE_COMPILER_IS_GNUCC)<br \/>\nif(CMAKE_COMPILER_IS_GNUCXX)<br \/>\n\tset(CMAKE_CXX_FLAGS &#8220;${CMAKE_CXX_FLAGS} &#8211;std=c++0x -funsigned-char -frounding-math -fsignaling-nans -ffloat-store -mfpmath=sse -msse2 -fmessage-length=0 -Wall -Wextra -Wno-unknown-pragmas -Wno-unused-variable -Wno-unused-parameter -Wno-comment&#8221;)<br \/>\nendif(CMAKE_COMPILER_IS_GNUCXX)<\/p>\n<p>if(WIN32)<br \/>\n\t#windows include dirs<br \/>\nelse(WIN32)<br \/>\n\t#linux include dirs<br \/>\n\tinclude_directories(SYSTEM \/usr\/local\/include)<br \/>\nendif(WIN32)<\/p>\n<p>add_subdirectory(src)<br \/>\n[\/cc]<\/p>\n<p>The other at src<br \/>\n[cc lang=&#8221;cmake&#8221;]<br \/>\nset(PART_NAME ${BINARY_NAME})<\/p>\n<p>set(${PART_NAME}_SRC<br \/>\n\t#sources here<br \/>\n    sample.cpp<br \/>\n)<\/p>\n<p>include_directories(${PROJECT_SOURCE_DIR})<br \/>\nadd_executable(${PART_NAME} ${${PART_NAME}_SRC})<\/p>\n<p>if(WIN32)<br \/>\n\ttarget_link_libraries(${PART_NAME}<br \/>\n\t#\twindows libraries<br \/>\n\t)<br \/>\nelse(WIN32)<br \/>\n\ttarget_link_libraries(${PART_NAME}<br \/>\n\t#\tlinux libraries<br \/>\n\t)<br \/>\nendif(WIN32)<\/p>\n<p>install(TARGETS ${PART_NAME} RUNTIME DESTINATION usr\/bin)<\/p>\n<p>[\/cc]<\/p>\n<h2>The files<\/h2>\n<p><a href=\"http:\/\/code.sualdea.com\/wp-content\/uploads\/2014\/04\/cmake-sample-files-ls.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-37\" src=\"http:\/\/code.sualdea.com\/wp-content\/uploads\/2014\/04\/cmake-sample-files-ls.png\" alt=\"cmake-sample-files-ls\" width=\"192\" height=\"106\" \/><\/a><br \/>\n<a href=\"http:\/\/code.sualdea.com\/wp-content\/uploads\/2014\/04\/cmake-template.zip\">download here cmake-template.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Minimum cmake sample. It will create an executable for sample.cpp. Windows Download cmake-gui from http:\/\/www.cmake.org\/. Execute using path as follows Configure and Generate.Choose build chain (compiler, linker &#8230;). Remeber that Visual Studio 11 means Visual Studio 2012. It will create solution and project files in build directory. Open and use it. Linux cd to the &hellip; <a href=\"https:\/\/code.sualdea.com\/?p=15\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Cmake sample<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[7,8,6,5],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c","tag-cmake","tag-linux","tag-windows"],"_links":{"self":[{"href":"https:\/\/code.sualdea.com\/index.php?rest_route=\/wp\/v2\/posts\/15","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code.sualdea.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code.sualdea.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code.sualdea.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code.sualdea.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15"}],"version-history":[{"count":21,"href":"https:\/\/code.sualdea.com\/index.php?rest_route=\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":47,"href":"https:\/\/code.sualdea.com\/index.php?rest_route=\/wp\/v2\/posts\/15\/revisions\/47"}],"wp:attachment":[{"href":"https:\/\/code.sualdea.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code.sualdea.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code.sualdea.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}