如何通过CMake更改Visual Studio解决scheme的启动项目?

我正在使用CMake来生成Visual Studio项目。 除了一件事以外,一切正常。

解决scheme中的启动项目始终是ALL_BUILD 。 我如何通过CMake将启动项目更改为我想要的真实项目?

你不能。 startup-project存储在一个不是由CMake生成的二进制文件中。 没有这个二进制文件,Visual Studio将默认为解决scheme文件中的第一个项目,ALL_BUILD项目始终是第一个…

更新:这个答案是“过时的”,因为现在CMake 3.6是可行的。 查看ComicSansMS的答案 。

CMake现在通过VS_STARTUP_PROJECT目录属性支持3.6及更高版本:

 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解决scheme的启动项目。

自Visual 2005以来,configuration存储在文件名为projectname.vc(x)proj.user中,这是纯粹的xml。

我不知道如何更改启动项目,但是您可以将ALL_BUILD设置为运行所需的可执行文件,而不是显示愚蠢的popup窗口:

 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) 

这里可用的例子。

如果你不能允许像我这样的perl依赖,我只是写了一个叫做slnStartupProject的窗口的命令行工具来解决这个问题。 它像这样自动设置启动项目:

 slnStartupProject slnFilename projectName 

在使用cmake生成解决scheme后,我亲自使用它来设置项目,该解决scheme始终将假ALL_BUILD项目设置为第一个项目。

源代码在github上:

https://github.com/michaKFromParis/slnStartupProject

叉子和反馈是受欢迎的。

希望这可以帮助!

在IDE中点击“设置为启动项目”时,用户作出的明确select存储在二进制文件中是正确的。 但是我发现在其他地方,Visual Studio首次打开解决scheme时将解决scheme中的第一个项目作为隐式启动项目,所以CMake对此有影响。

现在我们的问题是:ALL_BUILD始终是第一个项目。 为了改变这种情况,我在CMake之后运行一个简短的perl脚本,将所需的项目定义从文件中剪切出来并粘贴到前面。 第一个参数的解决scheme文件path,第二个项目名称:

 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"; 

一旦解决scheme已经打开,隐式启动项目被保存在二进制文件中,因此变得明确,所以这甚至还存在CMake重新运行(例如由零检查触发,这不允许执行后)。 以同样的方式,anm明确的用户select也被保留。

(用ActiveState Perl在Win7机器上编写和testing)

使用cmake 3.5,启动项目(对于VS 2010)可以改变

 SET(CMAKE_DEFAULT_STARTUP_PROJECT myFavoriteProject) 

在项目的主CMakeLists.txt中。 默认情况下,它被设置为ALL_BUILD。