Visual Studio Linux 跨平台的 CMake 生成器
CMake Generator for Visual Studio Linux cross-platform
我想从 CMake 项目为跨平台 Linux 项目生成 Visual Studio 解决方案。
Visual Studio2017 跨平台工作负载运行良好,尤其是在调试方面。我用它来定位 WSL。
现在我有一个现有的 Linux CMake 项目,我想在 Windows 和 Visual Studio 上开发它并在 WSL 上构建它。我只是似乎没有看到为 Visual Studio 生成适当解决方案的方法。谁能赐教一下?
已经有 some queries to support the "Linux" project type by CMake, but I don't think that there is something implemented yet(查看代码无法生成所需的项目设置)。
在这些情况下,您只能使用 include_external_msproject()
命令调用。
这会将现有的 .vcproj
文件包含到您的 CMake 生成的解决方案中,例如:
include_external_msproject(
MyProject
${CMAKE_CURRENT_SOURCE_DIR}/MyProject/MyProject.vcproj
)
所以让我们制作一个现有 Linux .vcxproj
文件的模板,如下所示:
<?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()
命令:
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 项目为跨平台 Linux 项目生成 Visual Studio 解决方案。
Visual Studio2017 跨平台工作负载运行良好,尤其是在调试方面。我用它来定位 WSL。 现在我有一个现有的 Linux CMake 项目,我想在 Windows 和 Visual Studio 上开发它并在 WSL 上构建它。我只是似乎没有看到为 Visual Studio 生成适当解决方案的方法。谁能赐教一下?
已经有 some queries to support the "Linux" project type by CMake, but I don't think that there is something implemented yet(查看代码无法生成所需的项目设置)。
在这些情况下,您只能使用 include_external_msproject()
命令调用。
这会将现有的 .vcproj
文件包含到您的 CMake 生成的解决方案中,例如:
include_external_msproject(
MyProject
${CMAKE_CURRENT_SOURCE_DIR}/MyProject/MyProject.vcproj
)
所以让我们制作一个现有 Linux .vcxproj
文件的模板,如下所示:
<?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()
命令:
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
无论您选择何种编译选项,这仍然需要一个模板。但是使用模板使这种方法更加灵活。