undefined reference - example code and commands included

undefined reference - example code and commands included

Also getting undefined reference. Please help! Here are some code snippets.
[]$ ls
MyLib.cpp MyLib.h test.cpp
[]$ cat MyLib.h
#if !defined _MYLIB_H_
#define _MYLIB_H_
extern void print(void);
[]$ cat MyLib.cpp
#include "MyLib.h"
void print(void) {
std::cout << "Hello World!" << std::endl;
[]$ icc -g -Wall -I./ -c MyLib.cpp -o MyLib.o
[]$ xiar r libMyLib.a MyLib.o
xiar: executing 'ar'
[]$ cat test.cpp
#include "MyLib.h"
int main (int argc, char* argv[]) {
return 0;
[]$ icc -g -Wall -I./ -c test.cpp -o test.o
[]$ icc -g -Wall -L./ -lMyLib test.o -o test
test.o: In function 'main':
./test.cpp:3: undefined reference to 'print()'
[]$ ls
libMyLib.a MyLib.cpp MyLib.h MyLib.o test.cpp test.o
[]$ xiar t libMyLib.a
xiar: executing 'ar'

There may be typos, couldn't get copy and paste to work.

If I extract MyLib.o from the library and relink with:
[]$ icc -g -Wall test.o MyLib.o -o test
It works fine.

What am I doing wrong?

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Instead of -
icc -g -Wall -L./ -lMyLib test.o -o test

icc -g -Wall -L./ test.o -lMyLib -o test
This should solve your issue.


Another way can be use shared library
$ icc -shared MyLib.o -o libMyLib.so

Then the sequence of files for creating the excutable do not matter.

Thanks everyone. Works great now. I appreciate all the help.


I am trying to tune an application on Itanium. when i use -parallel option the executable has the dependency on libguide.so. when i run the executable
i am getting the following error.

"error while loading shared libraries: libguide.so: cannot open shared object file: No such file or directory"

I have set the LD_LIBRARY_PATH and libguide.so is present in that path. I dont know why it's not picking up. when i did ldd on the executable it shows

/hpcsw/apps/linux/ipf/ecc-8.1_010/lib/libguide.so => /hpcsw/apps/linux/ipf/ecc-8.1_010/lib/libguide.so (0x2000000000040000)
libm.so.6.1 => /lib/tls/libm.so.6.1 (0x20000000000b0000)
libguide.so => not found
libpthread.so.0 => /lib/tls/libpthread.so.0 (0x2000000000140000)
libc.so.6.1 => /lib/tls/libc.so.6.1 (0x2000000000170000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x20000000003d8000)
libdl.so.2 => /lib/libdl.so.2 (0x2000000000408000)
/lib/ld-linux-ia64.so.2 => /lib/ld-linux-ia64.so.2 (0x2000000000000000)

I am usingthe following linker"/lib/ld-linux-ia64.so.2 -> ld-2.3.2.so". I tried to do static compilation also, but that too was not successful. Quick reply wil be highly appreciated.

Thanks in advance,

Leave a Comment

Please sign in to add a comment. Not a member? Join today