shell command

I am a little new to ubuntu & linux, How do I make a shell command compile my source code I surfed and surfed and surfed but the net has no flies (confusing-ish?). So all I want it to do is get the directory then compile "main.cpp" to main I found:( in c++ I know it might look like this:
1
2
3
string curDir = pwd //print working directory
cd pwd
g++ main.cpp -o main
Last edited on
nvm I feel stupid now
I don't get it. You want to make a shell script to compile your code, or you just want to compile from the command line? If the latter; you just type gxx [options if any] [-o outfile] infiles [more infiles if necessary]. Everything in square brackets is optional; if you don't specifiy -o outfile you get a.out; which is an old executable format.

Oh, and xx is either cc or ++.
Last edited on
If you want shell script. Then check out below

scriptname
1
2
3
#!/bin/ksh
removeExtension=`echo $1 | cut -d '.' -f1`
g++ $1 -o $removeExtension


Then chmod 700 scriptname

Finally, in your .profile add the path for the location of your script. Then just create an alias for the script name and you'll be good to go.
:-)
you can use make.
if your file is called main.cpp (or main.c)
make main will do it.

note only for simple progs, not if you have multiple source files.
(headers are fine)
and no you don't need a makefile you use the implicit rules.



Or if you want to be silly!!!

clever.cpp
1
2
3
4
5
6
7
8
9
#define COMPILE \
make -f /dev/null ${0%.*} && exec ./clever || exit

#include <iostream>

int main(void)
{
    std::cout <<  "hello\n";
}

then...
1
2
3
4
5
6
7
$ bash clever.cpp
c++ -O2 -fno-strict-aliasing -pipe  clever.cpp  -o clever
hello
$ bash clever.cpp
`clever' is up to date.
hello
 


etc etc the silliness is endless



Topic archived. No new replies allowed.