แก้ไข

แชร์ผ่าน


Tutorial: Create C++ cross-platform projects in Visual Studio

Visual Studio C and C++ development isn't just for Windows anymore. This tutorial shows how to use Visual Studio for C++ cross platform development on Windows and Linux. It's based on CMake, so you don't have to create or generate Visual Studio projects. When you open a folder that contains a CMakeLists.txt file, Visual Studio configures the IntelliSense and build settings automatically. You can quickly start editing, building, and debugging your code locally on Windows. Then, switch your configuration to do the same on Linux, all from within Visual Studio.

In this tutorial, you learn how to:

  • clone an open-source CMake project from GitHub
  • open the project in Visual Studio
  • build and debug an executable target on Windows
  • add a connection to a Linux machine
  • build and debug the same target on Linux

Prerequisites

  • Set up Visual Studio for Cross Platform C++ Development

    • First, install Visual Studio and choose the Desktop development with C++ and Linux development with C++ workloads. This minimal install is only 3 GB. Depending on your download speed, installation shouldn't take more than 10 minutes.
  • Set up a Linux machine for Cross Platform C++ Development

    • Visual Studio doesn't require any specific distribution of Linux. The OS can be running on a physical machine, in a VM, or in the cloud. You could also use the Windows Subsystem for Linux (WSL). However, for this tutorial, a graphical environment is required. WSL isn't recommended here, because it's intended primarily for command-line operations.

    • Visual Studio requires these tools on the Linux machine: C++ compilers, gdb, ssh, rsync, make, and zip. On Debian-based systems, you can use this command to install these dependencies:

      sudo apt install -y openssh-server build-essential gdb rsync make zip
      
    • Visual Studio requires a recent version of CMake on the Linux machine that has server mode enabled (at least 3.8). Microsoft produces a universal build of CMake that you can install on any Linux distro. We recommend you use this build to ensure that you have the latest features. You can get the CMake binaries from the Microsoft fork of the CMake repo on GitHub. Go to that page and download the version that matches the system architecture on your Linux machine, then mark it as an executable:

      wget <path to binary>
      chmod +x cmake-3.11.18033000-MSVC_2-Linux-x86_64.sh
      
    • You can see the options for running the script with --help. We recommend that you use the -prefix option to specify installing in the /usr path, because /usr/bin is the default location where Visual Studio looks for CMake. The following example shows the Linux-x86_64 script. Change it as needed if you're using a different target platform.

      sudo ./cmake-3.11.18033000-MSVC_2-Linux-x86_64.sh --skip-license --prefix=/usr
      
  • Git for windows installed on your Windows machine.

  • A GitHub account.

Clone an open-source CMake project from GitHub

This tutorial uses the Bullet Physics SDK on GitHub. It provides collision detection and physics simulations for many applications. The SDK includes sample executable programs that compile and run without having to write other code. This tutorial doesn't modify any of the source code or build scripts. To start, clone the bullet3 repository from GitHub on the machine where you have Visual Studio installed.

git clone https://github.com/bulletphysics/bullet3.git
  1. On the Visual Studio main menu, choose File > Open > CMake. Navigate to the CMakeLists.txt file in the root of the bullet3 repo you downloaded.

    Screenshot of Visual Studio menu showing File > Open > C Make. A folder has yet to be opened. This is just the menu opened to this point.

    As soon as you open the folder, your folder structure becomes visible in the Solution Explorer.

    Screenshot of the Solution Explorer window in Folder View mode. It displays the contents of the project (files and folders) and CMakeLists.txt is highlighted.

    This view shows you exactly what is on disk, not a logical or filtered view. By default, it doesn't show hidden files.

  2. Choose the Show all files button to see all the files in the folder.

    Screenshot of the Solution Explorer window with the Show All Files button highlighted. This button sits on top of the solution explorer window and to the right.

Switch to targets view

