Software to run c programs in windows

Run C++ in command prompt — Windows

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 least that works for Java and Python. I’ve tried to get my head around how to do that with C++, and haven’t been able to find anything good. Is there any compiler (like Java’s JDK) that I can stick into my path and use the C++ equivalent of javac and java to run and compile my code from CMD?

Note: please don’t post answers and comments about how IDEs are better — I know they are. I’m just used to doing it the old way 😀

11 Answers 11

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.

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.

Steps to perform the task:

First, download and install the compiler.

Then, type the C/C++ program and save it.

Then, open the command line and change directory to the particular one where the source file is stored, using cd like so:

Then, to compile, type in the command prompt:

Finally, to run the code, type:

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

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.

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:

save it as cppExecutor.bat

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

If you want to use gcc :

With the shortcut:

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 #include d 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.

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.

Читайте также:  Vnc server linux centos

How can I run an EXE program from a Windows Service using C#?

How can I run an EXE program from a Windows Service using C#?

This is my code:

When I run this service, the application is not starting.
What’s wrong with my code?

9 Answers 9

This will never work, at least not under Windows Vista or later. The key problem is that you’re trying to execute this from within a Windows Service, rather than a standard Windows application. The code you’ve shown will work perfectly in a Windows Forms, WPF, or Console application, but it won’t work at all in a Windows Service.

Windows Services cannot start additional applications because they are not running in the context of any particular user. Unlike regular Windows applications, services are now run in an isolated session and are prohibited from interacting with a user or the desktop. This leaves no place for the application to be run.

More information is available in the answers to these related questions:

The best solution to your problem, as you’ve probably figured out by now, is to create a standard Windows application instead of a service. These are designed to be run by a particular user and are associated with that user’s desktop. This way, you can run additional applications whenever you want, using the code that you’ve already shown.

Another possible solution, assuming that your Console application does not require an interface or output of any sort, is to instruct the process not to create a window. This will prevent Windows from blocking the creation of your process, because it will no longer request that a Console window be created. You can find the relevant code in this answer to a related question.

i have tried this article Code Project, it is working fine for me. I have used the code too. article is excellent in explanation with screenshot.

I am adding necessary explanation to this scenario

You have just booted up your computer and are about to log on. When you log on, the system assigns you a unique Session ID. In Windows Vista, the first User to log on to the computer is assigned a Session ID of 1 by the OS. The next User to log on will be assigned a Session ID of 2. And so on and so forth. You can view the Session ID assigned to each logged on User from the Users tab in Task Manager.

But your windows service is brought under session ID of 0. This session is isolated from other sessions. This ultimately prevent the windows service to invoke the application running under user session’s like 1 or 2.

In order to invoke the application from windows service you need to copy the control from winlogon.exe which acts as present logged user as shown in below screenshot.

you can use from windows task scheduler for this purpose, there are many libraries like TaskScheduler that help you.

for example consider we want to scheduling a task that will executes once five seconds later:

notepad.exe will be executed five seconds later.

for details and more information please go to wiki

if you know which class and method in that assembly you need, you can invoke it yourself like this:

Top answer with most upvotes isn’t wrong but still the opposite of what I would post. I say it will totally work to start an exe file and you can do this in the context of any user. Logically you just can’t have any user interface or ask for user input.

Here is my advice:

  1. Create a simple Console Application that does what your service should do right on start without user interaction. I really recommend not using the Windows Service project type especially because you (currently) can’t using .NET Core.
  2. Add code to start your exe you want to call from service

Example to start e.g. plink.exe. You could even listen to the output:

  1. Use NSSM (Non-Sucking Service Manager) to register that Console Application as service. NSSM can be controlled via command line and can show an UI to configure the service or you configure it via command line. You can run the service in the context of any user if you know the login data of that user.

I took LocalSystem account which is default and more than Local Service. It worked fine without having to enter login information of a specific user. I didn’t even tick the checkbox «Allow service to interact with desktop» which you could if you need higher permissions.

Читайте также:  256 символов содержит кодировка windows

Lastly I just want to say how funny it is that the top answer says quite the opposite of my answer and still both of us are right it’s just how you interpret the question :-D. If you now say but you can’t with the windows service project type — You CAN but I had this before and installation was sketchy and it was maybe kind of an unintentional hack until I found NSSM.

You can execute an .exe from a Windows service very well in Windows XP. I have done it myself in the past.

You need to make sure you had checked the option «Allow to interact with the Desktop» in the Windows service properties. If that is not done, it will not execute.

I need to check in Windows 7 or Vista as these versions requires additional security privileges so it may throw an error, but I am quite sure it can be achieved either directly or indirectly. For XP I am certain as I had done it myself.

First, we are going to create a Windows Service that runs under the System account. This service will be responsible for spawning an interactive process within the currently active User’s Session. This newly created process will display a UI and run with full admin rights. When the first User logs on to the computer, this service will be started and will be running in Session0; however the process that this service spawns will be running on the desktop of the currently logged on User. We will refer to this service as the LoaderService.

Next, the winlogon.exe process is responsible for managing User login and logout procedures. We know that every User who logs on to the computer will have a unique Session ID and a corresponding winlogon.exe process associated with their Session. Now, we mentioned above, the LoaderService runs under the System account. We also confirmed that each winlogon.exe process on the computer runs under the System account. Because the System account is the owner of both the LoaderService and the winlogon.exe processes, our LoaderService can copy the access token (and Session ID) of the winlogon.exe process and then call the Win32 API function CreateProcessAsUser to launch a process into the currently active Session of the logged on User. Since the Session ID located within the access token of the copied winlogon.exe process is greater than 0, we can launch an interactive process using that token.

