Using the example from this great answer about the link order in gcc, I tried the following:
echo 'extern int b; int a = b;' > b.cpp
g++ -c b.cpp -o b.o
ar cr libb.a b.o
echo 'int b;' > d.cpp
g++ -c d.cpp -o d.o
ar cr libd.a d.o
echo 'extern int a; int main() { return a; }' > a.cpp
g++ a.cpp -o a1 d.o b.o # Works
g++ a.cpp -o a2 libd.a libb.a # Fails
If the .a
file is simply an archive file containing one or several .o
files, how come linking with the lib*.a
files (in the wrong order, as stated in the original answer) fails, but using the .o
files directly (in the same order) works?