Is there any way to compile programs remotely on Linux with Putty and Sublime? I'm running windows and my c++ compiler is different but my programs are graded on how they run when compiled on the server.
I use winscp to copy files to a unix box to compile / run if I am editing on windows. or you could use pscp (part of putty but not graphical)
https://winscp.net/eng/download.php
This may be a long way of doing it, but this is always what I have done when faced with this problem. (This is assuming that your code is saved on your windows machine and you want to compile it on your linux machine)
In the shell, use the touch command to create a new file
$ touch newFile.cpp
Then use nano to open the file and copy and paste your code into the shell.
$ nano newFile.cpp
then just use the command to compile and then execute.
There's probably a faster way to do but it works.
Related
I learn programming in codecademy, and they have a simulator with the console on the right side like this.
When I want to compile and run programs, I type g++ mycpp anothercpp then run in with second command ./a.out
Can I open this console in the actual c++?
You need to install a compiler. You seem to be using Windows. On Windows, it's easy to install clang compiler. Once you do it, you'll be able to do something similar:
I want to compile C++ in VSCode. I followed the walkthrough here
When I run the example program in VSCode terminal, nothing is output into the terminal. I tried in powershell and in cmd using the dropdown in VSCode terminal window.
When I run the .exe from a standalone cmd terminal, it works fine. Could the problem be something about not knowing how to write the output? I'm guessing the program runs but just doesn't know where to output.
I opened VS code before adding mingw to my PATH. I simply had to restart VS code. I really figured each time I opened a new terminal in VS Code, it would.. open a new terminal. Perhaps they do some kind of virtual terminal and it's really all one terminal from launch? Who knows. Either way, restart VS code.
Command Prompt method :
first changer the directory to where file is located.
g++ -o program Menu-driven-program-DAA.cpp&program.exe
Running Cpp program through powershell terminal in vscode:
g++ -o program Menu-driven-program-DAA.cpp
.\program.exe
I've mentioned both the commands in vscode powershell terminal seperately because it throws an error
Note:- try gcc instead of g++ if it doesn't works for me only g++ did.
I encountered the same problem and by pure chance following worked in my case: When I opened VSC from a GIT shell via 'code .', I didn't get any output from a shell within VSC. But when I opened VSC from windows command prompt via 'code .' and executed the same file again and it worked.
Im new about compile code for linux. It's propably Debian 5.0. And I need compile my cpp code for it as ready to run, i mean the other person can easly run program like in Windows, by just clicking on it.
Anybody can help?
I use virtualbox for this. It's easy and convenient. You can run multiple Linux distros and multiple versions of Windows provided you have the proper licenses. You can also run subversion, etc on each virtual machine so that you can sync your changes across all of them when building.
Assuming you want to be able to compile something on Windows and have it work on any Linux machine, that's simply not possible. Debian and Ubuntu both support many architectures, many of which have absolutely no binary compatibility. If you know what type of hardware your friend has you can build a binary targeted to that architecture.
If you want a quick and dirty answer, you can build for i386 since a 64bit machine can probably still run it fine (not guaranteed though).
Once you compile it, you can easily create a shortcut on the Desktop -or add an entry on a menu- to launch your program via a script; something like:
#!/bin/bash
/path/to/your/progam
Save it as launch.sh -for example- and give it ugo+x permissions as such
chmod ugo+x launch.sh
When you create the shortcut, you can associate a icon to your script exactly in the same way you do it in Windows.
UPDATE
If you are sending the compiled program to your friend (let's assume via email). You can simply instruct your friend to launch the terminal window in the same directory where he downloaded your file and run the following:
chmod ugo+x your_program
./your_program
Or you can send him 2 files: one with your program and one with a "launch" script as I described above. Since both files will be downloaded to the same directory, you can change your launch script to:
#!/bin/bash
./your_program
When he clicks on launch.sh, your program will be executed.
I'm a beginner with programming, and I've been doing work in C/C++ in Ubuntu. When I tell something to cin/cout/cerr or printf/scanf or take arguments from the command line, this all happens from the linux terminal in Ubuntu.
Now if I want to run these same programs (very simple programs, beginner-level) and run them in Windows, how do I run them from the Windows command line? A previous course I've taken had us download cygwin to simulate the linux command line in windows, but what if I want to just run the program from the ordinary windows command line? Is that possible, and does it require modification of the software?
You can cross-compile the program for Windows from linux.
On Ubuntu, process is basically this:
sudo apt-get install wine mingw32 mingw32-binutils mingw32-runtime
...
i586-mingw32msvc-g++ -o myProgram.exe myProgram.cpp
Easy, right? Google for "ubuntu cross-compile windows," there's a ton of information out there.
It's exactly the same. You run cmd and write the command (almost) exactly as you would in Linux.
For example, if you build your program to program, you would run it in Linux like this:
./program --option1 -o2 file1 file2
And in Windows, first you have to make the output have a .exe suffix and then in cmd you would write:
program.exe --option1 -o2 file1 file2
Basically saying, cmd is Windows' terminal. It's nowhere near as good as the Linux terminal, but that would be all you get without installing additional software.
cin/cout/cerr and printf/scanf/fprintf(stderr, ...) use the standard C preopened files stdin, stdout and stderr which are defined both in Linux and Windows. Once you run the application from Windows' terminal (cmd), you see the input/output exactly as you would in the Linux terminal. I/O redirection is also very similar.
cin and cout, and printf and scanf, work much the same in Windows as they do in Linux. (I'm pretty sure cerr does too, but that one i'm not 100% sure about. At the very least, though, it's there and works.) The biggest difference is that Windows typically won't expand wildcards (stuff like *.txt) before running your program; you have to do that yourself in most cases.
Basically, as long as the app doesn't use anything specific to Linux or GCC, you could just recompile it on the target machine using whatever compiler you like to test.
If you don't want to recompile...well...good luck with that. Even Cygwin won't run native Linux binaries. You'd need a virtual machine with Linux on it.
Well, if you program is portable and not using any features specific to Linux, you would have to compile it from source on Windows to make it work on Windows.
You would need the GCC tool-chain for windows to do that, which you can get from the TDM-GCC homepage. Its MinGW internally and the installer allows you to choose the features you want to install as well as the target directory for installation. It also adds itself to Windows path so that the compiler commands are available from the shell prompt.
I have to do the cross compilation regularly and it works without any issues for me. There is one change which you must make if your project is using Makefiles. For the target binary, such as <target>.out in linux, you would have to edit your Makefile and rename it to <target>.exe so that it runs on the command line. If you are not using Makefiles and just doing gcc <file.c>, the a.exe is produced by default (similar to a.out in Linux).
Say you have this program code you want to run on UNIX and Windows:
#include <stdio.h>
int main()
{
printf("Hi\n");
return 0;
}
When you type a command in a UNIX shell it will be something like this.
/usr/home/bobby# gcc main.c
/usr/home/bobby# ./a.out
Hi
/usr/home/bobby#
On Windows you'd have to first choose your development environment/compiler. Without going to something like Cygwin, you could install the Windows SDK or Visual studio (although if the latter you might just want to develop in the GUI).
Start -> Run -> cmd /k ""C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86
C:\Windows\system32>cd c:\bobby
C:\bobby>cl main.c
C:\bobby>main.exe
Hi
C:\bobby>
When a C program is compiled into an executable this is done in a system dependent way. On Ubuntu the ELF format is used and on Windows we have PE.
When you start a process the ELF or PE is read giving instructions/map on how to allocate memory and where to put various pieces of the process in a virtual memory table. Further it links up to dynamically loaded libraries, already in physical memory, that it share with other processes which is using the same libraries. Or if the dynamic libraries is not present load them. (Linux .so, windows .dll). If it has static libraries these are allocated and linked in (Linux .a, Windows .lib). - Very simplified.
Memory restrictions etc are inherited from previous process.
Environment variables are put into the running environment for the process. This being paths, arguments, etc. Then main() is added to the stack and called.
Now everything happening before main is called and how linkage etc are resolved, and so many other things, depends on the system. This is why one simply can't run an executable compiled on Linux on Windows.
Using cygwin one is simply creating a virtual environment where those linkages etc are the same and would work. One create an ELF environment.
To get it linked for native Windows command line one would have to compile for Windows. On that matter I see there is lots of answers already.
The ELF and PE, as on different systems, also have different ways of handling environment variables etc. What these are etc. So i.e. file expansion is handled differently. However both running processes has the default streams like stderr, stdout and stdin. But below your code in C they are not the same.
It is like driving a diesel vs a petrol car. Much is the same but under the hood quite a few things is different.
Be aware that i.e. signals are handled different on Windows.
How do I run a command like interface on windows and use the g++ and ./a.out
I am a beginning programmer used to using putty/ssh to write (nano), compile (g++ command), and run (./a.out) c++ programs.
Our class has now switched to netbeans, but our latest assignment requires us to use the ./a.out <datafile.txt-like command.
Or is the input redirection style ./a.out <data.txt unique to unix and cannot be done in windows?
edit: the < input redirection marks made my post mostly unreadable. Sorry about that
2nd edit: There is actually a terminal built into netbeans that VERY conveniently starts in your project directory. open it in netbeans by selecting Window -> output -> Terminal
Cygwin is a collection of tools which provide a Linux look and feel environment for Windows.
start,
run,
cmd,
cd directory,
g++ a.cpp,
a.exe
Windows includes . in $PATH, so the ./ at the beginning is superfluous (and wrong, since it would be .\). The rest is the same.