2

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?

4
  • 1
    Having such script is fine for projects with only small source files, and only 2-4 of them. As soon as you begin hacking a slightly bigger project, any build system pays out as for the faster compile times. There is not only make, which indeed has an intriguing syntax, but many others as well. That's your search term. Commented Feb 18, 2014 at 9:20
  • 1
    Alternatively, try scons. Commented Feb 18, 2014 at 9:20
  • @cup: That's what I wrote, only more generally. Anyways, currently, I prefer scons, too. QMake is pretty simple for small projects, cmake can be nice, too (but it can be pretty unnice, also). However, look for "build system" first. Commented Feb 18, 2014 at 9:21
  • Isn't the second command missing an input file? Commented Feb 18, 2014 at 9:21

2 Answers 2

1
  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.

Sign up to request clarification or add additional context in comments.

1 Comment

considering your advise, how could makefile add advantage over simple script file?
1

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?

3 Comments

last line of my answer :D
also #!/bin/bash at the top of script ;)
@jparthj try to run it as root...or may be the problem relates to /sbin/ and such issues...may be flags in compiling should run from another path rather than #!/bin/bash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.