Run C++ in command prompt - Windows

前端 未结 11 1621
逝去的感伤
逝去的感伤 2020-12-22 19:02

I know that everyone uses an IDE nowadays, but I just find it simpler to write my code in notepad++, compile it using a command prompt command, and run it from there too. At

相关标签:
11条回答
  • 2020-12-22 19:15

    Open cmd and go In Directory where file is saved. Then, For compile, g++ FileName. cpp Or gcc FileName. cpp

    For Run, FileName. exe

    This Is For Compile & Run Program.

    Make sure, gcc compiler installed in PC or Laptop. And also path variable must be set.

    0 讨论(0)
  • 2020-12-22 19:15

    have MinGW compiler bin directory added to path.

    use mingw32-g++ -s -c source_file_name.cpp -o output_file_name.o to compile

    then mingw32-g++ -o executable_file_name.exe output_file_name.o to build exe

    finally, you run with executable_file_name.exe

    0 讨论(0)
  • 2020-12-22 19:17

    It depends on what compiler you're using.

    For example, if you are using Visual C++ .NET 2010 Express, run Visual C++ 2010 Express Command Prompt from the start menu, and you can simply compile and run the code.

    > cl /EHsc mycode.cpp
    > mycode.exe
    

    or from the regular command line, you can run vcvars32.bat first to set up the environment. Alternatively search for setvcvars.cmd (part of a FLOSS project) and use that to even locate the installed VS and have it call vcvars32.bat for you.

    Please check your compiler's manual for command lines.

    0 讨论(0)
  • 2020-12-22 19:24

    If you're running Windows then make use of this:

    g++ -o program program.cpp
    

    g++ is the name of the compiler and -o is the option needed for creating a .o file. Program (without .cpp suffix) is the exe file and program.cpp is your source file that you want to compile.

    g++ -o program program.cpp&program.exe
    

    Use this shortcut to run the .exe file of the program. This may run in Ubuntu but you may have to use .out suffix instead of .exe. Use this handy batch script I made to execute your programs on Windows:

    @echo off&&cls
    set /p pathName=Enter The Path where the file is located:%=%
    cd %pathName%
    REM set /p exec=Enter The Name of the executable you want to make:%=%
    set /p file=Enter The Name of the file you want to compile:%=%
    g++ -o %file% %file%.cpp
    %file%.exe
    

    save it as cppExecutor.bat

    Also you could use the following commands on Unix (Linux and Mac) OS:

    CC program.cc
    

    If you want to use gcc:

    gcc -o program program.cpp
    

    With the shortcut:

    gcc -o program program.cpp&program.exe
    
    0 讨论(0)
  • 2020-12-22 19:25

    Sure, it's how most compilers got started. GCC is probably the most popular (comes with most flavors of *nix). Syntax is just gcc my_source_code.cpp, or gcc -o my_executable.exe my_source_code.cpp. It gets more complicated, of course, when you have multiple source files (as in implementation; anything #included works automatically as long as GCC can find it).

    MinGW appears to be a version of GCC for Windows, if that's what you're using. I haven't tried it though.

    Pretty sure most IDEs also include a command line interface. I know Visual Studio does, though I have never used it.

    0 讨论(0)
  • 2020-12-22 19:26

    I really don't see what your problem is, the question is rather unspecific. Given Notepad++ I assume you use Windows.

    You have so many options here, from the MinGW (using the GCC tool chain and GNU make) to using a modern MSVC. You can use the WDK (ddkbuild.bat/.cmd or plain build.exe), the Windows SDK (nmake.exe), other tools such as premake and CMake, or msbuild that comes with MSVC and the Windows SDK.

    I mean the compiler names will differ, cl.exe for MSVC and the WDK and Windows SDK, gcc.exe for MinGW, but even from the console it is customary to organize your project in some way. This is what make and friends were invented for after all.

    So to know the command line switches of your particular compiler consult the manual of that very compiler. To find ways to automate your build (i.e. the ability to run a simple command instead of a complex command line), you could sift through the list on Wikipedia or pick one of the tools I mentioned above and go with that.

    Side-note: it isn't necessary to ask people not to mention IDEs. Most professional developers have automated their builds to run from a command line and not from within the IDE (as during the development cycle for example), because there are so many advantages to that approach.

    0 讨论(0)
提交回复
热议问题