Thread: Question about making a C++ archive (.a file)

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Question about making a C++ archive (.a file)

    I am confused by how to make a archive. Here is the code I use:

    I first have a vector.h file
    Code:
    //vector.h
    namespace TEST{
      int add(int x, int y);
    }
    Also a vector.cc file contains its definition.
    Code:
    //vector.cc
    #include <iostream>
    #include "vector.h"
    
    int TEST::add(int x, int y){
      return (x+y);
    }
    
    int main(){
        std::cout<<TEST::add(1,1)<<std::endl;
        return 0;
    }

    I use the following command to make a libvec.a
    g++ -c vector.cc
    ar rcs libvec.a vector.o
    Then here is the program uses libvec.a
    Code:
    //main.cc
    #include <iostream>
    #include "vector.h"
    
    using namespace std;
    
    int main(){
        cout<<TEST::add(2,3)<<endl;
        return 0;
    }
    So far so good. Finally I use the following command to make main program using libvec.a
    Code:
    g++ -o main main.cc  libvec.a
    It reports:
    libvec.a(vector.o)(.text+0xc): In function `main':
    : multiple definition of `main'
    /tmp/ccRdvIKb.o(.text+0x0): first defined here
    collect2: ld returned 1 exit status


    I am confused. It seems that I can't define main() function for an archive. But I have seen many code that has main() function but also is compiled for an archive. Anybody has a clue what is wrong?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to remove your "main" from "vector.cc", since the linker will use all the code in vector.o from libvec.a when it links the executable, it will include the second main, which is now "competing" with your main.cc's main() function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thanks. One more question:
    To make an archive (aka static library), must I use
    g++ -c vector.cc
    ar rcs libvec.a vector.o
    ?
    Can I use
    g++ -o libvec.a vector.cc
    ?
    Last edited by meili100; 08-11-2008 at 12:30 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I believe the way to compile, assuming you have an .A file, is

    g++ -I path/to/lib othercode.cc -o executable

    In this way, you can keep the lib wherever you store your personal libraries and use #include <lib.hpp>

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Assuming the use of g++ implies a Unix/Linux variant: Creating Unix Libraries.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    g++ -c vector.cc
    ar rcs libvec.a vector.o
    is the right way to create a library. Of course, you don't want to use c every time you add a object file to the library [the whole point is that you can have a collection of loosely related object files in one library - and then the linker can "borrow" any object that it needs, but it doesn't have to use all of the library].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about file streams.
    By Lawn Gnomusrex in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2008, 06:05 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM