jparthj
jparthj

Reputation: 1636

Compile C/C++ program via simple script

I have multiple c source files. I would like to compile them via script. I know i can do this via makefile, but i prefer simple script instead.

I feel makefiles are too complex, so i'm looking for simple script to compile mulitple files and then create a shared library in Linux(GNU).

I know how to compile/build shared library using terminal so just want to run my command from simple script.

gcc -c -Wall tbl0.c tbl1.c tbl2.c 

gcc -shared -Wall -o libtbl.so tbl.c -I.
    -Wl,-z,defs -L. -lpthread -lm -ldl

Any help?

Upvotes: 2

Views: 3915

Answers (2)

MLSC
MLSC

Reputation: 5972

Firstly:

chmod +x script.sh

then invoke it by saying:

./script.sh

your script should be:

#!/bin/bash
gcc -c -Wall tbl0.c tbl1.c tbl2.c 

gcc -shared -Wall -o libtbl.so tbl.c -I. -Wl,-z,defs -L. -lpthread -lm -ldl

Don't you forget input file in the second line?

Upvotes: 1

Aman
Aman

Reputation: 8985

  1. Just copy paste your commands in a file, say x.sh.

  2. Type chmod +x x.sh in the directory where x.sh resides.

  3. Run x.sh by typing ./x.sh

apollo:~/test$ cat > x.sh   
gcc -c -Wall tbl0.c tbl1.c tbl2.c    

gcc -shared -Wall -o libtbl.so -I.    
    -Wl,-z,defs -L. -lpthread -lm -ldl  

apollo:~/test$ chmod +x x.sh   

apollo:~/test$ ./x.sh  #run

I will suggest that you stick with a Makefile. They may look complex initially, but are useful in the long run.

Upvotes: 1

Related Questions