我正在使用CMake生成Visual Studio项目,除了一件事,其他都很好。
解决方案中的启动项目总是ALL_BUILD
。如何通过CMake将启动项目更改为我想要的真正项目?
我正在使用CMake生成Visual Studio项目,除了一件事,其他都很好。
解决方案中的启动项目总是ALL_BUILD
。如何通过CMake将启动项目更改为我想要的真正项目?
现在CMake通过版本3.6及更高版本的VS_STARTUP_PROJECT
目录属性来支持此功能:
cmake_minimum_required(VERSION 3.6)
project(foo)
# ...
add_executable(bar ${BAR_SOURCES})
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT bar)
这将把bar
设置为foo.sln
解决方案的启动项目。
你无法做到。启动项目存储在二进制文件中,这个文件不是由CMake生成的。如果没有这个二进制文件,Visual Studio会默认使用解决方案文件中的第一个项目,而ALL_BUILD项目总是第一个...
更新:此答案已经“过时”,因为使用CMake 3.6现在可以实现。请参见ComicSansMS的答案。
create_default_target_launcher(
your_desired_target_name
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/desired_path/"
# or ${CMAKE_CURRENT_BINARY_DIR}, depending on your setup
)
这个模块可以在rpavlik的github上找到。你只需要在你的最顶层CMakeLists.txt文件中添加下面的内容:
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/external/rpavlik-cmake-modules-1c73e35") # or whichever path you put the module in.
include(CreateLaunchers)
这里提供的示例。
slnStartupProject slnFilename projectName
https://github.com/michaKFromParis/slnStartupProject
欢迎使用分支和反馈。use strict;
use File::Spec;
# variables
my $slnPath = File::Spec->rel2abs($ARGV[0]);
my $projectName = $ARGV[1];
my $contents;
my $header;
my $project;
my $GUID = "[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}";
my $fh;
# read file content (error if not found)
print "Setting \"$projectName\" as Startup Project in \"$slnPath\"...\n";
die "Error: path \"$slnPath\" not found!\n" if not -f $slnPath;
open($fh, "<", $slnPath) or die "Error: cannot read $slnPath: $!";
$contents = do { local $/; <$fh> };
close($fh) or warn "close failed: $!";
# extract part before Projects definition section (the first mention of "Project([GUID])")
$header = $1 if $contents =~ s{(.*?(?=Project\("\{${GUID}\}"\)))}{}si;
# extract definition of the project specified (error if not found)
$project = $1 if $contents =~ s{(Project\("\{${GUID}\}"\) = \"${projectName}\".*?EndProject\s)}{}si;
die "Error: Project not found!\n" if not defined $project or not length $project;
# write header, project definition and remaining content back into the file
`attrib -R "$slnPath"`;
open($fh, ">", $slnPath) or die "Error: cannot write to $slnPath: $!";
print $fh $header, $project, $contents;
close($fh) or warn "close failed: $!";
print "Successfully done.\n";
使用cmake 3.5,可以通过以下方式更改启动项目(适用于VS 2010)
SET(CMAKE_DEFAULT_STARTUP_PROJECT myFavoriteProject)
VS_STARTUP_PROJECT
指令有何不同? - tresfVS_STARTUP_PROJECT
属性来处理VS 2010/2015解决方案。 - mtartar