locked
How to compile .cpp files from cmd.exe

    Question

  • I am using visual studio 2010. I wanted to compile my cpp from cmd. I can compile it using   "cl file.cpp" comand in visual studio developer command line tool.

    But I wanted to compile it from the built in cmd of windows. When I try to use the command "cl file.cpp" it does not recognize the command. I even tried to cd to cl.exe path and run from there but then it gives a mspdb110.dll is missing error.

    I herd that we need to put environment variables but I am not able to find anywhere which variables to set.

    Sunday, May 13, 2012 6:23 PM

Answers

  • RANA TALLAL JAVED wrote:
    >
    >Well you all are right but the problem is that I wanted to make a general
    >law that whenever you type in cmd cl it recognizes it
    >
    >If I run varsall.bat file then it only gives me recognition of cl in the
    >cmd window in which I called the bat file.  Similar is the case with
    >calling of vcvars32.bat
    >
    >I wanted a general setting. Or something like run once and all the cmd
    >shells recognize the cl command.
     
    Seriously, you ought to be able to figure this out yourself.  You have been
    given everything you need.
     
    Ask yourself this.  What does vcvars32.bat do?  You have the file -- go
    look at it.  What it does is change some environment variables and create
    some new ones.  You can certainly go into the System control panel and make
    all those changes globally.
     
    If you find the code too dense, do this in a fresh cmd window:
     
        set > before.txt
        vcvars32
        set > after.txt
     
    Now, you can compare the two text files with your favorite "diff" tool, and
    find out exactly which variables were changed.
     
    HOWEVER!  Before you go down this path, let me tell you why this is a BAD
    IDEA.  The problem is that VS2010 is not the last version of Visual Studio
    that will ever be made.  Let's say you had made these environment variable
    changes global when VS2005 came out.  Then, VS2008 comes out.  You will
    probably want to be able to compile with 2008, but all of your global
    environment variables are set to VS2005.
     
    FURTHER, you can't even run vcvars32.bat from VS2008, because you have
    alreday polluted the global environment with settings from VS2005!
     
    Really, the best plan is to do what I suggested earlier: make yourself a
    desktop icon that uses "cmd /k" to run vcvars32, then use that icon to
    launch the shell.  That makes it trivially easy to switch to a new version,
    or even keep them both and switch back and forth.
    --
    Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.
     

    Tim Roberts, VC++ MVP Providenza & Boekelheide, Inc.
    • Proposed as answer by Venzoo Friday, May 18, 2012 8:12 AM
    • Marked as answer by RANA TALLAL JAVED Sunday, May 20, 2012 1:35 PM
    Wednesday, May 16, 2012 4:59 AM

All replies

  • The file that has to be executed is called “vcvarsall.bat” and is located in “VC” subfolder of Visual Studio. It is executed automatically when you select “Visual Studio Command Prompt” from Start menu.


    • Edited by Viorel_MVP Sunday, May 13, 2012 7:01 PM
    Sunday, May 13, 2012 7:00 PM
  • Well yes that does work but I want to configure it like auto picker. Just write cl in command prompt and it says yeh I do recognize it. The .bat file is basically making changes to the env variables. Is there a way to make these changes permanent.

    Sunday, May 13, 2012 10:24 PM
  • RANA TALLAL JAVED wrote:

    Well yes that does work but I want to configure it like auto picker.  Just write cl in command prompt and it says yeh I do
    recognize it. The .bat file is basically making changes to the env  variables. Is there a way to make these changes permanent.

    Control Panel | System | Advanced | Environment Variables. You'll have  to figure out which variables that BAT file sets, and set them up  identically in this dialog.


    Igor Tandetnik

    Monday, May 14, 2012 12:16 AM
  • RANA TALLAL JAVED wrote:
    >
    >Well yes that does work but I want to configure it like auto picker. Just
    >write cl in command prompt and it says yeh I do recognize it. The .bat file
    >is basically making changes to the env variables. Is there a way to make
    >these changes permanent.
     
    What I do is create a desktop shortcut to this:
       %comspec% /k ""C:\VS10\VC\vcvarsall.bat"" x86
     
    Now I can click that icon instead of starting a bare cmd.exe, and I'm all
    set to do command-line compilation.
     
    Actually, I have one for VC6, one for VC2003, one for VC2005, one for
    VC2008, one for VC2010, and more for various DDK environments.  I do
    virtually all of my development work in a command line.
    --
    Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.
     

    Tim Roberts, VC++ MVP Providenza & Boekelheide, Inc.
    Monday, May 14, 2012 6:21 AM
  • Well you all are right but the problem is that I wanted to make a general law that whenever you type in cmd cl it recognizes it

    If I run varsall.bat file then it only gives me recognition of cl in the cmd window in which I called the bat file.
    Similar is the case with calling of vcvars32.bat

    I wanted a general setting. Or something like run once and all the cmd shells recognize the cl command.


    Monday, May 14, 2012 10:47 AM
  • RANA TALLAL JAVED wrote:
    >
    >Well you all are right but the problem is that I wanted to make a general
    >law that whenever you type in cmd cl it recognizes it
    >
    >If I run varsall.bat file then it only gives me recognition of cl in the
    >cmd window in which I called the bat file.  Similar is the case with
    >calling of vcvars32.bat
    >
    >I wanted a general setting. Or something like run once and all the cmd
    >shells recognize the cl command.
     
    Seriously, you ought to be able to figure this out yourself.  You have been
    given everything you need.
     
    Ask yourself this.  What does vcvars32.bat do?  You have the file -- go
    look at it.  What it does is change some environment variables and create
    some new ones.  You can certainly go into the System control panel and make
    all those changes globally.
     
    If you find the code too dense, do this in a fresh cmd window:
     
        set > before.txt
        vcvars32
        set > after.txt
     
    Now, you can compare the two text files with your favorite "diff" tool, and
    find out exactly which variables were changed.
     
    HOWEVER!  Before you go down this path, let me tell you why this is a BAD
    IDEA.  The problem is that VS2010 is not the last version of Visual Studio
    that will ever be made.  Let's say you had made these environment variable
    changes global when VS2005 came out.  Then, VS2008 comes out.  You will
    probably want to be able to compile with 2008, but all of your global
    environment variables are set to VS2005.
     
    FURTHER, you can't even run vcvars32.bat from VS2008, because you have
    alreday polluted the global environment with settings from VS2005!
     
    Really, the best plan is to do what I suggested earlier: make yourself a
    desktop icon that uses "cmd /k" to run vcvars32, then use that icon to
    launch the shell.  That makes it trivially easy to switch to a new version,
    or even keep them both and switch back and forth.
    --
    Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.
     

    Tim Roberts, VC++ MVP Providenza & Boekelheide, Inc.
    • Proposed as answer by Venzoo Friday, May 18, 2012 8:12 AM
    • Marked as answer by RANA TALLAL JAVED Sunday, May 20, 2012 1:35 PM
    Wednesday, May 16, 2012 4:59 AM