{"id":448,"date":"2025-04-15T18:15:00","date_gmt":"2025-04-15T18:15:00","guid":{"rendered":"https:\/\/permutationcity.co.uk\/bp\/?p=448"},"modified":"2025-04-14T19:21:49","modified_gmt":"2025-04-14T19:21:49","slug":"vcpkg","status":"publish","type":"post","link":"https:\/\/permutationcity.co.uk\/bp\/2025\/04\/15\/vcpkg\/","title":{"rendered":"vcpkg"},"content":{"rendered":"\n<p>It&#8217;s best if you can avoid re-inventing the wheel.\nIf someone has already written the code you need then it can save a lot of time.\nAlthough you should probably make sure about\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Comparison_of_free_and_open-source_software_licenses\">licences<\/a>\nbefore incorporating it.\nThat could be copy-pasting something off a web page but,\nespecially for bigger bits of code,\nfar better as a library.\nHowever, as you get more libraries and they themselves need other libraries it can get complicated.\n<a href=\"https:\/\/vcpkg.io\/en\/\">vcpkg<\/a>\nlets you outsource a lot of that complexity.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-old-way\">The old way<\/h2>\n\n\n\n<p>Let&#8217;s say you want to use the C++\n<a href=\"https:\/\/github.com\/fmtlib\/fmt\">fmt library<\/a>.\nThere are a variety of ways to get the library.\nPerhaps installing it on a Linux system using\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/APT_(software)\">APT<\/a>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;shell&quot;,&quot;mime&quot;:&quot;text\/x-sh&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;Shell&quot;,&quot;language&quot;:&quot;Shell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;shell&quot;}\">apt install libfmt-dev<\/pre><\/div>\n\n\n\n<p>But installing on the system requires documentation or a setup script. It something that&#8217;s easy to miss when setting up a project. Maybe make use of the <a href=\"https:\/\/en.wikipedia.org\/wiki\/CMake\">CMake<\/a> build system to <a href=\"https:\/\/cmake.org\/cmake\/help\/latest\/module\/FetchContent.htm\">fetch<\/a> it for you:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;cmake&quot;,&quot;mime&quot;:&quot;text\/x-cmake&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;CMake&quot;,&quot;language&quot;:&quot;CMake&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cmake&quot;}\">include(FetchContent)\n\nFetchContent_Declare(\n    fmt\n    GIT_REPOSITORY https:\/\/github.com\/fmtlib\/fmt\n    GIT_TAG e69e5f977d458f2650bb346dadf2ad30c5320281) # 10.2.1\nFetchContent_MakeAvailable(fmt)<\/pre><\/div>\n\n\n\n<p>This is pretty good.\nYou just need need to run cmake and it will download and build the library.\nHowever it&#8217;s going to do that every time you run cmake which can be slow.\nThis is also a simple setup with one library and no dependencies.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"a-new-way\">A new way<\/h2>\n\n\n\n<p>What you could do instead is use vcpkg.\nIt&#8217;s most useful when you have a bunch of libraries as you will need to do some\n<a href=\"https:\/\/learn.microsoft.com\/en-gb\/vcpkg\/get_started\/get-started?pivots=shell-bash\">setup<\/a>.\nHowever after that you can create a <code>vcpkg.json<\/code> file:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;application\/json&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;JSON&quot;,&quot;language&quot;:&quot;JSON&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;json&quot;}\">{\n    &quot;dependencies&quot;: [\n        &quot;fmt&quot;\n    ]\n}<\/pre><\/div>\n\n\n\n<p>If you want more packages just add them to the list.\nThere&#8217;s an extensive\n<a href=\"https:\/\/vcpkg.io\/en\/packages?query=\">package directory<\/a>.\nThey will be downloaded and compiled before you do your build.\nAll the artefacts will be cached so it only has to happen when you change your build setup.<\/p>\n\n\n\n<p>If the package you want has it&#8217;s own dependencies you don&#8217;t have to worry about it.\nvcpkg will download everything that&#8217;s needed first.\nBy default it just gets the latest available version but you can\n<a href=\"https:\/\/learn.microsoft.com\/en-us\/vcpkg\/consume\/lock-package-versions\">specify the package version<\/a>.\nIf the library you want isn&#8217;t available or needs customisation you can even write an\n<a href=\"https:\/\/learn.microsoft.com\/en-us\/vcpkg\/concepts\/overlay-ports\">overlay port<\/a>\nto pull it in to the system yourself.<\/p>\n\n\n\n<p>It&#8217;s not perfect.\nEspecially if you are dealing with more advanced features like\n<a href=\"https:\/\/learn.microsoft.com\/en-us\/vcpkg\/users\/examples\/overlay-triplets-linux-dynamic\">overlay triplets<\/a>.\nThe caching mechanism can feel a bit mysterious.\nSometimes you change the build system and suddenly it &#8220;decides&#8221; to recompile.\nIt has all the downloaded source in it&#8217;s cache but\nit will have to recompile the source.\nUnfortunately I&#8217;ve used some big libraries and been hit by long compile times.\nThat&#8217;s not too bad as a one off but it kept happening.\nI&#8217;m going to say this is more a problem of understanding when it happens rather a bug in the system.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"in-the-end\">In the end<\/h2>\n\n\n\n<p>There are a lot of ways to consume libraries.\nIf you only need a few it probably doesn&#8217;t matter too much.\nHowever it&#8217;s one of those things that can gradually mount up.\nIt&#8217;s easy to find yourself using versions of libraries that are years old and\nit takes so long to update them all individually.\nThis puts everything in one place and gives a framework for handling it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s best if you can avoid re-inventing the wheel. If someone has already written the code you need then it can save a lot of time. Although you should probably make sure about licences before incorporating it. That could be copy-pasting something off a web page but, especially for bigger bits of code, far better [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[10,18],"class_list":["post-448","post","type-post","status-publish","format-standard","hentry","category-general","tag-builds","tag-tools"],"_links":{"self":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/comments?post=448"}],"version-history":[{"count":1,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/448\/revisions"}],"predecessor-version":[{"id":449,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/448\/revisions\/449"}],"wp:attachment":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/media?parent=448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/categories?post=448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/tags?post=448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}