JumpingRock
JumpingRock

Reputation: 59

Trouble with implementing a basic Makefile in c

Okay so I need to make a basic Makefile for a program I wrote. Here are the files:

list.c
hash.c
order_book.c
libdefault_hash.a //provided already so I do not need to create.

I need to create libraries for list.c and hash.c so that orderbook can use them when it compiles. So this is what I currently have in Makefile:

all: orderbook

orderbook: orderbook.c liblist.a libhash.a
    gcc -std=c99 -o orderbook order_book.c list.c -L. -llist -lhash -libdefault_hash

liblist.a: list.c
    gcc -std=c99 -c list.c
    ar rcu liblist.a list.o

libhash.a: hash.c
    gcc -std=c99 -c hash.c
    ar rcu libhash.a hash.o

My understanding of how makefiles work is very small but here is my thought process,

  1. all: orderbook will mean that orderbook: will run.

  2. orderbook.c will then compile, then the code will compile the libraries.

  3. Once the libraries are compiled it will run:

    gcc -std=c99 -o orderbook order_book.c list.c -L. -llist -lhash -libdefault_hash

And the result should be a simple program file named orderbook, but the terminal prints out:

$ make
gcc -std=c99 -o orderbook order_book.c list.c hash.c -L. -llist -lhash -libdefault_hash
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/../../../../x86_64-pc-linux-gnu/bin/ld: skipping incompatible ./liblist.a when searching for -llist
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -llist
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -libdefault_hash
collect2: ld returned 1 exit status
make: *** [orderbook] Error 1
$

Any help/guidance is much appreciated.

Upvotes: 2

Views: 185

Answers (1)

Beta
Beta

Reputation: 99084

Let's take this in small steps. First, here's a sequence of commands that looks like what you have in mind:

gcc -std=c99 -c list.c -o list.o
ar rcu liblist.a list.o
gcc -std=c99 -c hash.c -o hash.o
ar rcu libhash.a hash.o
gcc -std=c99 -o orderbook order_book.c -L. -llist -lhash -libdefault_hash

Try these commands without Make, and see which ones work (are you sure "rcu" shouldn't be "-rcu"?). Tell us the results either by commenting on this answer or editing your question. Once any of these commands works, we can start writing the makefile.

Upvotes: 1

Related Questions