I think You are copying the .exe to different location. This might be the problem I guess. When you copy the exe, you are not copying its dependencies.

So, what you can do is, put all dependent dlls in GAC so that any .net exe can access it

Else, do not copy the exe to new location. Just create a environment variable and call the exe in your c#. Since the path is defined in environment variables, the exe is can be accessed by your c# program.

previously I had some kind of same issue in my c#.net 3.5 project in which I was trying to run a .exe file from c#.net code and that exe was nothing but the another project exe(where i added few supporting dlls for my functionality) and those dlls methods I was using in my exe application. At last I resolved this by creating that application as a separate project to the same solution and i added that project output to my deployment project. According to this scenario I answered, If its not what he wants then I am extremely sorry.

www.makeuseof.com

Follow MUO

How can I run a C++ program in Windows 7?

How to run C++ program in Windows 7 32-bit?

Please help. What to do next.

I’m not sure what a cpp file is. Can you elaborate?

In any case, I would recommend you to ask a new question. Please provide a thorough description of what you are trying to do, where this file is from, and where you downloaded it from.

I’m afraid I do not understand your question. However, if you are able to describe in detail what you are trying to do or what your issue is, then I recommend you to ask a new question. This way your question will receive a great exposure as it will be featured on the frontpage of Answers.

Читайте также:  Install windows in sccm

like Ishita above, I recommend you to ask a new question.

I use vmware and a copy of windows xp to run a virtual xp machine on my vista 64 machine. Compiling a 32 bit program be it vb, c++, c, assembly can be a real hassle on a 64 bit machine. Using Vmware will help, but I would suggest learning to use a new compiler that will work on a 64 bit machine and has a strong support group like eclipse.

1. Install the software DOSBox ver 0.73 : download here

2. Create a folder,for example „Turbo“ (c:Turbo)

3. Download and extract TC into the Turbo folder (c:Turbo): download here

4. Run the DOSBox 0.73 from the icon located on the desktop:

5. Type the following commands at the command prompt [Z]: mount d c:Turbo [The folder TC is present inside the folder Turbo]

Now you should get a message which says: Drive D is mounted as a local directory c:Turbo

6. Type d: to shift to d:

7. Next follow the commands below:

tc or tc.exe [This starts you the Turbo C++ 3.0]

8. In the Turbo C++ goto Options>Directories> Change the source of TC to the source directory [D] ( i.e. virtual D: refers to original c:Turbo . So make the path change to something like D:TCinclude and D:TClib respectively )

You can save yourself some time by having DOSBox automatically mount your folders and start TurboC++:

For DOSBox versions older then 0.73 browse into program installation folder and open the dosbox.conf file in any text editor. For version 0.73 go to Start Menu and click on “Configuration” and then “Edit Configuration“. Then scroll down to the very end, and add the lines which you want to automatically execute when DOSBox starts.

Automatically mount and start Turbo C++3.0 in DOSBox ver 0.73:

Those commands will be executed automatically when DOSBox starts!

Full screen: Alt and Enter

When you exit from the DosBox [precisely when u unmount the virtual drive where Turbo C++ 3.0 has been mounted] all the files you have saved or made changes in Turbo C++ 3.0 will be copied into the source directory(The directory which contains TC folder)

Don’t use shortcut keys to perform operations in TC because they might be a shortcut key for DOSBOX also . Eg : Ctrl+F9 will exit DOSBOX rather running the code .

To use Turbo C++ IDE in windows 7 or in Vista; For those who wants to learn C/C++ just uninstall your «Graphics driver «

For that Computers->properties->device manager->Display Adapters->graphics driver version u have uninstall that. And after log off Run your «C:\TC\BIN\TC.exe».it will open in full screen.

Just Load the files in C:TC ( Hard disk c: drive)

It is the default location for TC.

Enter these lines in the Dos Box options file. (Start menu – Dos box – Options – DOSBox 0.74 Options) That opens in notepad.

Scroll down in the notepad file – After these lines

# Lines in this section will be run at startup.

# You can put your MOUNT lines here.”

Alt + Enter for fullscreen, Alt + x to exit TC. Type exit to close Dos Box.

Click on Run in the menu – Don’t use Ctrl + F9 to run program. (It will close Dos Box). All directories are set by default location.

Software to run C programs on Windows 7

Copy the source code to Visual C++ ( Comes with Visual Studios 6.0 , 2005 , 2008 , 2010 etc. ) and compile . The exe created would be compatible with your OS .

Windows is not like linux which has C/C++ compiler provided with the OS. You need to get your own.

Try Visual Studio Express. It’s free, it’s from MSFT and it’s good. You’ll be able to compile and create executable files.

In the File menu, select Create a new project. Depending on what your CPP file is about, a Win 32 -> Console Application is the simpliest you can do.

Add your CPP file and hit the build command. You’ll probably have tweaking with include files to get it to compile

both of which uses the MinGW 32-bit development environment environment for building applications.

Both of these tools are a little bit old, and neither is great for developing Windows applications, but for learning C/C++ they should do just fine.

Оцените статью