A very beginner for Visual C++ with CMake.


   Yesterday, I installed Visual Studio Community 2017 for learning Visual C++ with CMake. Why did I decide to learn Visual C++? Because I want to compile Apache Dev version for Microsoft Windows. The page says httpd can be built on Windows using a cmake-based build system. Besides, Visual C++ Team says they can support CMake in Visual Studio 2017 easily and CMake Team does CMake version 3.7 or higher are compatible with Visual Studio 2017. The time is very good. I decided to learn Visual C++ with CMake.

[About Visual Studio 2017]
   First, download Visual Studio Community 2017 from the site. About installing, see Install Visual Studio 2017. At Step 4 – Select workloads, I checked all check boxes because I don’t know what I need exactly. VS 2017 system Requirements is here.
   The installation takes several time though it depends on your environment. Be patient.

   When you run Visual Studio Community 2017 for the first time, it suggests you sign in to Visual Studio and you need it because Unlocks the Visual Studio Community Edition instead of being limited to the trial period of 30 days.

[About CMake]
   Second, download CMake from the site. Choose the file you need. In my case, cmake-x.x.x-win32-x86.msi. Click the msi file to install. At Install Options dialog, choose “Add CMake to the system PATH for all users” or “Add CMake to the system PATH for the current user”.
   Now I have VS 2017 and CMake on my PC.
   Build HelloWorld.exe for checking I have proper installations or not. I do all in Visual Studio.

  1. Run Visual Studio 2017 from Desktop icon.
  2. To create HelloWorld app, on the menu bar, choose File, New, Project. Like this.
  3. In the Visual C++ category, choose the Win32 Console Application template, and then name the project HelloWorld.
  4. In the HelloWorld.cpp file, enter a blank line before the line return 0; and then enter the following code:
    printf("Hello, World.n");

   At the point, I can get HelloWorld.exe without CMake. But I need CMake. So, I add a CMakeLists.txt file to the directory that includes the HelloWorld.cpp file.

  1. At Solution Explorer, change View from Solution to Folder. Like this.
  2. Right click HelloWorld folder and add a new file. Change the file name from New File to CMakeLists.txt.
  3. In the CMakeLists.txt file, enter the following code:
    add_executable(HelloWorld HelloWorld.cpp)
    Now I have CMake(K) on the Menu bar.
  4. I right click the CMakeLists.txt file at Solution Explorer and build HelloWorld.exe with CMake. See fig.07.
  5. I select HelloWorld.exe in the Startup Item dropdown in the General toolbar and launch the debugger. See fig.08 and fig.09.
  6. The execute time of HelloWorld.exe is veeeeeeeeeeeeeeeeeeeeery short. So I cannot see “Hello, World.” in the cmd window if I set a Break Point at the line return 0;. Any way, I have HelloWorld.exe. I’m happy.

   My goal is still a long way off, though.

Leave a Reply

Your email address will not be published. Required fields are marked *