{"id":2255,"date":"2019-10-01T09:10:57","date_gmt":"2019-10-01T06:10:57","guid":{"rendered":"http:\/\/demensdeum.com\/blog\/?p=2255"},"modified":"2024-12-16T22:32:34","modified_gmt":"2024-12-16T19:32:34","slug":"shared-library-cmake-c","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/hi\/2019\/10\/01\/shared-library-cmake-c\/","title":{"rendered":"Shared Library CMake C++"},"content":{"rendered":"<p>Recently I decided to make all parts of FlameSteelFramework separate shared libraries, below I will show an example of CMakeLists.txt file for <a href=\"https:\/\/gitlab.com\/demensdeum\/FlameSteelCore\" target=\"_blank\" rel=\"noopener noreferrer\">FlameSteelCore<\/a>:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>cmake_minimum_required(VERSION 3.5)\n\nproject (FlameSteelCore)\nset(CMAKE_BUILD_TYPE Release)\n\ninclude_directories(${CMAKE_CURRENT_SOURCE_DIR}\/src)\n\nfile(GLOB_RECURSE SOURCE_FILES\n    \"src\/FlameSteelCore\/*.cpp\"\n)\n\nadd_library(FlameSteelCore SHARED ${SOURCE_FILES})\n\ninstall(DIRECTORY \"${CMAKE_SOURCE_DIR}\/src\/FlameSteelCore\"\n        DESTINATION include\/FlameSteelFramework\n        FILES_MATCHING\n        PATTERN \"*.h\"\n)\n\ninstall(TARGETS FlameSteelCore DESTINATION lib)\n<\/code><\/pre>\n<\/div>\n<p>Commands that CMake executes: compiles all files with the *.cpp extension from the src\/FlameSteelCore\/ directory into a shared library, copies all headers with the *.h extension from src\/FlameSteelCore to include\/FlameSteelFramework (in my case it is \/usr\/local\/include\/FlameSteelFramework), copies shared lib to the lib directory (\/usr\/local\/lib)<br \/>After installation, you may need to update the LD cache &#8211; sudo ldconfig.<br \/>To build and install on Ubuntu (if you have the correct build toolchain), just run the commands:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>cmake . && make && sudo make install\n<\/code><\/pre>\n<\/div>\n<p>To test the installation process, I pass make prefix to the local folder makeInstallTestPlayground:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>cmake -DCMAKE_INSTALL_PREFIX:PATH=\/home\/demensdeum\/makeInstallTestPlayground . && make && make install\n<\/code><\/pre>\n<\/div>\n<h3>References<\/h3>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/17511496\/how-to-create-a-shared-library-with-cmake\" target=\"_blank\" rel=\"noopener noreferrer\">https: \/\/stackoverflow.com\/questions\/17511496\/how-to-create-a-shared-library-with-cmake<\/a><br \/><a href=\"https:\/\/stackoverflow.com\/questions\/6003374\/what-is-cmake-equivalent-of-configure-prefix-dir-make-all-install\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/stackoverflow.com\/questions\/6003374\/what-is-cmake-equivalent-of-configure-prefix-dir-make-all-install<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I decided to make all parts of FlameSteelFramework separate shared libraries, below I will show an example of CMakeLists.txt file for FlameSteelCore: cmake_minimum_required(VERSION 3.5) project (FlameSteelCore) set(CMAKE_BUILD_TYPE Release) include_directories(${CMAKE_CURRENT_SOURCE_DIR}\/src) file(GLOB_RECURSE SOURCE_FILES &#8220;src\/FlameSteelCore\/*.cpp&#8221; ) add_library(FlameSteelCore SHARED ${SOURCE_FILES}) install(DIRECTORY &#8220;${CMAKE_SOURCE_DIR}\/src\/FlameSteelCore&#8221; DESTINATION include\/FlameSteelFramework FILES_MATCHING PATTERN &#8220;*.h&#8221; ) install(TARGETS FlameSteelCore DESTINATION lib) Commands that CMake executes: compiles all<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/hi\/2019\/10\/01\/shared-library-cmake-c\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Shared Library CMake C++&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[61,52],"tags":[124,122,123],"class_list":["post-2255","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","tag-c","tag-cmake","tag-shared-lib","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"hi","enabled_languages":["en","ru","zh","de","fr","ja","pt","hi"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"ru":{"title":true,"content":true,"excerpt":false},"zh":{"title":true,"content":true,"excerpt":false},"de":{"title":true,"content":true,"excerpt":false},"fr":{"title":true,"content":true,"excerpt":false},"ja":{"title":true,"content":true,"excerpt":false},"pt":{"title":true,"content":true,"excerpt":false},"hi":{"title":false,"content":false,"excerpt":false}}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/2255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/comments?post=2255"}],"version-history":[{"count":12,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/2255\/revisions"}],"predecessor-version":[{"id":3940,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/2255\/revisions\/3940"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/media?parent=2255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/categories?post=2255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/tags?post=2255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}