2

I have a project that I converted from a makefile that has a source file that expects the command line options from the compiler. For example for when the project was built with gcc if you did program --help it would spit out the gcc command line used to compile the program.

How can I do the same thing in Visual Studio, so that it spits out the cl command line used to compile the program? Basically I want to hit F7 (to build solution) and have the whole thing automated. I can't find a macro for it. Thanks

edit; I mean programatically, so for example I want when I run the program for its output to contain the cl.exe command string that is used. You can see the command line at Configuration Properties > C/C++ > Command Line > All Options but I can't find a macro for it or some way to encapsulate it in a file.

loop
  • 3,460
  • 5
  • 34
  • 57
  • What are those cmd line options? Many options you can change from properties dialog – Digital_Reality Jan 03 '14 at 07:12
  • I dont know if this would work in windows (or linux for that matter) but could you pipe a file into the exe as it is run? – thermite Jan 03 '14 at 08:41
  • 4
    It is already automated, you don't have to help. IF you want to see the command lines passed to the compiler then just take a look at the `cl.command.*.tlog` files in the build directory. – Hans Passant Jan 03 '14 at 10:01
  • Duplicate of https://stackoverflow.com/questions/1211841/how-can-i-make-visual-studios-build-be-very-verbose/7273530 – nephewtom Feb 04 '19 at 22:34

3 Answers3

4

A .vcxproj is a Visual Studio project in the MSBuild project format. You can build it by running msbuild.exe, devenv.exe, devenv.com, using the Visual Studio GUI or the MSBuild API.

Visual Studio GUI uses the MSBuild API. In doing so, it limits the MSBuild output. If you want more details, change your user settings in Visual Studio:

Tools > Options > Project and Solutions > Build and Run > two verbosity settings

Detailed will show the cl.exe command lines.

Tom Blodget
  • 20,260
  • 3
  • 39
  • 72
  • Are you sure? Don't have VS2010 here anymore, but in VS2012 at diagnostic verbosity it shows the TaskParameters to the CL task but not the cl.exe command line - moreover that's still a long way from getting the command line at copile time, in a source file. – stijn Jan 03 '14 at 19:54
  • 2
    @stijn Yes. Cl.exe and link.exe are called through Tracker.exe. But, you're that doesn't get even half way to including that info into the program. Capturing it into a soure file and repeating the build until it doesn't change would be one way. – Tom Blodget Jan 04 '14 at 01:00
  • You're right, must have missed something yesterday: the full CL commandline is indeed printed in the output. – stijn Jan 04 '14 at 10:27
3

Since VS switched the underlying build system to MsBuild the command line as shown in that dialog is created programatically within VS only. It might not even be the exact command line passed to cl: MsBuild itself invokes CL via a task and as such there is no direct link with what is shown in VS nor is there a way to get the command line out of it.

Anyway, there is no such thing as the command line since each source file might have different options. Furthermore I doubt you want the full commandline including the absolute include paths etc. Nobody is interested in that. Now if you make clever use of the macros from http://msdn.microsoft.com/en-us/library/b0084kay.aspx you can sort of recreate the command line yourself since most options are there:

std::string CompilerCommandLineOptions()
{
  std::string cmd;
  #ifdef _CHAR_UNSIGNED
  cmd += " /J";
  #endif
  #ifdef __cplusplus_cli
  cmd += " /clr";
  #endif
  #ifdef _CPPRTTI
  cmd += " /GR"
  #endif
  //etc
  return cmd;
}

Note: are you sure it's worth the hassle? Is there really somebody interested in the command line? It's not even sufficient to build the project. Why not the linker options as well then?

stijn
  • 34,664
  • 13
  • 111
  • 163
0

The closest thing which I came across cl command line which msuild executes is "hacking" the rsp file used while calling cl.exe.

Using Override compiler solution, I changed ClCompile ToolExe to custom mycl.bat script and this script received an argument which was @tmp-1234xxx.rsp file. This rsp file contained whole command line except cl.exe path, something like -

rsp file

/P /DDEBUG Source.cpp

Then after making desired changes in the rsp file by calling a separate bash script which were very minor for me, I called cl.exe with contents of my rsp file. So, whenever user hits the build button, this script executes.

mycl.bat script

@echo off
SET PATH=%PATH%;/usr/bin  //to call cygwin bash
set parameter=%1
set parameter=%parameter:~1% //to remove @ in the beginning 
c:/cygwin/bin/bash process.sh %parameter%

process.sh

iconv -f UCS-2 -t UTF-8 <$1 >$1.conv  //file converted to UTF-8, else bash wasn't handling it well
contents=`cat $1.conv`
#Processing on file contents here
path/to/cl.exe $(contents)

Very nasty solution, but it worked for my use case. I wanted to change the names of the file on the go based on some logic.

The problem I faced is Visual Studio uses tlogs written by CL Task to check while file needs to to be rebuilt on incremental build and my target's tlogs files were not enough. There were tlogs of every command in batch and bash scripts but not for whole target. So, it was building whole thing on incremental builds also.