Help building/linking to static library

This is a discussion on Help building/linking to static library within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Salem > U __ZN5Timer5startEv (in main) > T __ZN5Timer5startEv (in lib) Well that's exactly what I expected ...

  1. #16
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Quote Originally Posted by Salem View Post
    > U __ZN5Timer5startEv (in main)
    > T __ZN5Timer5startEv (in lib)
    Well that's exactly what I expected to see, but I'm stumped as to why it doesn't work.

    So I grabbed the zip, and tried it on cygwin.

    With a couple of edits to the makefile, it works.

    First I got this
    ar: libUtil.a: No such file or directory
    So I fixed the invocation of ar

    Then I got this
    main.cpp:2:19: Timer.h: No such file or directory
    So I fixed the FLAGS

    Then all was well...
    Code:
    $ make
    g++ -ansi -W -Wall -I. -c Timer.cpp
    g++ -ansi -W -Wall -I. -c main.cpp
    main.cpp:8: warning: unused parameter 'argc'
    main.cpp:8: warning: unused parameter 'argv'
    g++ -ansi -W -Wall -I. -o main -L. main.o -lUtil
    $ ./main
    123
    Time elapsed: 2653000 usecs.
    Time elapsed: 0 usecs.
    Time elapsed: 0 usecs.
    Time elapsed: 0 usecs.
    The makefile now looks like this
    Code:
    $ cat Makefile
    CC = g++
    FLAGS = -ansi -W -Wall -I.
    
    all: libUtil.a main.o
            $(CC) $(FLAGS) -o main -L. main.o -lUtil
    libUtil.a: Timer.o
            @ar crs libUtil.a Timer.o
    Timer.o: Timer.cpp Timer.h
            $(CC) $(FLAGS) -c Timer.cpp
    main.o: main.cpp
            $(CC) $(FLAGS) -c main.cpp
    Just for good measure, I did this as well
    Code:
    $ g++ main.o Timer.o
    $ ./a.exe
    234
    Time elapsed: 1582000 usecs.
    Time elapsed: 0 usecs.
    Time elapsed: 0 usecs.
    Time elapsed: 0 usecs.
    If you just link both objects, does it work?
    It might tell us whether it's a linker problem or a library problem.
    I changed the invocation of ar to what you posted and no dice. It links successfully with Timer.o, but not libUtil.a

  2. #17
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    I just built your second OS X tarball with no problem except that you had #include <Timer.h> with the angle brackets instead of quotes. I suspect you still have a header in /usr/include. My compiler squawked about that so I changed them to double quotes and everything was fine:

    Code:
    whoie$ cat Makefile
    CC = g++
    FLAGS = -ansi -W -Wall
    
    all: libUtil.a main.o
            $(CC) $(FLAGS) -o main -L. -I. main.o -lUtil
    libUtil.a: Timer.o
            @ar rcs -v libUtil.a Timer.o
    Timer.o: Timer.cpp Timer.h
            $(CC) $(FLAGS) -c Timer.cpp
    main.o: main.cpp
            $(CC) $(FLAGS) -c main.cppwhoie$
    whoie$ cat main.cpp
    #include <iostream>
    #include "Timer.h"
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main(){
      char c;
      unsigned long usec = 0;
    
      Timer t; 
    
      for( t.start(); cin.get(c); t.reset() ) {
        usec = t.getMicroSeconds();
        cout << "Time elapsed: " << usec << " usecs." << endl;
      }
    
      return 0;
    }
    whoie$ make all
    g++ -ansi -W -Wall -c Timer.cpp
    a - Timer.o
    g++ -ansi -W -Wall -c main.cpp
    g++ -ansi -W -Wall -o main -L. -I. main.o -lUtil
    whoie$ ./main
    
    Time elapsed: 894230 usecs.
    
    Time elapsed: 524626 usecs.
    
    Time elapsed: 799940 usecs.
    234
    Time elapsed: 1963042 usecs.
    Time elapsed: 1 usecs.
    Time elapsed: 1 usecs.
    Time elapsed: 0 usecs.
    whoie$
    I think you have a librarian issue. What that is....I don't have a good idea. How did you install XCode tools? Perhaps you should take this issue to an apple developer newsgroup or something. They will probably have more insight into this kind of problem. If you find out what it is, please post back. I'd hate to run into this myself someday.

  3. #18
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Quote Originally Posted by whoie View Post
    I just built your second OS X tarball with no problem except that you had #include <Timer.h> with the angle brackets instead of quotes. I suspect you still have a header in /usr/include. My compiler squawked about that so I changed them to double quotes and everything was fine:
    Yeah - I had the library and the headers installed in their respective /usr directories (now /usr/local)

    I think you have a librarian issue. What that is....I don't have a good idea. How did you install XCode tools? Perhaps you should take this issue to an apple developer newsgroup or something. They will probably have more insight into this kind of problem. If you find out what it is, please post back. I'd hate to run into this myself someday.
    I'm not using XCode (except as an editor). All of the compiling/linking is done via command line with gcc/g++

  4. #19
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by Kernel Sanders View Post
    I'm not using XCode (except as an editor). All of the compiling/linking is done via command line with gcc/g++
    Right, but that only comes with the Xcode tools right? It's been awhile now, but I think gcc/g++ for OS X gets bundled with Xcode. Do you remember when you installed it?

  5. #20
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Quote Originally Posted by whoie View Post
    Right, but that only comes with the Xcode tools right? It's been awhile now, but I think gcc/g++ for OS X gets bundled with Xcode. Do you remember when you installed it?
    I didn't know that. I installed about the time I got my Macbook (mid January), but I believe it has updated since

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create a static library from C++ and C code
    By lehe in forum C++ Programming
    Replies: 1
    Last Post: 04-06-2009, 07:28 PM
  2. Using static libraries
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 02-29-2008, 10:40 PM
  3. Replies: 4
    Last Post: 07-06-2006, 02:53 AM
  4. [GLUT] Pointers to class methods
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 02-16-2006, 03:03 PM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 09:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21