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

Ray tracing - constructing ray through pixel

java pixel raytracing

I have an assignment to implement ray tracing in Java.

C++ Builder - MultiThreading

c++ multithreading c++builder vcl tthread

I have a bit of a problem. I teach C++ Builder for the first time this year and I need to create an assignment for my students regarding multi-threading. I would use TIdThreadComponent or TThread. ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.