When you open a folder that uses CMake, Visual Studio automatically generates the CMake cache. This operation might take a few moments, depending on the size of your project.

  1. In the Output Window, select Show output from and then choose CMake to monitor the status of the cache generation process. When the operation is complete, it says "Target info extraction done."

    Screenshot of the Output window. The Show output from: dropdown is set to CMake.

    After this operation completes, IntelliSense is configured. You can build the project, and debug the application. Visual Studio now shows a logical view of the solution, based on the targets specified in the CMakeLists files.

  2. Use the Solutions and Folders button in the Solution Explorer to switch to CMake Targets View.

    Screenshot of the Solutions and Folders button in the Solution Explorer. It is selected, showing a dropdown with a choice for c:\projects\bullet3 and another choice for CMake Targets View, which is selected.

    Here's what that view looks like for the Bullet SDK:

    Screenshot of the Solution Explorer CMake targets view. It contains an entry called BULLET_PHYSICS Project, under which are entries like App_BasicExample (executable), App_ExampleBrowser (executable), and so on.

    Targets view provides a more intuitive view of what is in this source base. You can see some targets are libraries and others are executables.

  3. Expand a node in CMake Targets View to see its source code files, wherever those files might be located on disk.

Add an explicit Windows x64-Debug configuration

Visual Studio creates a default x64-Debug configuration for Windows. Configurations are how Visual Studio understands what platform target it's going to use for CMake. The default configuration isn't represented on disk. When you explicitly add a configuration, Visual Studio creates a file called CMakeSettings.json. It's populated with settings for all the configurations you specify.

  1. Add a new configuration. Open the Configuration drop-down in the toolbar and select Manage Configurations.

    Screenshot of the Configuration drop-down in the toolbar. Manage Configurations... is selected.

    The CMake Settings Editor opens. Select the green plus sign on the left-hand side of the editor to add a new configuration. The Add Configuration to CMakeSettings dialog appears:

    Screenshot of the Add Configuration to CMakeSettings dialog. It has entries such as Linux-Debug, x86-Debug. x64-Debug is selected.

    This dialog shows all the configurations included with Visual Studio, plus any custom configurations that you create. If you want to continue to use a x64-Debug configuration that should be the first one you add. Select x64-Debug, and then choose the Select button. Visual Studio creates the CMakeSettings.json file with a configuration for x64-Debug, and saves it to disk. You can use whatever names you like for your configurations by changing the name parameter directly in CMakeSettings.json.

Set a breakpoint, build, and run on Windows

In this step, we debug an example program that demonstrates the Bullet Physics library.

  1. In Solution Explorer, select AppBasicExampleGui and expand it.

  2. Open the file BasicExample.cpp.

  3. Set a breakpoint that gets hit when you click in the running application. The click event is handled in a method within a helper class. To quickly get there:

    1. Select CommonRigidBodyBase that the struct BasicExample is derived from. It's around line 30.

    2. Right-click and choose Go to Definition. Now you're in the header CommonRigidBodyBase.h.

    3. In the browser view above your source, you should see that you're in the CommonRigidBodyBase. To the right, you can select members to examine. Open the drop-down and select mouseButtonCallback to go to the definition of that function in the header.

      Screenshot of the Member list toolbar drop-down in the editor window. It list functions such as getRayTo(in x, int y). The mouse button callback method is highlighted.

  4. Place a breakpoint on the first line within this function. It gets hit when you click a mouse button within the window of the application, when run under the Visual Studio debugger.

  5. To launch the application, select the launch drop-down in the toolbar. It's the one with the green play icon that says "Select Startup Item." In the drop-down, select AppBasicExampleGui.exe. The executable name now displays on the launch button:

    Screenshot of the Visual Studio toolbar launch drop-down. AppBasicExampleGui.exe is selected, but other options are visible such as App_ExampleBrowser.exe, App_HelloWorld.exe, and others.

  6. Choose the launch button to build the application and necessary dependencies, then launch it with the Visual Studio debugger attached. After a few moments, the running application appears:

    Screenshot of the running application. It's a collection of colored blocks on a yellow plane.

  7. Move your mouse into the application window, then click a button to trigger the breakpoint. The breakpoint brings Visual Studio back to the foreground, and the editor shows the line where execution is paused. You can inspect the application variables, objects, threads, and memory, or step through your code interactively. Choose Continue to let the application resume, and then exit it normally. Or, halt execution within Visual Studio by using the stop button.

