12

I am trying to use g++ to compile a .cc file, and I need it to link a .o file.

So I tried:

$g++ -o client -I../ipc -L../messages.o client.cc
/usr/bin/ld: error: ../messages.o: can not read directory: Not a directory

And I have tried:

$g++ -o client -I../ipc -l../messages.o client.cc
/usr/bin/ld: error: cannot find -l../messages.pb.o
$$ ls -l ../messages.o

-rw-r--r-- 1 hap497 hap497 227936 2010-02-03 22:32 ../messages.o

Can you please tell me how to link in a .o file?

Thank you.

n179911
  • 19,547
  • 46
  • 120
  • 162

1 Answers1

18
$g++ -o client -I../ipc client.cc ../messages.o
t0mm13b
  • 34,087
  • 8
  • 78
  • 110
  • 7
    This oughtta do it. The OP's problem is that -L specifies a path to search for libraries, and -l specifies a library to find in that path. An object file need only be given as an argument. – Fred Larson Feb 04 '10 at 22:12
  • Thank you. I have tried '$g++ -o client -I../ipc client.cc ../messages.o' But it turns out messages.o need libprotobuf.a library. So I tried 'g++ -o client -I../ipc client.cc -l/usr/local/lib/libprotobuf.a -lpthread ../messages.o; And I still get '/usr/bin/ld: ../messages.o: in function ipc::protobuf_AssignDesc_messages_2eproto():ipc/messages.pb.cc:33: error: undefined reference to 'google::protobuf::DescriptorPool::generated_pool()' I appreciate for more ideas? – n179911 Feb 04 '10 at 23:19
  • 2
    Instead of `-l/usr/local/lib/libprotobuf.a`, try just `-lprotobuf`. `/usr/local/lib` is probably in your default path; if not, add it with `-L/usr/local/lib`. The `-l` option adds the 'lib' to the front and '.a' to the end. – Fred Larson Feb 04 '10 at 23:21
  • Could've used some explanation in your answer, but it's OK, @FredLarson does it perfectly. – corazza Jan 07 '13 at 21:11