我想从CMake项目为跨平台
Linux项目生成Visual Studio解决方案.
Visual Studio 2017跨平台工作负载运行良好,特别是在调试方面.我用它来定位WSL.
现在我有一个现有的Linux CMake项目,我想在Windows和Visual Studio上开发并在WSL上构建它.我似乎没有看到为Visual Studio生成适当的解决方案的方法.任何人都可以开导我吗?
解决方法
CMake已经有
some queries支持“Linux”项目类型,但我不认为
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
对于您选择的任何编译选项,这仍然需要一个模板.但使用模板使这种方法更灵活.