CMake Generator for Visual Studio Linux跨平台 [英] CMake Generator for Visual Studio Linux cross-platform

查看:104
本文介绍了CMake Generator for Visual Studio Linux跨平台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从CMake项目中为跨平台Linux项目生成Visual Studio解决方案.

I want to generate a Visual Studio Solution for a cross-platform Linux Project from a CMake Project.

Visual Studio 2017跨平台工作负载可以很好地工作,尤其是在调试时.我用它来瞄准WSL. 现在,我有一个现有的Linux CMake项目,我想在Windows和Visual Studio上进行开发并在WSL上进行构建.我似乎似乎没有找到为Visual Studio生成适当解决方案的方法.谁能启发我?

The Visual Studio 2017 cross-platform workload works nicely, especially when it comes to debugging. I use it to target the WSL. Now I have an existing Linux CMake project that I want to develop on Windows and Visual Studio and build it on WSL. I just don't seem to see a way of generating an appropriate Solution for Visual Studio. Can anyone enlighten me?

推荐答案

已经有

There were already some queries to support the "Linux" project type by CMake, but I don't think that there is something implemented yet (looking at the code it's not able to generate the required project settings).

在这种情况下,您只能使用 include_external_msproject() 命令调用.

In those cases you can only work with include_external_msproject() command calls.

这会将现有的.vcproj文件包含到CMake生成的解决方案中,例如:

This would include an existing .vcproj file into your CMake generated solution like:

include_external_msproject(
    MyProject 
    ${CMAKE_CURRENT_SOURCE_DIR}/MyProject/MyProject.vcproj
)


因此,让我们为现有的Linux .vcxproj文件创建一个模板,如下所示:


So let's make a template of an existing Linux .vcxproj file like this:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="@CMAKE_MATCH_1@.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|x86">
      <Configuration>Debug</Configuration>
      <Platform>x86</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|x86">
      <Configuration>Release</Configuration>
      <Platform>x86</Platform>
    </ProjectConfiguration>
  </ItemGroup>
  <PropertyGroup Label="Globals">
    <ProjectGuid>@_guid@</ProjectGuid>
    <Keyword>Linux</Keyword>
    <RootNamespace>@_target@</RootNamespace>
    <MinimumVisualStudioVersion>@CMAKE_MATCH_1@.0</MinimumVisualStudioVersion>
    <ApplicationType>Linux</ApplicationType>
    <ApplicationTypeRevision>1.0</ApplicationTypeRevision>
    <TargetLinuxPlatform>Generic</TargetLinuxPlatform>
    <LinuxProjectType>{D51BCBC9-82E9-4017-911E-C93873C4EA2B}</LinuxProjectType>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'" Label="Configuration">
    <UseDebugLibraries>true</UseDebugLibraries>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'" Label="Configuration">
    <UseDebugLibraries>false</UseDebugLibraries>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  <ImportGroup Label="ExtensionSettings" />
  <ImportGroup Label="Shared" />
  <ImportGroup Label="PropertySheets" />
  <PropertyGroup Label="UserMacros" />
  <ItemGroup>
    <ClCompile Include="@_sources@" />
  </ItemGroup>
  <ItemGroup>
  </ItemGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  <ImportGroup Label="ExtensionTargets" />
</Project>

并使用以下命令创建一个新的add_linux_executable()命令:

And create a new add_linux_executable() command using this:

cmake_minimum_required(VERSION 2.8)

project(ConfigureVCXProjForLinux)

function(add_linux_executable _target)
    if (CMAKE_GENERATOR MATCHES "Visual Studio ([0-9]*)")
        foreach(_source IN LISTS ARGN)
            get_filename_component(_source_abs "${_source}" ABSOLUTE)
            file(TO_NATIVE_PATH "${_source_abs}" _source_native)
            list(APPEND _sources "${_source_native}")
        endforeach()
        file(TO_NATIVE_PATH "${CMAKE_CURRENT_LIST_FILE}" _list_file_native)
        list(APPEND _sources "${_list_file_native}")

        string(
            UUID _guid 
            NAMESPACE "2e4779e9-c831-47b0-b138-3745b2ed6ba9" 
            NAME ${_target}
            TYPE SHA1
            UPPER
        )

        configure_file(
           "LinuxTemplate.vcxproj.in" 
            "${CMAKE_CURRENT_BINARY_DIR}/${_target}.vcxproj" 
            @ONLY
        )

        include_external_msproject(
            ${_target}
            "${CMAKE_CURRENT_BINARY_DIR}/${_target}.vcxproj"
            TYPE "8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942"
            GUID "${_guid}"
        )
    endif()
endfunction()

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

    int main()
    {
        std::cout << "Hello Linux !" << std::endl;
    }
]=])
add_linux_executable(${PROJECT_NAME} "main.cpp")

请注意模板替换:

  • @CMAKE_MATCH_1@表示Visual Studio版本号
  • @_target@用于项目RootNamespace
  • @_sources@对于任何源文件,您都已给``add_linux_executable()`作为参数
  • @_guid@项目的唯一GUID
  • @CMAKE_MATCH_1@ for the Visual Studio version number
  • @_target@ for the project RootNamespace
  • @_sources@ for ever source file you have given ``add_linux_executable()` as an argument
  • @_guid@ a unique GUID for the project

根据您选择的任何编译选项,这仍然需要一个模板.但是使用模板使这种方法更加灵活.

This would still require one template per whatever compilation options you choose. But using templates makes this approach somewhat more flexible.

这篇关于CMake Generator for Visual Studio Linux跨平台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