Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Add Linux Compilation to Cmake Project in Visual Studio

Visual Studio has added lots of new features for C++ in the past year.

CMake With the CMake support, I can do "Open Folder" and select a folder with a CMakeLists.txt file in it. Visual Studio does a lot of nice work in discovering and building it automatically.

Linux Compilation Visual studio now supports remote compilation on Linux over SSH. Several tutorials show how users can create a new "Linux Console Application" in Visual Studio, and it will automatically ask to setup an SSH connection to be used for building it. I don't see any instructions for how to do this on an existing project of any kind.

Particularly with a CMake project, is it possible to open a CMake folder in Visual Studio 2017 and have it built over on a remote Linux machine? IfSoHow?

like image 673
solvingJ Avatar asked Jun 10 '17 01:06

solvingJ


People also ask

How do I compile CMakeLists TXT in Visual Studio?

Building CMake projects Select the preferred configuration and press F5, or choose the Run (green triangle) button on the toolbar. The project automatically builds first, just like a Visual Studio solution. Right-click on CMakeLists. txt in Solution Explorer and select Build from the context menu.

How use CMake Linux?

Open the Command Palette (Ctrl+Shift+P) and run the CMake: Build command, or select the Build button from the Status bar. You can select which targets you'd like to build by selecting CMake: Set Build Target from the Command Palette. By default, CMake Tools builds all targets.


1 Answers

There is no build-in support for a VS "Linux Console Application" in CMake yet (as for CMake version 3.9).

Edit: Visual Studio 2017 15.4 now comes with something similar without generating actual .vcxproj files. See Visual C++ for Linux Development with CMake

With a standard CMake version besides the possibilities described here using existing .vcxproj files as a template, you can only trick CMake into generating those project types:

cmake_minimum_required(VERSION 3.7)

project(HelloLinux)

file(WRITE main.cpp [=[
#include <iostream>

int main()
{
    std::cout << "Hello from Linux Console!" << std::endl;
}
]=])

add_executable(HelloLinux "main.cpp")

set_target_properties(
    HelloLinux
    PROPERTIES
        VS_GLOBAL_KEYWORD "Linux"
        VS_GLOBAL_ApplicationType "Linux"
        VS_GLOBAL_ApplicationTypeRevision "1.0"
        VS_GLOBAL_TargetLinuxPlatform "Generic"
        VS_GLOBAL_LinuxProjectType "{D51BCBC9-82E9-4017-911E-C93873C4EA2B}"
)

This actually works and produces a Linux .vcxproj project that is accepted by VS. But since we sidestepped CMake here, none of the other compiler/linker options you define in your CMake script will be assigned.

So my recommendation is to raise a feature request for CMake itself to directly support this (e.g. via platform toolset option Remote_GCC_1_0).

like image 68
Florian Avatar answered Sep 19 '22 18:09

Florian