{"id":2934,"date":"2017-07-13T10:22:43","date_gmt":"2017-07-13T17:22:43","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=2934"},"modified":"2017-08-10T16:33:24","modified_gmt":"2017-08-10T23:33:24","slug":"organizing-and-reusing-cc-library-projects-with-cmake","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/linux\/cmake\/library\/","title":{"rendered":"Organizing and Reusing C\/C++ Library Projects with CMake"},"content":{"rendered":"<p>This tutorial shows how to create library projects with CMake and reference the from other projects. We will create a basic shared library for Raspberry Pi using a cross-toolchain, make it export a few settings and import it from a different project.<\/p>\n<p>Before you begin, install VisualGDB 5.3 or later.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualGDB Linux Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/01-prjname2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2935\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/01-prjname2.png\" alt=\"01-prjname\" width=\"909\" height=\"619\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/01-prjname2.png 909w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/01-prjname2-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/01-prjname2-130x90.png 130w\" sizes=\"(max-width: 909px) 100vw, 909px\" \/><\/a><\/li>\n<li>Select &#8220;Create a new project -&gt; Application -&gt; Use CMake&#8221; and check the &#8220;Use the advanced CMake Project System&#8221; checkbox:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/02-cmakesys.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2936\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/02-cmakesys.png\" alt=\"02-cmakesys\" width=\"822\" height=\"662\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/02-cmakesys.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/02-cmakesys-300x242.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a>We will change the project type to a shared library once the project is created.<\/li>\n<li>On the next page select &#8220;Build the project locally with a cross-compiler&#8221; and pick the &#8220;Raspberry Pi&#8221; cross-toolchain. If it&#8217;s not installed, VisualGDB will download and install it automatically. Then select your Raspberry Pi in the &#8220;Development computer&#8221; field:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/03-target2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2937\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/03-target2.png\" alt=\"03-target\" width=\"822\" height=\"662\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/03-target2.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/03-target2-300x242.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to generate the project. Open the generated CMakeLists.txt file and find the add_executable() statement:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/04-prj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2938\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/04-prj.png\" alt=\"04-prj\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/04-prj.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/04-prj-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/04-prj-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Replace &#8220;add_executable(&#8230;)&#8221; with &#8220;add_library(&lt;name&gt; SHARED &lt;&#8230;&gt;)&#8221; and save the CMakeLists.txt file so that Solution Explorer picks up the update. Then right-click on the &#8220;Source files&#8221; folder and select &#8220;Add-&gt;New Item&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/05-newitem.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2939\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/05-newitem.png\" alt=\"05-newitem\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/05-newitem.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/05-newitem-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/05-newitem-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Add the following files to the library:\n<ol style=\"list-style-type: lower-alpha;\">\n<li>private\/PrivateHeader.h:\n<pre class=\"\">#pragma once\r\n#include &lt;stdio.h&gt;\r\n\r\nstatic void PrivateFunction()\r\n{\r\n\u00a0\u00a0\u00a0 printf(\"Hello from PrivateFunction()\\n\");\r\n}<\/pre>\n<\/li>\n<li>public\/PublicHeader.h:\n<pre class=\"\">#pragma once\r\n\r\nvoid PublicFunction();<\/pre>\n<\/li>\n<li>Replace the main source file contents with this:\n<pre class=\"\">#include \"PrivateHeader.h\"\r\n#include \"PublicHeader.h\"\r\n\r\n#ifdef USING_CMAKE_LIBRARY_DEMO\r\n#error This should not happen\r\n#endif\r\n\r\nvoid PublicFunction()\r\n{\r\n\u00a0\u00a0\u00a0 printf(\"Hello from PublicFunction()\\n\");\r\n\u00a0\u00a0\u00a0 PrivateFunction();\r\n}<\/pre>\n<p>This illustrates the concept of public and private header files. Private headers are headers only visible to the library itself. Public headers are headers visible to any program that uses the library and typically contain declarations for exported library functions.<\/li>\n<\/ol>\n<\/li>\n<li>As we have not set any include directories, the PrivateHeader.h and PublicHeader.h files will appear as missing:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/06-includes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2940\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/06-includes.png\" alt=\"06-includes\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/06-includes.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/06-includes-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/06-includes-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Open Visual Studio properties for the library and add &#8220;private&#8221; to the Additional Include Directories field:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/07-private.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2941\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/07-private.png\" alt=\"07-private\" width=\"822\" height=\"542\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/07-private.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/07-private-300x198.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>Then add &#8220;public&#8221; to the Exported Settings -&gt; Public -&gt; Additional Include Directories:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/08-public.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2942\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/08-public.png\" alt=\"08-public\" width=\"822\" height=\"542\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/08-public.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/08-public-300x198.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>Finally add &#8220;USING_CMAKE_LIBRARY_DEMO&#8221; to the Exported Settings-&gt;Interface-&gt;Preprocessor Definitions:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/09-iface-macro.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2943\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/09-iface-macro.png\" alt=\"09-iface-macro\" width=\"822\" height=\"542\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/09-iface-macro.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/09-iface-macro-300x198.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a>The difference between Public and Interface settings is that <strong>Public<\/strong> settings are applied to both the library and the applications using it, while the <strong>Interface<\/strong> settings are only applied to the library users. I.e. the USING_CMAKE_LIBRARY_DEMO macro will not be defined when compiling the library itself.<\/li>\n<li>Apply the settings and ensure that CMakeLists.txt got edited accordingly:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/10-cmakelists.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2944\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/10-cmakelists.png\" alt=\"10-cmakelists\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/10-cmakelists.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/10-cmakelists-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/10-cmakelists-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>The main source file should now compile without problems:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/11-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2945\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/11-build.png\" alt=\"11-build\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/11-build.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/11-build-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/11-build-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Now we show how to use the library from other projects. First we will add a small demo application to the same CMake project. Then we will show how to import the library from a different project. Right-click in the .vgdbcmake project item and select Add-&gt;New Item. Then pick &#8220;Executable&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/12-client.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2946\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/12-client.png\" alt=\"12-client\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/12-client.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/12-client-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/12-client-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Replace the main file contents for the newly created executable with this:\n<pre class=\"\">#include \"PublicHeader.h\"\r\n\r\n#ifndef USING_CMAKE_LIBRARY_DEMO\r\n#error CMakeLibraryDemo is missing\r\n#endif \r\n\r\nint main()\r\n{\r\n\u00a0\u00a0\u00a0 PublicFunction();\r\n\u00a0\u00a0\u00a0 return 0;\r\n}<\/pre>\n<\/li>\n<li>Note that initially the PublicHeader.h won&#8217;t be found: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/13-clientsrc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2947\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/13-clientsrc.png\" alt=\"13-clientsrc\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/13-clientsrc.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/13-clientsrc-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/13-clientsrc-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Right-click on the References node in Solution Explorer and add a reference from the test application to CMakeLibraryDemo:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/14-addref.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2948\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/14-addref.png\" alt=\"14-addref\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/14-addref.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/14-addref-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/14-addref-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Now the test application should build properly. Try debugging it by pressing F5:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/15-run.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2949\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/15-run.png\" alt=\"15-run\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/15-run.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/15-run-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/15-run-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Note how both DemoLibraryClient and libCMakeLibraryDemo.so got deployed to \/tmp on your Raspberry Pi. You can change the deployment directory for the entire project via Visual Studio Project Properties for the .vgdbcmake project in Solution Explorer. Try changing it to \/tmp\/MyLibraryDemo:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/16-deploy.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2950\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/16-deploy.png\" alt=\"16-deploy\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/16-deploy.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/16-deploy-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/16-deploy-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Now both the library and the application will be deployed to the new directory:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/17-newpath.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2951\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/17-newpath.png\" alt=\"17-newpath\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/17-newpath.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/17-newpath-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/17-newpath-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Now we will show how to reference the library from a completely different project. Start the VisualGDB Linux Project Wizard again and create another CMake-based application in a different directory:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/anotherclient.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2971\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/anotherclient.png\" alt=\"anotherclient\" width=\"909\" height=\"619\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/anotherclient.png 909w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/anotherclient-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/anotherclient-130x90.png 130w\" sizes=\"(max-width: 909px) 100vw, 909px\" \/><\/a><\/li>\n<li>Replace the main source file contents with this:\n<pre class=\"\">#include &lt;stdio.h&gt;\r\n\r\n#ifdef USING_CMAKE_LIBRARY_DEMO\r\n#include \"PublicHeader.h\"\r\n#endif \r\n\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n#ifdef USING_CMAKE_LIBRARY_DEMO\r\n\u00a0\u00a0\u00a0 PublicFunction();\r\n#else\r\n\u00a0\u00a0\u00a0 printf(\"Compiled without CMakeLibraryDemo\\n\");\r\n#endif \r\n}<\/pre>\n<\/li>\n<li>As we have not referenced the library yet, the part printing &#8220;Compiled without CMakeLibraryDemo&#8221; will be active:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/19-clientsrc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2953\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/19-clientsrc.png\" alt=\"19-clientsrc\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/19-clientsrc.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/19-clientsrc-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/19-clientsrc-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Before we can reference the library from this project, we need to add a reference to the corresponding CMakeLists.txt file. Right-click on the .vgdbcmake project in Solution Explorer and select Add-&gt;Reference Another CMake Folder:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/20-addref.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2954\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/20-addref.png\" alt=\"20-addref\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/20-addref.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/20-addref-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/20-addref-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Point to the CMakeLists.txt file belonging to the library project:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/21-pickfile.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2955\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/21-pickfile.png\" alt=\"21-pickfile\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/21-pickfile.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/21-pickfile-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/21-pickfile-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>This will insert an &#8220;add_subdirectory&#8221; statement to the new project&#8217;s CMakeLists.txt file referencing the library project&#8217;s directory:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/22-sln.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2956\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/22-sln.png\" alt=\"22-sln\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/22-sln.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/22-sln-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/22-sln-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Save the CMakeLists.txt file to update Solution Explorer. Now you can add a reference to CMakeLibraryDemo using the normal <strong>Add Reference<\/strong> command:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/23-addref.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2957\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/23-addref.png\" alt=\"23-addref\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/23-addref.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/23-addref-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/23-addref-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>Build the project and see how <strong>PublicFunction()<\/strong> is now invoked:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/24-deployed.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2958\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/24-deployed.png\" alt=\"24-deployed\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/24-deployed.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/24-deployed-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/24-deployed-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a><\/li>\n<li>The &#8220;add reference&#8221; command translated to adding CMakeLibraryDemo to the target_link_libraries() statement in CMakeLists.txt:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/25-link.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2959\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/25-link.png\" alt=\"25-link\" width=\"1154\" height=\"783\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/25-link.png 1154w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/25-link-300x204.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2017\/06\/25-link-1024x695.png 1024w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/a>Once we did that, CMake automatically picked up the USING_CMAKE_LIBRARY_DEMO macro and the public include directory from the exported settings in the library project, so we did not need to specify them by hand.<\/li>\n<\/ol>\n<p>Note that each time you build the project, CMake will check the dependencies of the CMakeLibraryDemo project and build it if needed. If your library project is too complicated and you want to avoid rechecking\/building it each time, consider creating a CMake package. See <a href=\"https:\/\/visualgdb.com\/tutorials\/linux\/cmake\/find_package\/\">this tutorial<\/a> for a detailed example on using packages.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to create library projects with CMake and reference the from other projects. We will create a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[77,79,33,85],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2934"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=2934"}],"version-history":[{"count":3,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2934\/revisions"}],"predecessor-version":[{"id":3024,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2934\/revisions\/3024"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=2934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=2934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=2934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}