Gulbahar
Gulbahar

Reputation: 5547

Makefile to compile c/h?

I need to compile an old application whose tarball only contains *.c and *h, ie. no Makefile. The root directory contains the application, and a sub-directory contains a library the application needs.

My make/Makefile knowledge isn't great, and I was wondering what the easiest way would be to compile this application + library.

Thank you.


Edit: Using this script...

# cat compile.bash
#!/bin/bash

cd mylib
for cfile in *.c; do
   ofile=$(echo "$cfile" | sed 's#.c$#.so#')
   gcc -shared -c "$cfile" -o "$ofile"
done

cd ..
gcc *.c -I mylib -L mylib -mylib -o myapp

... I notice that each *.c file in mylib/ is compiled into a *.so file instead of compiling each into an object file and building a single .so file, and I get tons of warnings and errors, eg.

unzip.c: In function âunzipâ:
unzip.c:991: warning: format not a string literal and no format arguments

gcc: unrecognized option '-mylib'

file_util.c: In function âfile_moveâ:
file_util.c:98: error: âerrnoâ undeclared (first use in this function)

I don't know how to compile the library, and then compile the application without error/warning.

Upvotes: 1

Views: 572

Answers (3)

Idelic
Idelic

Reputation: 15582

No need to use a for loop or generate intermediate object files:

 (cd mylib && gcc -shared -fPIC -o libfoo.so *.c) && \
   gcc -Imylib -o app *.c mylib/libfoo.so

Upvotes: 1

John Carter
John Carter

Reputation: 55369

The easiest is probably to get an IDE to do the build for you. Netbeans for one will create a Makefile so you can then build the project independently of the IDE.

Upvotes: 0

phihag
phihag

Reputation: 288280

Compile the library:

cd libfoo
for cfile in *.c; do
   ofile=$(echo "$cfile" | sed 's#.c$#.so#')
   gcc -shared -c "$cfile" -o "$ofile"
done

After this, you should have a libfoo.so file in libfoo/. Then, compile the program (Don't forget to cd back):

gcc *.c -I libfoo -L libfoo -lfoo -o application

Upvotes: 1

Related Questions