unresolved names linking against library but passing obj files directly works fine?

Consider the following g++ invocation (Apple GCC 4.2.1, running on OSX 10.6.5) which works just fine:

g++ -c util/warthog_cfg.cpp -o  util/obj/warthog_cfg.o -DOS_MAC -O0 -Wall -Wno-sign-compare -ansi -fmessage-length=0 -m32
g++ main.cpp -o main -I./util util/obj/warthog_cfg.o -m32

Observe that I passed warthog_cfg.o (a required object file) as an argument in the second line. Now I want to put warthog_cfg.o into a library file and link against that. I run the following:

g++ -c util/warthog_cfg.cpp -o  util/obj/warthog_cfg.o -DOS_MAC -O0 -Wall -Wno-sign-compare -ansi -fmessage-length=0 -m32
ar -crs lib/libutil.a  util/obj/warthog_cfg.o
g++ main.cpp -o ./bin/main -DOS_MAC -O0 -Wall -Wno-sign-compare -ansi -fmessage-length=0 -m32 -L./lib -framework CoreServices -I./TransitC -I./cpd2012/ -I./util -lutil -lutil
Undefined symbols:
  "warthog::util::warthog_cfg::print(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
      _main in ccI5ycWY.o
  "warthog::util::warthog_cfg::warthog_cfg()", referenced from:
      _main in ccI5ycWY.o
  "warthog::util::warthog_cfg::parse_args(int, char**)", referenced from:
      _main in ccI5ycWY.o
  "warthog::util::warthog_cfg::~warthog_cfg()", referenced from:
      _main in ccI5ycWY.o
      _main in ccI5ycWY.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [main] Error 1

The calls appear identical. I thought perhaps the problem is with ar not adding the .o file to the archive. However, otool seems to indicate otherwise. File sizes seem about right too.

otool -I lib/libutil.a 
Archive : lib/libutil.a

-rw-r--r--  1 foo  staff  5908 10 Sep 18:39 warthog_cfg.o
-rw-r--r--  1 foo  staff  6800 10 Sep 18:35 lib/libutil.a

What's going on here?


I'm not actually seeing you linking the library to the main program.

I don't generally use -L for this (this sets a search path, but I know where the library is). Just add your lib/libutil.a on the linker invocation just like you would another object file. If you want to keep the "search my lib" scheme, add -lutil and hope there's no other util library getting in the way.

Need Your Help

Dynamic linking in C++ not working

c++ inheritance dynamic linker overriding

I have a problem with a small program. I hope you can open my eyes.

I resized uploaded images but it still have big size

c# asp.net

I used to use this code to resize the uploaded image, but when I compared the generated image with other images in other websites, I found that It have large size !!