Page 1 of 1

gcc can't link

Posted: Mon Aug 31, 2015 10:45 pm
by mcheung63
hi,
my gcc 5.2.0 hi can link to .o but not .a, why?

i586-peter-elf-gcc a.c workspace/PeterI/library/libpeter/libpeter.o <--- this is ok

But this i586-peter-elf-gcc a.c workspace/PeterI/library/libpeter/libpeter.a gives me these

^
/toolchain/lib/gcc/i586-peter-elf/5.2.0/../../../../i586-peter-elf/lib/libc.a(lib_a-peter_stub.o): In function `_read':
/Users/peter/workspace/PeterI/toolchain/build-newlib/i586-peter-elf/newlib/libc/sys/peter/../../../../../../newlib-2.1.0/newlib/libc/sys/peter/peter_stub.c:12: undefined reference to `vfs_fread'
/Users/peter/workspace/PeterI/toolchain/build-newlib/i586-peter-elf/newlib/libc/sys/peter/../../../../../../newlib-2.1.0/newlib/libc/sys/peter/peter_stub.c:22: undefined reference to `getChar'
/toolchain/lib/gcc/i586-peter-elf/5.2.0/../../../../i586-peter-elf/lib/libc.a(lib_a-peter_stub.o): In function `_write':
/Users/peter/workspace/PeterI/toolchain/build-newlib/i586-peter-elf/newlib/libc/sys/peter/../../../../../../newlib-2.1.0/newlib/libc/sys/peter/peter_stub.c:39: undefined reference to `k_outStringToDisplay_len'
/toolchain/lib/gcc/i586-peter-elf/5.2.0/../../../../i586-peter-elf/lib/libc.a(lib_a-peter_stub.o): In function `_open':
/Users/peter/workspace/PeterI/toolchain/build-newlib/i586-peter-elf/newlib/libc/sys/peter/../../../../../../newlib-2.1.0/newlib/libc/sys/peter/peter_stub.c:52: undefined reference to `vfs_fopen'
/toolchain/lib/gcc/i586-peter-elf/5.2.0/../../../../i586-peter-elf/lib/libc.a(lib_a-peter_stub.o): In function `_lseek':
/Users/peter/workspace/PeterI/toolchain/build-newlib/i586-peter-elf/newlib/libc/sys/peter/../../../../../../newlib-2.1.0/newlib/libc/sys/peter/peter_stub.c:34: undefined reference to `vfs_fseek'
/toolchain/lib/gcc/i586-peter-elf/5.2.0/../../../../i586-peter-elf/lib/libc.a(lib_a-peter_stub.o): In function `_close':
/Users/peter/workspace/PeterI/toolchain/build-newlib/i586-peter-elf/newlib/libc/sys/peter/../../../../../../newlib-2.1.0/newlib/libc/sys/peter/peter_stub.c:59: undefined reference to `vfs_fclose'
/toolchain/lib/gcc/i586-peter-elf/5.2.0/../../../../i586-peter-elf/lib/libc.a(lib_a-peter_stub.o): In function `malloc':
/Users/peter/workspace/PeterI/toolchain/build-newlib/i586-peter-elf/newlib/libc/sys/peter/../../../../../../newlib-2.1.0/newlib/libc/sys/peter/peter_stub.c:163: undefined reference to `kmalloc'
/toolchain/lib/gcc/i586-peter-elf/5.2.0/../../../../i586-peter-elf/lib/libc.a(lib_a-peter_stub.o): In function `_malloc_r':
/Users/peter/workspace/PeterI/toolchain/build-newlib/i586-peter-elf/newlib/libc/sys/peter/../../../../../../newlib-2.1.0/newlib/libc/sys/peter/peter_stub.c:167: undefined reference to `kmalloc'
/toolchain/lib/gcc/i586-peter-elf/5.2.0/../../../../i586-peter-elf/lib/libc.a(lib_a-peter_stub.o): In function `free':
/Users/peter/workspace/PeterI/toolchain/build-newlib/i586-peter-elf/newlib/libc/sys/peter/../../../../../../newlib-2.1.0/newlib/libc/sys/peter/peter_stub.c:171: undefined reference to `kfree'
/toolchain/lib/gcc/i586-peter-elf/5.2.0/../../../../i586-peter-elf/lib/libc.a(lib_a-peter_stub.o): In function `_free_r':
/Users/peter/workspace/PeterI/toolchain/build-newlib/i586-peter-elf/newlib/libc/sys/peter/../../../../../../newlib-2.1.0/newlib/libc/sys/peter/peter_stub.c:175: undefined reference to `kfree'


thanks
from Peter ([email protected])

Re: gcc can't link

Posted: Tue Sep 01, 2015 2:24 am
by Combuster
Start with using -l and -L for library archives

Re: gcc can't link

Posted: Tue Sep 01, 2015 4:12 am
by mcheung63
Hi, i have -L and -l. Otherwise it prompt me
/toolchain/lib/gcc/i586-peter-elf/5.2.0/../../../../i586-peter-elf/bin/ld: cannot find -lpeter

Any other hints?
thanks

Re: gcc can't link

Posted: Tue Sep 01, 2015 7:37 am
by Combuster
Any other hints?
Your second post contradicts your first, so I can't trust anything you said at all. Until the problem between keyboard and chair is fixed I can't help.

Re: gcc can't link

Posted: Tue Sep 01, 2015 9:19 am
by mcheung63
good bye

Re: gcc can't link

Posted: Wed Sep 02, 2015 11:35 am
by rdp
The problem is that you are are placing the .o and .a files on the command line before any reference to the functions in the .o or .a file is made. It's OK in the case of the .o file, because the linker will link it to your other files no matter what. An archive file (.a) is treated differently: Only object files containiing referenced symbols are pulled in. Since it appears that newlib is referencing your functions, you need to put your .a file after the reference to newlib.

-Rich

Re: gcc can't link

Posted: Thu Sep 03, 2015 11:34 pm
by mcheung63
Hi Mr Rdp, your idea is correct, but changing the order of .a and .o doesnt help. i added --start-group in ld, so ld resolves the nested dependency, thanks