How to Add Linux Compilation to Cmake Project in Visual Studio

后端 未结 2 1091
陌清茗
陌清茗 2020-12-17 01:16

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 w

相关标签:
2条回答
  • 2020-12-17 01:48

    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).

    0 讨论(0)
  • 2020-12-17 02:07

    It does not seem to work as you expect, yet. It seems you need to create separate linux vcproject for your existing cmake codebase. There is nothing like linux target in VS options. For more information see comments in this msdn blog.

    You may either create 'new linux project' and copy your sources or try (and adapt) using for existing sources these unofficial scripts: https://github.com/robotdad/vclinux

    0 讨论(0)
提交回复
热议问题