Resolver order in library linking
**********************************************************************************
Disclaimer: The information shared on this blog is for general informational purposes only and reflects personal views or experiences in technology. While I strive for accuracy, I cannot guarantee that all content is current or error-free. I am not liable for any loss or damage resulting from the use of this information. Please verify independently before acting on any advice.
********************************************************************** This situation pops up every now and then, when we want to link an external shared library to our program.
A sample cc: command is "g++ -std=c++0x -L/usr/lib/ -laio -Wall -O2 -g aio.cpp -o aio"
THis will throw an error:
/tmp/ccFbxgDn.o: In function `main':
/home/gnayan/quick-and-dirty-libaio-example/aio.cpp:91: undefined reference to `io_queue_init'
/home/gnayan/quick-and-dirty-libaio-example/aio.cpp:99: undefined reference to `io_submit'
/home/gnayan/quick-and-dirty-libaio-example/aio.cpp:103: undefined reference to `io_getevents'
/home/gnayan/quick-and-dirty-libaio-example/aio.cpp:109: undefined reference to `io_destroy'
collect2: error: ld returned 1 exit status
Disclaimer: The information shared on this blog is for general informational purposes only and reflects personal views or experiences in technology. While I strive for accuracy, I cannot guarantee that all content is current or error-free. I am not liable for any loss or damage resulting from the use of this information. Please verify independently before acting on any advice.
********************************************************************** This situation pops up every now and then, when we want to link an external shared library to our program.
A sample cc: command is "g++ -std=c++0x -L/usr/lib/ -laio -Wall -O2 -g aio.cpp -o aio"
THis will throw an error:
/tmp/ccFbxgDn.o: In function `main':
/home/gnayan/quick-and-dirty-libaio-example/aio.cpp:91: undefined reference to `io_queue_init'
/home/gnayan/quick-and-dirty-libaio-example/aio.cpp:99: undefined reference to `io_submit'
/home/gnayan/quick-and-dirty-libaio-example/aio.cpp:103: undefined reference to `io_getevents'
/home/gnayan/quick-and-dirty-libaio-example/aio.cpp:109: undefined reference to `io_destroy'
collect2: error: ld returned 1 exit status
You will check that the library is installed in its standard path /usr/lib/ or /usr/local/lib....or in x86_64 system.
/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu
But still the compilation fails..or rather the linking fails.
So whats the problem:
What if we do: "g++ -std=c++0x -L/usr/lib/ -Wall -O2 -g aio.cpp -o aio -laio"
This will compile and link succesfully, so whats the trick:
The trick here is to put the library AFTER the module you are compiling.
The problem is a reference thing. The linker resolves references in
order, so when the library is BEFORE the module being compiled, the
linker gets confused and does not think that any of the functions in the
library are needed. By putting the library AFTER the module, the
references to the library in the module are resolved by the linker.