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
Quote:
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?