| How to compile and execute C/C++ programs on web server |
Print this page
|
If you have appropriate privileges on web server, you can upload to it C/C++ source code, compile it and execute. To do that you need shell access to server. If you don't have access to shell at web server — you can use Web Console.
Below you can find simple instructions to compile and execute C/C++ programs on web server:
- First of all you should make sure that you have access to GCC, G++ or CC compiler. For that just execute following shell command:
For GCC:
shell> gcc --help
For G++:
shell> g++ --help
For CC:
shell> cc --help
- If you see output like that:
For GCC:
Usage: gcc [options] file...
For G++:
Usage: g++ [options] file...
For CC:
Usage: cc [options] file...
It means, that you can use compiler.
- In case of error, may be full path command can helps:
shell> /usr/bin/[gcc, g++ or cc] --help
shell> /usr/sbin/[gcc, g++ or cc] --help
shell> /usr/local/bin/[gcc, g++ or cc] --help
shell> /usr/local/sbin/[gcc, g++ or cc] --help
shell> /bin/[gcc, g++ or cc] --help
shell> /sbin/[gcc, g++ or cc] --help
- The next step is preparing source code for compilation. You can upload some C/C++ source code file to web server. Also you can write simple "Hello word" example program, to do that just execute following shell command:
shell> echo 'main () { printf("Hello World\n"); }' > test-bin.c
That comand will put at file "test-bin.c" source code of simple "Hello word" program, that will print at screen string "Hello word".
Example you can see at following screenshot:
 Screenshot: Writing simple "Hello word" example C program
- To compile source code, just execute following shell command:
For GCC:
shell> gcc test-bin.c -o test-bin
For G++:
shell> g++ test-bin.c -o test-bin
For CC:
shell> cc test-bin.c -o test-bin
That comand will compile file "test-bin.c" and will write binary executable at file "test-bin".
- Finally — execute binary:
For our simple "Hello word" example program you should see following output:
Example you can see at following screenshot:
 Screenshot: Compiling and executing simple "Hello word" example C program
To find more information about GCC, G++ and CC command-line parameters just execute following shell commands:
shell> gcc --help
shell> g++ --help
shell> cc --help
Or if you want to read manuals — execute following shell commands:
shell> man gcc
shell> man g++
shell> man cc
Also, for more information about how to compile C/C++ source code using command shell, you can read that article.
/ Any republication of website content required direct track back link /
|