Reputation: 1559
I have these files : help.cpp help.h help.o main.cpp and I want to create a static lib.
help.cpp
#include <iostream>
#include "help.h"
using namespace std;
void hello()
{
cout << "hi" << endl;
}
help.h
#ifndef HELP_H
#define HELP_H
void hello();
#endif
main.cpp
#include <iostream>
#include "help.h"
using namespace std;
int main()
{
hello();
return 0;
}
To create a static lib :
ar rcs libST.a help.o
g++ main.cpp -L . -l ST #to create my binary
I ran it , it printed out
hi
I thought why not to do like this: g++ main.cpp libST.a
and it worked as well.
Please tell me, what is the difference between command 2 and 3?
Upvotes: 1
Views: 94
Reputation: 9642
The behaviour of both will be (almost) identical in this case, but there are a few differences.
g++ main.cpp -L . -l ST
This compiles main.cpp and attempts to link it against something called libST
. This will search the whole linker search path (typically /usr/local/lib, /lib and /usr/lib), including the current directory (since you specified it with -L
). Note that this may link against either a shared or a static library, as long as it's called libST.a or libST.so.
If libST didn't exist in the current folder, your linker would go searching in all other folders that it knew about too.
g++ main.cpp libST.a
This does the same, but you're explicitly giving a static library to link against. This won't search, and it won't possibly link against a shared library, it'll just pull the object files out of the archive and use those when linking.
Upvotes: 2