gcc can't link

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
mcheung63
Member
Member
Posts: 175
Joined: Thu Jun 22, 2006 8:33 am
Location: Hong Kong
Contact:

gcc can't link

Post 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])
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: gcc can't link

Post by Combuster »

Start with using -l and -L for library archives
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
mcheung63
Member
Member
Posts: 175
Joined: Thu Jun 22, 2006 8:33 am
Location: Hong Kong
Contact:

Re: gcc can't link

Post 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
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: gcc can't link

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
mcheung63
Member
Member
Posts: 175
Joined: Thu Jun 22, 2006 8:33 am
Location: Hong Kong
Contact:

Re: gcc can't link

Post by mcheung63 »

good bye
rdp
Posts: 4
Joined: Sat Apr 25, 2015 6:15 pm
Libera.chat IRC: rdp

Re: gcc can't link

Post 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
User avatar
mcheung63
Member
Member
Posts: 175
Joined: Thu Jun 22, 2006 8:33 am
Location: Hong Kong
Contact:

Re: gcc can't link

Post 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
Post Reply