0

I'm trying to compile a program on Ubuntu 12.04 on VirtualBox, and I get the following error:

daniel@daniel-VirtualBox:~/Documents/Redes/t1$ make
gcc -g    -c -o bwc.o bwc.c
gcc -g    -c -o jsocket6.4.o jsocket6.4.c
gcc -g    -c -o Dataclient-seqn.o Dataclient-seqn.c
gcc -g    -c -o bufbox.o bufbox.c
gcc -g  bwc.o jsocket6.4.o Dataclient-seqn.o bufbox.o -o bwc-orig -lpthread  
Dataclient-seqn.o: In function `Now':
/home/daniel/Documents/Redes/t1/Dataclient-seqn.c:68: undefined reference to `clock_gettime'
collect2: ld returned 1 exit status
make: *** [bwc-orig] Error 1

I found out I had to add -lrt, but I'm not sure how to do it, here's my makefile:

CC=gcc
CFLAGS=-g # -m32

BIN=bwc-orig bwc bwc-tcp bws-tcp

all: $(BIN)

bwc-orig: bwc.o jsocket6.4.o Dataclient-seqn.o jsocket6.4.h bufbox.o 
    $(CC) $(CFLAGS) bwc.o jsocket6.4.o Dataclient-seqn.o bufbox.o -o $@ -lpthread  

bwc: bwc.o jsocket6.4.o Dataclient-bigseq.o jsocket6.4.h bufbox.o 
    $(CC) $(CFLAGS) bwc.o jsocket6.4.o Dataclient-bigseq.o bufbox.o -o $@ -lpthread 

bwc-tcp: bwc.o jsocket6.4.o Data-tcp.o jsocket6.4.h
    $(CC) $(CFLAGS) bwc.o jsocket6.4.o Data-tcp.o -o $@ -lpthread 

bws-tcp: bws.o jsocket6.4.o Data-tcp.o jsocket6.4.h
    $(CC) $(CFLAGS) bws.o jsocket6.4.o Data-tcp.o -o $@ -lpthread 

cleanall: 
    rm -f $(BIN) *.o

I modified the makefile, to add the -lrt flag:

CC=gcc
CFLAGS=-g -lrt # -m32

BIN=bwc-orig bwc bwc-tcp bws-tcp

all: $(BIN)

bwc-orig: bwc.o jsocket6.4.o Dataclient-seqn.o jsocket6.4.h bufbox.o 
    $(CC) $(CFLAGS) bwc.o jsocket6.4.o Dataclient-seqn.o bufbox.o -o $@ -lpthread  

bwc: bwc.o jsocket6.4.o Dataclient-bigseq.o jsocket6.4.h bufbox.o 
    $(CC) $(CFLAGS) bwc.o jsocket6.4.o Dataclient-bigseq.o bufbox.o -o $@ -lpthread 

bwc-tcp: bwc.o jsocket6.4.o Data-tcp.o jsocket6.4.h
    $(CC) $(CFLAGS) bwc.o jsocket6.4.o Data-tcp.o -o $@ -lpthread 

bws-tcp: bws.o jsocket6.4.o Data-tcp.o jsocket6.4.h
    $(CC) $(CFLAGS) bws.o jsocket6.4.o Data-tcp.o -o $@ -lpthread 

cleanall: 
    rm -f $(BIN) *.o

But I get the following output (it's pretty much the same, except I see the -lrt flag at the beggining):

daniel@daniel-VirtualBox:~/Documents/Redes/t1$ make
gcc -g -lrt    -c -o bwc.o bwc.c
gcc -g -lrt    -c -o jsocket6.4.o jsocket6.4.c
gcc -g -lrt    -c -o Dataclient-seqn.o Dataclient-seqn.c
gcc -g -lrt    -c -o bufbox.o bufbox.c
gcc -g -lrt  bwc.o jsocket6.4.o Dataclient-seqn.o bufbox.o -o bwc-orig -lpthread

Dataclient-seqn.o: In function `Now':
/home/daniel/Documents/Redes/t1/Dataclient-seqn.c:76: undefined reference to `clock_gettime'
collect2: ld returned 1 exit status
make: *** [bwc-orig] Error 1

I don't know how I have to add the -lrt flag. I actually don't know so much about makefiles, and don't know what to do anymore.

Thanks.

EDIT: Nevermind, I just solved it. After running make, and getting that last output, I just decided to run

gcc -g bwc.o jsocket6.4.o Dataclient-seqn.o bufbox.o -o bwc-orig -lpthread -lrt

And it worked, because the problem was I had to add -lrt at the end.

Thanks anyway.

2
  • 1
    When you say "making CFLAGS=-g -lrt", you mean exactly what? If you changed the makefile, show the version including what you tried, exactly the way you tried it, and your output in that circumstance. If you changed the make command line, show the command you ran, and -- again -- your output. Commented May 14, 2015 at 16:34
  • Add your own solution as an answer, not an edit to the question. (This is actually better for you, as it means you can get reputation from upvotes to the answer separately). Commented May 25, 2015 at 17:09

1 Answer 1

1

Library flags need to go last. Everywhere you see -lpthread also add -lrt (or maybe refactor to a variable so you only need to change one place if you need to add still more libraries).

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

Comments

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.