- 1. General remarks
-
2. Detailed discussion
- 2.1 Creating the project
- 2.2 Loading works
- 2.3 Configuration files
- 2.4 Engineering configuration
- 2.5 Commissioning Execution
- 3. Project cases
- 4. Summary
1. General remarks
In the previous series of blog posts, we learned how to build third-party dependency libraries, and how to go about putting together your own CMake project, and in particular how to write CMake's core configuration file. For a long time, this file has been written additionally for C/C++ projects, and then configured using CMake commands or GUI tools into an MSVC project for Windows, or a Makefile file for Linux. Although this is a significant improvement over the previous need to use different projects for different platforms, it is possible to go one step further, that is, to use the CMake project directly in the IDE for development, which undoubtedly has a qualitative improvement in the efficiency of C/C++ program development.
Starting with Visual Studio 2017, Microsoft Visual Studio (VS for short) supports the import of CMake projects. The so-called CMake project means that it is no longer necessary to create traditional MSVC projects, such as .sln or .vcxproj project files, but directly use as a project configuration file to load, to carry out the work of building and development. Not only VS, other IDEs such as Visual Studio Code, Qt Creator, IntelliJ IDEA, CLion can directly support the import of Make projects. However, as a beginner, I still recommend starting with Microsoft Visual Studio for CMake project development, after all, the so-called universe of the first IDE is not called for nothing. In my opinion, Microsoft Visual Studio is indeed a little heavy, the editor is not the most beautiful, UI operation is not necessarily the most user-friendly, but the debugging features provided by the best and indispensable, especially suitable for business productivity environment. Here I will take Visual Studio 2019 as an example to explain in detail how to carry out CMake project development to improve our C/C++ program development efficiency.
2. Detailed discussion
2.1 Creating the project
Launch Visual Studio 2019 and the startup page pops up, as shown in Figure 1 below:
Click the "Create New Project" button at the bottom right to enter the "Create New Project" page, as shown in Figure 2 below:
Select the "CMake Project" template, if you don't see it you can search for a template. Click the "Next" button.
Go to the Configure New Item page, as shown in Figure 3 below:
Fill in the project name and location in the "Configure New Project" page and click the "Create" button to enter the main working page of Visual Studio 2019, as shown in Figure 4 below:
2.2 Loading works
Close Visual Studio 2019 and simulate loading an existing CMake project directly. If you start Visual Studio 2019 again, you can see the history of the last loaded project in the startup page shown in Figure 1, and click on it to load it again. However, if you don't have any history, click the "Continue without code" button to go directly to the main page. In the menu bar, select File - & gt; Open - & gt; CMake button, as shown in Figure 5:
At this point, the "Open CMake Project" dialog box will pop up, select the project file, CMake project is opened through this core configuration file, as shown in Figure 6:
Remember that you must open the file this way to open the CMake project, if you drag the file directly into the Visual Studio 2019 main page it will only be displayed in text form.
2.3 Configuration files
Next, we need to take care of a configuration file before we can proceed with development. Having a very large number of configuration items, or external parameters that need to be passed in, needs to be managed using a configuration file. Here's the trouble though, it was introduced with CMake 3.20 and is a relatively new feature, Visual Studio 2019 doesn't interface with this configuration file right off the bat, instead using a file of its own design as the configuration for CMake build projects. Currently, both configuration files are supported by Visual Studio 2019, but it is more recommended because it is more standardized, conforms to CMake's specifications, and can be recognized and supported by a wide range of IDEs and build tools.
Specifically, if the main page of the program, and especially the toolbar on the main page, is different from Figure 7 below:
Then you can select Tools - & gt; Options - & gt; CMake - & gt; General in the menu bar, check the "First time to use CMake presets for configuration, build and test" radio box, as shown in Figure 8:
Click on the Configuration drop-down menu in the toolbar and select the Manage Configuration button, as shown in Figure 9 below:
At this point Visual Studio 2019 automatically creates the profile, as shown in Figure 10 below:
From this file we can see that the default windows-default configuration is actually Debug mode, we can add a RelWithDebInfo mode to it, that is, Release with DebugInfo mode, the specific content is:
{
"version": 2,
"configurePresets": [
{
"name": "linux-default",
"displayName": "Linux Debug",
"description": "Aimed at Linux (used form a nominal expression) Windows subsystems(WSL)or remote Linux systems。",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}"
},
"vendor": {
"/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Linux" ] },
"/VisualStudioRemoteSettings/CMake/1.0": { "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}" }
}
},
{
"name": "windows-default",
"displayName": "Windows x64 Debug",
"description": "geared towards Visual Studio 开发环境(used form a nominal expression) Windows。",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}"
},
"vendor": { "/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Windows" ] } }
},
{
"name": "RelWithDebInfo",
"displayName": "Windows x64 RelWithDebInfo Shared Library",
"description": "geared towards Visual Studio 开发环境(used form a nominal expression) Windows。",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}"
},
"vendor": { "/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Windows" ] } }
}
]
}
After you add this configuration and Ctrl+S to save it, the toolbar's Configuration drop-down menu will have the RelWithDebInfo option selected, as shown in Figure 11 below:
Note that there are times when the conflict issues between the two configuration methods can lead to some anomalies, such as the toolbar configuration menus not being quite the same. If you encounter this situation you can launch Visual Studio 2019, clean up the project's intermediate generated files, and try reloading the project again.
2.4 Engineering configuration
Then the next step do not rush to write the source code file, to complete the first write. New project will have a default content, as follows:
# : A CMake project for ZipTest, where the source code is included and project-specific logic is defined.
# Project-specific logic.
#
cmake_minimum_required (VERSION 3.8)
project ("ZipTest")
# Add the source code to the executable of this project.
add_executable (ZipTest "" "")
# TODO: Add tests and install targets if needed.
We will understand this configuration code file if we have studied the previous blog post and we recommend the reader to review it. The key thing to say here is that after modifying the file, you need to Ctrl+S to save it, Visual Studio 2019 will automatically do the project configuration and you can see some output information in the output window:
1> already configured“RelWithDebInfo”activate (a plan) CMake generating。
1> Environmental settings:
1> CommandPromptType=Native
1> DevEnvDir=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\
1> ExtensionSdkDir=C:\Program Files (x86)\Microsoft SDKs\Windows Kits\10\ExtensionSDKs
1> Framework40Version=v4.0
1> FrameworkDir=C:\windows\\Framework64\
1> FrameworkDIR64=C:\windows\\Framework64
1> FrameworkVersion=v4.0.30319
1> FrameworkVersion64=v4.0.30319
1> HTMLHelpDir=C:\Program Files (x86)\HTML Help Workshop
1> IFCPATH=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\ifc\x64
1> IGCCSVC_DB=AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAggkwNDDGDkq0HFOUjEsXQAQAAAACAAAAAAAQZgAAAAEAACAAAADxjk35GiqLjZDHeYjx5dq8wxmbU7aEbBW9J68TO/bzIwAAAAAOgAAAAAIAACAAAADdM3gyHGrxXOwEEyHmxfe9ocZnP6CM0OTQGZYVZKgQWmAAAAC8xGVDuFoU062/gozauvaMPUmsT8FAuEXoLnI9lTwHVT6XWpjF7lVBoYB+vxo1dgIUAtW0nl1wZSUg9KRxmYpIicPPLm7B+twKXEdbaDMIu55E10uazKjjvoHY/4KYu+tAAAAAoK95FWIYAE3f+YLjfb3S77+ZMJXFw69cRlxyTYekzkyfOFdUcCY94ahV+XHEgao2y8e/zT+q2zHU0SqXho0LDQ==
1> INCLUDE=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\ATLMFC\include;C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\include;C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um;C:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\ucrt;C:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\shared;C:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\um;C:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\winrt;C:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\cppwinrt
1> LIB=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\ATLMFC\lib\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\lib\x64;C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\lib\um\x64;C:\Program Files (x86)\Windows Kits\10\lib\10.0.22000.0\ucrt\x64;C:\Program Files (x86)\Windows Kits\10\lib\10.0.22000.0\um\x64
1> LIBPATH=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\ATLMFC\lib\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\lib\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\lib\x86\store\references;C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.22000.0;C:\Program Files (x86)\Windows Kits\10\References\10.0.22000.0;C:\windows\\Framework64\v4.0.30319
1> NETFXSDKDir=C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\
1> Path=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\\Extensions\Microsoft\IntelliCode\CLI;C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\bin\HostX64\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\VC\VCPackages;C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow;C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer;C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\bin\Roslyn;C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Team Tools\Performance Tools\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Team Tools\Performance Tools;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\vs2019\\x64;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\vs2019\;C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\x64\;C:\Program Files (x86)\HTML Help Workshop;C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\devinit;C:\Program Files (x86)\Windows Kits\10\bin\10.0.22000.0\x64;C:\Program Files (x86)\Windows Kits\10\bin\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\\MSBuild\Current\Bin;C:\windows\\Framework64\v4.0.30319;C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\Program Files\HP\OMEN-Broadcast\Common;C:\Program Files\CMake\bin;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\TortoiseGit\bin;C:\Work\3rdparty\bin;C:\SoftWare\Qt\Qt5.12.5\5.12.5\msvc2017_64\bin;C:\File\MyGitHub\GISBasic\3rdParty\bin;C:\Program Files\dotnet\;C:\Work\eGova3rdParty\protobuf\bin;C:\Program Files\Java\jdk1.8.0_271\bin;C:\Program Files\KTX-Software\bin;C:\SoftWare\sonar-scanner-msbuild;C:\Program Files\Cppcheck;C:\SoftWare\sonar-scanner-msbuild\sonar-scanner-4.7.0.2747\bin;C:\SoftWare\apache-ant-1.10.8\bin;C:\Program Files (x86)\pcsuite\;C:\SoftWare\apache-maven-3.6.2\bin;C:\Program Files\7-Zip;C:\Program Files\Git\cmd;C:\SoftWare\nvm;C:\Program Files\nodejs;C:\SoftWare\Python\Python311\Scripts\;C:\SoftWare\Python\Python311\;C:\Users\Charlee\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Microsoft VS Code\bin;C:\Users\Charlee\AppData\Local\GitHubDesktop\bin;C:\Users\Charlee\AppData\Roaming\npm;C:\SoftWare\nvm;C:\Program Files\nodejs;C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin;C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja;C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\VC\Linux\bin\ConnectionManagerExe
1> PROMPT=$P$G
1> UCRTVersion=10.0.22000.0
1> UniversalCRTSdkDir=C:\Program Files (x86)\Windows Kits\10\
1> VCIDEInstallDir=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\VC\
1> VCINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\
1> VCToolsInstallDir=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.29.30133\
1> VCToolsRedistDir=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Redist\MSVC\14.29.30133\
1> VCToolsVersion=14.29.30133
1> VS160COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\
1> VSCMD_ARG_app_plat=Desktop
1> VSCMD_ARG_HOST_ARCH=x64
1> VSCMD_ARG_no_logo=1
1> VSCMD_ARG_TGT_ARCH=x64
1> VSCMD_DEBUG=5
1> VSCMD_VER=16.11.29
1> VSINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\
1> WindowsLibPath=C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.22000.0;C:\Program Files (x86)\Windows Kits\10\References\10.0.22000.0
1> WindowsSdkBinPath=C:\Program Files (x86)\Windows Kits\10\bin\
1> WindowsSdkDir=C:\Program Files (x86)\Windows Kits\10\
1> WindowsSDKLibVersion=10.0.22000.0\
1> WindowsSdkVerBinPath=C:\Program Files (x86)\Windows Kits\10\bin\10.0.22000.0\
1> WindowsSDKVersion=10.0.22000.0\
1> WindowsSDK_ExecutablePath_x64=C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\x64\
1> WindowsSDK_ExecutablePath_x86=C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\
1> __devinit_path=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\devinit\
1> __DOTNET_ADD_64BIT=1
1> __DOTNET_PREFERRED_BITNESS=64
1> __VSCMD_PREINIT_PATH=C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\Program Files\HP\OMEN-Broadcast\Common;C:\Program Files\CMake\bin;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\TortoiseGit\bin;C:\Work\3rdparty\bin;C:\SoftWare\Qt\Qt5.12.5\5.12.5\msvc2017_64\bin;C:\File\MyGitHub\GISBasic\3rdParty\bin;C:\Program Files\dotnet\;C:\Work\eGova3rdParty\protobuf\bin;C:\Program Files\Java\jdk1.8.0_271\bin;C:\Program Files\KTX-Software\bin;C:\SoftWare\sonar-scanner-msbuild;C:\Program Files\Cppcheck;C:\SoftWare\sonar-scanner-msbuild\sonar-scanner-4.7.0.2747\bin;C:\SoftWare\apache-ant-1.10.8\bin;C:\Program Files (x86)\pcsuite\;C:\SoftWare\apache-maven-3.6.2\bin;C:\Program Files\7-Zip;C:\Program Files\Git\cmd;C:\SoftWare\nvm;C:\Program Files\nodejs;C:\SoftWare\Python\Python311\Scripts\;C:\SoftWare\Python\Python311\;C:\Users\Charlee\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Microsoft VS Code\bin;C:\Users\Charlee\AppData\Local\GitHubDesktop\bin;C:\Users\Charlee\AppData\Roaming\npm;C:\SoftWare\nvm;C:\Program Files\nodejs
1> __VSCMD_script_err_count=0
1> HOMEPATH=\Users\Charlee
1> DriverData=C:\Windows\System32\Drivers\DriverData
1> COMPUTERNAME=LAPTOP-K38HMG48
1> CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
1> POSTGIS_GDAL_ENABLED_DRIVERS=ENABLE_ALL
1> ProgramW6432=C:\Program Files
1> OneDrive=C:\Users\Charlee\OneDrive
1> __PSLockDownPolicy=0
1> RegionCode=APJ
1> DEVECOSTUDIO_VM_OPTIONS=C:\SoftWare\ideaI-windows\2023\vmoptions\
1> VisualStudioEdition=Microsoft Visual Studio Enterprise 2019
1> WEBIDE_VM_OPTIONS=C:\SoftWare\ideaI-windows\2023\vmoptions\
1> ServiceHubLogSessionKey=3AFDCD75
1> PROCESSOR_REVISION=b701
1> PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 183 Stepping 1, GenuineIntel
1> PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
1> PkgDefApplicationConfigFile=C:\Users\Charlee\AppData\Local\Microsoft\VisualStudio\16.0_36d14652\
1> JETBRAINS_CLIENT_VM_OPTIONS=C:\SoftWare\ideaI-windows\2023\vmoptions\jetbrains_client.vmoptions
1> PROJ_LIB=C:\Program Files\PostgreSQL\16\share\contrib\postgis-3.4\proj
1> CURL_CA_BUNDLE=C:\Program Files\PostgreSQL\16\ssl\certs\
1> TMP=C:\Users\Charlee\AppData\Local\Temp
1> DATAGRIP_VM_OPTIONS=C:\SoftWare\ideaI-windows\2023\vmoptions\
1> TEMP=C:\Users\Charlee\AppData\Local\Temp
1> LOCALAPPDATA=C:\Users\Charlee\AppData\Local
1> PUBLIC=C:\Users\Public
1> eGova3rdParty=C:\Work\3rdparty
1> GDAL_DATA=C:\Program Files\PostgreSQL\16\gdal-data
1> PSModulePath=C:\Program Files\WindowsPowerShell\Modules;C:\windows\system32\WindowsPowerShell\v1.0\Modules
1> ProgramData=C:\ProgramData
1> JAVA_HOME=C:\Program Files\Java\jdk1.8.0_271
1> USERDOMAIN=LAPTOP-K38HMG48
1> platformcode=M7
1> PROCESSOR_LEVEL=6
1> NUMBER_OF_PROCESSORS=32
1> PHPSTORM_VM_OPTIONS=C:\SoftWare\ideaI-windows\2023\vmoptions\
1> STUDIO_VM_OPTIONS=C:\SoftWare\ideaI-windows\2023\vmoptions\
1> VSAPPIDDIR=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\
1> ProgramFiles(x86)=C:\Program Files (x86)
1> FPS_BROWSER_USER_PROFILE_STRING=Default
1> CommonProgramFiles=C:\Program Files (x86)\Common Files
1> VS140COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\
1> USERDOMAIN_ROAMINGPROFILE=LAPTOP-K38HMG48
1> VisualStudioDir=C:\Users\Charlee\Documents\Visual Studio 2019
1> IGCCSVC_DB=AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAggkwNDDGDkq0HFOUjEsXQAQAAAACAAAAAAAQZgAAAAEAACAAAADxjk35GiqLjZDHeYjx5dq8wxmbU7aEbBW9J68TO/bzIwAAAAAOgAAAAAIAACAAAADdM3gyHGrxXOwEEyHmxfe9ocZnP6CM0OTQGZYVZKgQWmAAAAC8xGVDuFoU062/gozauvaMPUmsT8FAuEXoLnI9lTwHVT6XWpjF7lVBoYB+vxo1dgIUAtW0nl1wZSUg9KRxmYpIicPPLm7B+twKXEdbaDMIu55E10uazKjjvoHY/4KYu+tAAAAAoK95FWIYAE3f+YLjfb3S77+ZMJXFw69cRlxyTYekzkyfOFdUcCY94ahV+XHEgao2y8e/zT+q2zHU0SqXho0LDQ==
1> GISBasic=C:\File\MyGitHub\GISBasic\3rdParty
1> VSLS_SESSION_KEEPALIVE_INTERVAL=0
1> MAVEN_HOME=C:\SoftWare\apache-maven-3.6.2
1> ProgramFiles=C:\Program Files (x86)
1> RUBYMINE_VM_OPTIONS=C:\SoftWare\ideaI-windows\2023\vmoptions\
1> APPCODE_VM_OPTIONS=C:\SoftWare\ideaI-windows\2023\vmoptions\
1> FPS_BROWSER_APP_PROFILE_STRING=Internet Explorer
1> VSSKUEDITION=Enterprise
1> OnlineServices=Online Services
1> ThreadedWaitDialogDpiContext=-4
1> IDEA_VM_OPTIONS=C:\SoftWare\ideaI-windows\2023\vmoptions\
1> WEBSTORM_VM_OPTIONS=C:\SoftWare\ideaI-windows\2023\vmoptions\
1> GOLAND_VM_OPTIONS=C:\SoftWare\ideaI-windows\2023\vmoptions\
1> NVM_SYMLINK=C:\Program Files\nodejs
1> NVM_HOME=C:\SoftWare\nvm
1> SESSIONNAME=Console
1> VisualStudioVersion=16.0
1> SystemRoot=C:\windows
1> CommonProgramW6432=C:\Program Files\Common Files
1> ZES_ENABLE_SYSMAN=1
1> LOGONSERVER=\\LAPTOP-K38HMG48
1> VSAPPIDNAME=
1> USERPROFILE=C:\Users\Charlee
1> MSBuildLoadMicrosoftTargetsReadOnly=true
1> QtMsBuild=C:\Users\Charlee\AppData\Local\QtMsBuild
1> POSTGIS_ENABLE_OUTDB_RASTERS=1
1> VSLANG=2052
1> RIDER_VM_OPTIONS=C:\SoftWare\ideaI-windows\2023\vmoptions\
1> APPDATA=C:\Users\Charlee\AppData\Roaming
1> HOMEDRIVE=C:
1> DATASPELL_VM_OPTIONS=C:\SoftWare\ideaI-windows\2023\vmoptions\
1> GATEWAY_VM_OPTIONS=C:\SoftWare\ideaI-windows\2023\vmoptions\
1> CLION_VM_OPTIONS=C:\SoftWare\ideaI-windows\2023\vmoptions\
1> JETBRAINSCLIENT_VM_OPTIONS=C:\SoftWare\ideaI-windows\2023\vmoptions\
1> USERNAME=Charlee
1> PROCESSOR_ARCHITEW6432=AMD64
1> EFC_20336=1
1> PROCESSOR_ARCHITECTURE=x86
1> OS=Windows_NT
1> ComSpec=C:\windows\system32\
1> PYCHARM_VM_OPTIONS=C:\SoftWare\ideaI-windows\2023\vmoptions\
1> SystemDrive=C:
1> windir=C:\windows
1> ALLUSERSPROFILE=C:\ProgramData
1> command line (computing): "C:\windows\system32\" /c "%SYSTEMROOT%\System32\ 65001 >NUL && "C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\ENTERPRISE\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\" -G "Ninja" -DCMAKE_BUILD_TYPE:STRING="RelWithDebInfo" -DCMAKE_INSTALL_PREFIX:STRING="C:/Work/ZipTest/out/install/RelWithDebInfo" -DCMAKE_MAKE_PROGRAM="C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\ENTERPRISE\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\" "C:\Work\ZipTest" 2>&1"
1> Job Catalog: C:/Work/ZipTest/out/build/RelWithDebInfo
1> [CMake] -- Configuring done
1> [CMake] -- Generating done
1> [CMake] -- Build files have been written to: C:/Work/ZipTest/out/build/RelWithDebInfo
1> withdrawn CMake variant。
1> withdrawn源文件和标头。
1> withdrawn代码模型。
1> withdrawn工具链配置。
1> withdrawn包含路径。
1> CMake generating完毕。
This step of configuring the project must be done without fail, and make sure you see the "CMake generation complete" message. If it breaks, you can see what went wrong in the output log and fix it. Essentially, it is just a text file. This step prepares the environment for the build, generates some cache files and intermediate files, and makes it easier for the build toolchain to recognize the next step in the process.
2.5 Commissioning Execution
After ensuring that the CMake configuration project is complete, it is time to run the debugging. The functions in this step will work seamlessly with the MSVC project, such as Edit Code, F7 Generate, F5 Debug, Ctrl+F5 Execute, F9 Breakpoints, F10 Procedure-by-Procedure Debugging and F11 Statement-by-Statement Debugging. However, there is one thing to note, that is, to select the startup item, otherwise you may not be able to run the project. Specifically, you can select the startup item in the toolbar drop-down menu, as shown in Figure 12 below:
We should of course check this target, but be sure to note that this option will only appear once the CMake generation is complete. If this option is not there, then the previous CMake generation was not successful.
Another useful feature is the ability to switch to the CMake target project after successful CMake generation. This is accessed through the "Switch between solution and available views" button on the toolbar of the "Solution Explorer View", as shown in Figure 13 below:
This view looks a bit like an MSVC project, and is much more compact than the folder view. More importantly, the right-click menu of this view is a bit more useful, for example, the "Set as Startup Item" button can also be used to realize the above function of selecting a startup item. In addition, there is the "Add" function, similar to the "Add" function of MSVC project, you can create a new source code file and add it to the CMake project. However, this function is realized by modifying the file, readers can try it themselves.
In fact, I feel that the CMake target view is to imagine the same MSVC project, integrating more commonly used GUI functions, making the development of programming more efficient. However, these are still only half-finished products, such as the "Add" function can be realized by adding the source code file, but the corresponding changes are not necessarily the content we want, about which the reader can try for a period of time before understanding. At present, many commonly used IDE features still need to edit their own files to achieve, but nevertheless, it can already help us to improve the efficiency of a large part of the development.
3. Project cases
The default Hello CMake case is still too simple, let's use the previous case of calling libzip to compress files and folders. The project directory is as follows:
ZipTest
│
│
|
of the following:
#include <>
#include <filesystem>
#include <fstream>
#include <iostream>
using namespace std;
void CompressFile2Zip(std::filesystem::path unZipFilePath,
const char* relativeName, zip_t* zipArchive) {
std::ifstream file(unZipFilePath, std::ios::binary);
(0, std::ios::end);
size_t bufferSize = ();
char* bufferData = (char*)malloc(bufferSize);
(0, std::ios::beg);
(bufferData, bufferSize);
//The fourth parameter if non0,Automatically hosts the requested resources,tillzip_closebefore automatically destroying。
zip_source_t* source =
zip_source_buffer(zipArchive, bufferData, bufferSize, 1);
if (source) {
if (zip_file_add(zipArchive, relativeName, source, ZIP_FL_OVERWRITE) < 0) {
std::cerr << "Failed to add file " << unZipFilePath
<< " to zip: " << zip_strerror(zipArchive) << std::endl;
zip_source_free(source);
}
} else {
std::cerr << "Failed to create zip source for " << unZipFilePath << ": "
<< zip_strerror(zipArchive) << std::endl;
}
}
void CompressFile(std::filesystem::path unZipFilePath,
std::filesystem::path zipFilePath) {
int errorCode = 0;
zip_t* zipArchive = zip_open(zipFilePath.generic_u8string().c_str(),
ZIP_CREATE | ZIP_TRUNCATE, &errorCode);
if (zipArchive) {
CompressFile2Zip(unZipFilePath, ().string().c_str(),
zipArchive);
errorCode = zip_close(zipArchive);
if (errorCode != 0) {
zip_error_t zipError;
zip_error_init_with_code(&zipError, errorCode);
std::cerr << zip_error_strerror(&zipError) << std::endl;
zip_error_fini(&zipError);
}
} else {
zip_error_t zipError;
zip_error_init_with_code(&zipError, errorCode);
std::cerr << "Failed to open output file " << zipFilePath << ": "
<< zip_error_strerror(&zipError) << std::endl;
zip_error_fini(&zipError);
}
}
void CompressDirectory2Zip(std::filesystem::path rootDirectoryPath,
std::filesystem::path directoryPath,
zip_t* zipArchive) {
if (rootDirectoryPath != directoryPath) {
if (zip_dir_add(zipArchive,
std::filesystem::relative(directoryPath, rootDirectoryPath)
.generic_u8string()
.c_str(),
ZIP_FL_ENC_UTF_8) < 0) {
std::cerr << "Failed to add directory " << directoryPath
<< " to zip: " << zip_strerror(zipArchive) << std::endl;
}
}
for (const auto& entry : std::filesystem::directory_iterator(directoryPath)) {
if (entry.is_regular_file()) {
CompressFile2Zip(
().generic_u8string(),
std::filesystem::relative((), rootDirectoryPath)
.generic_u8string()
.c_str(),
zipArchive);
} else if (entry.is_directory()) {
CompressDirectory2Zip(rootDirectoryPath, ().generic_u8string(),
zipArchive);
}
}
}
void CompressDirectory(std::filesystem::path directoryPath,
std::filesystem::path zipFilePath) {
int errorCode = 0;
zip_t* zipArchive = zip_open(zipFilePath.generic_u8string().c_str(),
ZIP_CREATE | ZIP_TRUNCATE, &errorCode);
if (zipArchive) {
CompressDirectory2Zip(directoryPath, directoryPath, zipArchive);
errorCode = zip_close(zipArchive);
if (errorCode != 0) {
zip_error_t zipError;
zip_error_init_with_code(&zipError, errorCode);
std::cerr << zip_error_strerror(&zipError) << std::endl;
zip_error_fini(&zipError);
}
} else {
zip_error_t zipError;
zip_error_init_with_code(&zipError, errorCode);
std::cerr << "Failed to open output file " << zipFilePath << ": "
<< zip_error_strerror(&zipError) << std::endl;
zip_error_fini(&zipError);
}
}
int main() {
//zip file
// CompressFile("C:/Data/Builder/Demo/",
// "C:/Data/Builder/Demo/");
//zip file夹
CompressDirectory("C:/Data/Builder/Demo", "C:/Data/Builder/");
return 0;
}
of the following:
# Output cmake version hints
message(STATUS "The CMAKE_VERSION is ${CMAKE_VERSION}.")
# Minimum required version of cmake
cmake_minimum_required (VERSION 3.9)
# Project name, version, language
project (ZipTest VERSION 0.1 LANGUAGES CXX)
# cpp17 support
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Find dependent libraries
find_package(libzip REQUIRED)
# Add the source code to the executable for this project.
add_executable (${PROJECT_NAME} "")
# Link dependent libraries
target_link_libraries(${PROJECT_NAME} PRIVATE libzip::zip)
of the following:
{
"version": 2,
"configurePresets": [
{
"name": "linux-default",
"displayName": "Linux Debug",
"description": "Aimed at Linux (used form a nominal expression) Windows subsystems(WSL)or remote Linux systems。",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}"
},
"vendor": {
"/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Linux" ] },
"/VisualStudioRemoteSettings/CMake/1.0": { "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}" }
}
},
{
"name": "windows-default",
"displayName": "Windows x64 Debug",
"description": "geared towards Visual Studio 开发环境(used form a nominal expression) Windows。",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}"
},
"vendor": { "/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Windows" ] } }
},
{
"name": "RelWithDebInfo",
"displayName": "Windows x64 RelWithDebInfo Shared Library",
"description": "geared towards Visual Studio 开发环境(used form a nominal expression) Windows。",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}"
},
"vendor": { "/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Windows" ] } }
}
]
}
Be sure to note that we're using CMake's recommended and relatively new target linking mechanism to introduce the libzip library here, and be sure to review the previous blog post on this point. The point here is that iffind_package(libzip REQUIRED)
fails, then it may be necessary to specify the installation directory for the dependent libraries, specifically by adding the RelWithDebInfo configuration in the fileCMAKE_PREFIX_PATH
, the author here uses the directory pointed to by the GISBasic environment variable. As for how to build and install libzip? You can refer to the previous blog posts in this series.
{
"name": "RelWithDebInfo",
"displayName": "Windows x64 RelWithDebInfo Shared Library",
"description": "geared towards Visual Studio development environment Windows。",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
"CMAKE_PREFIX_PATH": "$env{GISBasic}",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}"
},
"vendor": {
"/VisualStudioSettings/CMake/1.0": {
"hostOS": [
"Windows"
]
}
}
}
4. Summary
Well, the steps and notes for CMake project development using Visual Studio 2019 are above. In fact, I would like to use Visual Studio 2022 or even a newer version for CMake project development, but limited by the working environment did not upgrade. Readers who have tried it are welcome to leave a comment to see what differences or enhancements there are compared with Visual Studio 2019.