Add a Linux configuration and connect to the remote machine

  1. Add a Linux configuration. Right-click the CMakeSettings.json file in the Solution Explorer view and select Add Configuration. You see the same Add Configuration to CMakeSettings dialog as before. Select Linux-Debug this time, then save the CMakeSettings.json file (ctrl + s).

  2. Visual Studio 2019 version 16.6 or later Scroll down to the bottom of the CMake Settings Editor and select Show advanced settings. Select Unix Makefiles as the CMake generator, then save the CMakeSettings.json file (ctrl + s).

  3. Select Linux-Debug in the configuration drop-down.

    Screenshot of the launch configuration drop-down. The visible options are: x64-Debug, Linux-Debug, and Manage Configurations.

    If it's the first time you're connecting to a Linux system, the Connect to Remote System dialog appears.

    Screenshot of the Visual Studio Connect to Remote System dialog.

    The dialog has fields for the host name, port, user name, authentication type, and password. All of the fields are blank except Port is set to 22 and Authentication type is set to Password.

    If you've already added a remote connection, you can open this window by navigating to Tools > Options > Cross Platform > Connection Manager.

  4. Provide the connection information to your Linux machine and choose Connect. Visual Studio adds that machine as to CMakeSettings.json as your default connection for Linux-Debug. It also pulls down the headers from your remote machine, so you get IntelliSense specific to that remote connection. Next, Visual Studio sends your files to the remote machine and generates the CMake cache on the remote system. These steps might take some time, depending on the speed of your network and power of your remote machine. You know it's complete when the message "Target info extraction done" appears in the CMake output window.

Set a breakpoint, build, and run on Linux

Because it's a desktop application, you need to provide some more configuration information to the debug configuration.

  1. In the CMake Targets view, right-click AppBasicExampleGui and choose Debug and Launch Settings to open the launch.vs.json file that's in the hidden .vs subfolder. This file is local to your development environment. You can move it into the root of your project if you wish to check it in and save it with your team. In this file, a configuration has been added for AppBasicExampleGui. These default settings work in most cases, but not here. Because it's a desktop application, you need to provide some additional information to launch the program so you can see it on your Linux machine.

  2. To find the value of the environment variable DISPLAY on your Linux machine, run this command:

    echo $DISPLAY
    

    In the configuration for AppBasicExampleGui, there's a parameter array, "pipeArgs". It contains a line: "${debuggerCommand}". It's the command that launches gdb on the remote machine. Visual Studio must export the display into this context before that command runs. For example, if the value of your display is :1, modify that line as follows:

    "export DISPLAY=:1;${debuggerCommand}",
    
  3. Launch and debug your application. Open the Select Startup Item drop-down in the toolbar and choose AppBasicExampleGui. Next, either choose the green play icon in the toolbar, or press F5. The application and its dependencies are built on the remote Linux machine, then launched with the Visual Studio debugger attached. On your remote Linux machine, you should see an application window appear.

  4. Move your mouse into the application window, and click a button. The breakpoint is hit. Program execution pauses, Visual Studio comes back to the foreground, and you see your breakpoint. You should also see a Linux Console Window appear in Visual Studio. The window provides output from the remote Linux machine, and it can also accept input for stdin. Like any Visual Studio window, you can dock it where you prefer to see it. Its position is persisted in future sessions.

    Screenshot of the Visual Studio Linux Console Window.

    The output in the window indicates that the C11 functions dynamically loaded using dlopen/dlsym is OK, a GL 3.0 context has been created and the Direct GLX rendering context obtained and made current. The window has various version information for GL_VENDOR, GL_VERSION, GL_SHADING_LANGUAGE_VERSION, and so on.

  5. You can inspect the application variables, objects, threads, memory, and step through your code interactively using Visual Studio. But this time, you're doing it all on a remote Linux machine instead of your local Windows environment. You can choose Continue to let the application resume and exit normally, or you can choose the stop button, as with local execution.

  6. Look at the Call Stack window and view the Calls to x11OpenGLWindow since Visual Studio launched the application on Linux.

    The Visual Studio Call Stack window, showing Linux call stack.

    The callstack shows the breakpoint on CommonRigidBodyBase::mouseMoveCallback, and the calls that precede it such as OnMouseMove, X11OpenGLWindow::pumpMessage, and so on.

What you learned

In this tutorial, you cloned a code base directly from GitHub. You built, ran, and debugged it on Windows without modifications. Then you used the same code base, with minor configuration changes, to build, run, and debug on a remote Linux machine.

Next steps

Learn more about configuring and debugging CMake projects in Visual Studio: