Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

to link against ``memset'', how to write ``ld'' command?

Status
Not open for further replies.

julianLiuRsch

Programmer
Oct 4, 2008
11
I complied and linked a very simple .c file, a.c :
---
#include <stdio.h>
int main() {
char* eee[][100] = {
"aaa",
"bbb"
};
}
===
and I used these two steps to compile and link it
---
gcc -c -o b.o b.c
ld -o z b.o
===
the error was
---
ld -o z b.o
ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0
b.o: In function `main':
b.c:(.text+0x1d): undefined reference to `memset'
===
Obviously I wrote a wrong ld command. If I insist on using ld to get the final exe-elf file (``z''), how to write this ld command?

(I am using Fedora 10 x86_64, gcc's condition is as below
---
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-cpu=generic --host=x86_64-redhat-linux
Thread model: posix
gcc version 4.1.2 20070925 (Red Hat 4.1.2-33)
===
)

thanks in adv!

 
> ld -o z b.o
So do
[tt]gcc -o z b.o[/tt]

It might look like you're invoking the compiler, but you're really invoking the linker.
What's more, you're invoking the linker with a whole raft of essential command line arguments.

Try
[tt]gcc -v -o z b.o[/tt]
to get a detailed picture of what's going on.



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
hi,

seems that compiler call, automativally memset() function,
to initialize something.

try in 2 steps to:

1)
...
char* eee[2][100] ;
...
compile & link
does give error ?

2)
...
char* eee[2][100] ;
memcp( eee[0][0], "aaa" ) ;
memset( eee[0][0], 10, 10 ) ;
...
compile & link
does give error ?

If it gives error at compile time, look in your doc,
which #include you have to do, to declare memset.

Futhermore, the expression:

char* eee[][100] ;

means a "bidimensional Array (matrix) of CharPointers" .

Did you mean "Array of pointers" ? In this case, use:
char* eee[100] = { "a", "bb", "ccc", "dd", "e" } ;

If you declare
char* eee[][100] ;
you tell the compiler that you have a matrix of 100 cols,
unknown rows, and each cell is a pointer to string:
the first to a static area loaded by aaa+nul and the second
to bbb+nul.
If you really mean a bidimensional array, in the initialization area (not all compilers permit it),
you have to use 2 levels of {} brakets ;

int Matrix[5][5]={ { 1,2,3}, {4,5,6}, {7,8,9}, {10,11,12} } ;

In this mode, you give, for compiler, a sense to your sequence of data: the integer 4,5 are not the 4th and 5th
value of the first row, but are the 1st and 2nd value for the 2nd row. If you don't use two levels of {}, compiler
probably interpretes flat them and undertand as and 5th
value.

ciao
vittorio

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top