Discussion:
Build a program using a custom glibc
Amittai Aviram
2012-05-26 03:28:08 UTC
Permalink
I have managed to build glibc 2.14 and install it in directory ~/glibc/glibc_install. So now I want to build and run programs using this C library instead of my system's default C library. First, to be sure that I was using my custom glibc, I added a call to puts into glibc/stdio-common/printf.c:__printf to print a message. Then I rebuilt and reinstalled glibc. Then I wrote a "Hello, World" program and built (compiled and linked) it as follows:

gcc -nodefaultlibs -static -libgcc -L~/GLIBC/glibc_install/lib -o myprog myprog.c

But I get the following linker error report:

/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start':
(.text+0x19): undefined reference to `__libc_csu_init'
/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start':
(.text+0x25): undefined reference to `__libc_start_main'
/tmp/ccACTQEp.o: In function `main':
c1.c:(.text+0xa): undefined reference to `puts'
collect2: ld returned 1 exit status

What am I doing wrong? Thanks!




Amittai Aviram
PhD Student in Computer Science
Yale University
646 483 2639
amittai.aviram-***@public.gmane.org
http://www.amittai.com
Paul Pluzhnikov
2012-05-26 03:38:40 UTC
Permalink
Post by Amittai Aviram
gcc -nodefaultlibs -static -libgcc -L~/GLIBC/glibc_install/lib -o myprog myprog.c
(.text+0x19): undefined reference to `__libc_csu_init'
(.text+0x25): undefined reference to `__libc_start_main'
c1.c:(.text+0xa): undefined reference to `puts'
collect2: ld returned 1 exit status
What am I doing wrong?  Thanks!
Libc also has startup files, crt*.o, which must match the libc you are using.

Go into your glibc build directory, and do "make check". Note the
command line that is being used to link tests. Replicate that command
line (adjusting for your ~/GLIBC/glibc_install/lib installation).

Also note that your command line currently is *completely* bogus:
-llibgcc asks the linker to look for liblibgcc.{a,so}; perhaps you
meant '-lc' to link with libc?

The order of libraries and sources on the command line matters:
http://webpages.charter.net/ppluzhnikov/linker.html

Finally, "-L~/..." will not be expanded by the shell. Try '-L ~/...'
or '-L$HOME/...' instead.

Hope this helps.
--
Paul Pluzhnikov
Amittai Aviram
2012-05-26 04:54:33 UTC
Permalink
Post by Paul Pluzhnikov
Post by Amittai Aviram
gcc -nodefaultlibs -static -libgcc -L~/GLIBC/glibc_install/lib -o myprog myprog.c
-llibgcc asks the linker to look for liblibgcc.{a,so}; perhaps you
meant '-lc' to link with libc?
Sorry--that was a typo in my e-mail, not in my command line. I had -lgcc. I was trying to follow the suggestions found here:
http://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Link-Options.html#Link-Options
--under "-nodefaultlibs."
Post by Paul Pluzhnikov
http://webpages.charter.net/ppluzhnikov/linker.html
I know, but my command line only had one library search path and one source file. Anyway, I'll try your suggestion with "make check."
Post by Paul Pluzhnikov
Finally, "-L~/..." will not be expanded by the shell. Try '-L ~/...'
or '-L$HOME/...' instead.
Actually, I entered the absolute path, but had the same results.

Amittai
Siddhesh Poyarekar
2012-05-26 04:10:14 UTC
Permalink
Post by Amittai Aviram
gcc -nodefaultlibs -static -libgcc -L~/GLIBC/glibc_install/lib -o myprog myprog.c
(.text+0x19): undefined reference to `__libc_csu_init'
(.text+0x25): undefined reference to `__libc_start_main'
c1.c:(.text+0xa): undefined reference to `puts'
collect2: ld returned 1 exit status
What am I doing wrong?  Thanks!
You can do the compile and link separately. And for the linker, you
provide the crt*.o files from ~/GLIBC/glibc_install/lib.

Try running the gcc command with a -v and you'll see what the linker
gets called with. You can then use the same command with your paths
adjusted to use your glibc.

Once you get this working, it would be great if you could add this to
the glibc wiki in the tips and tricks section:

http://sourceware.org/glibc/wiki
--
Siddhesh Poyarekar
http://siddhesh.in
Amittai Aviram
2012-05-27 06:31:38 UTC
Permalink
Post by Siddhesh Poyarekar
You can do the compile and link separately. And for the linker, you
provide the crt*.o files from ~/GLIBC/glibc_install/lib.
Try running the gcc command with a -v and you'll see what the linker
gets called with. You can then use the same command with your paths
adjusted to use your glibc.
Thank you very much for this suggestion, and thanks very much to Paul Pluzhnikov for his somewhat similar suggestion of using the command line of "make check" as a template. With some further work, I've arrived at a solution, represented by the sample makefile below. In this one, I assume that you have a program called "prog" that you want to build from source "prog.c" and that you have installed your custom glibc in directory "/home/my_acct/glibc_install."

TARGET = prog
OBJ = $(TARGET).o
SRC = $(TARGET).c
CC = gcc
CFLAGS = -g
LDFLAGS = -nostdlib -nostartfiles -static
GLIBCDIR = /home/my_acct/glibc_install/lib
STARTFILES = $(GLIBCDIR)/crt1.o $(GLIBCDIR)/crti.o `gcc --print-file-name=crtbegin.o`
ENDFILES = `gcc --print-file-name=crtend.o` $(GLIBCDIR)/crtn.o
LIBGROUP = -Wl,--start-group $(GLIBCDIR)/libc.a -lgcc -lgcc_eh -Wl,--end-group

$(TARGET): $(OBJ)
$(CC) $(LDFLAGS) -o $@ $(STARTFILES) $^ $(LIBGROUP) $(ENDFILES)
$(OBJ): $(SRC)
$(CC) $(CFLAGS) -c $^
Post by Siddhesh Poyarekar
Once you get this working, it would be great if you could add this to
http://sourceware.org/glibc/wiki
I would be glad and honored to do so, but I could not find an "edit" link on the Wiki, and it says at the top right, just below the "Home Page" tab, "Immutable Page." Is there a way that I can contribute this to the Wiki Tips and Tricks section? Thanks!

Best wishes,
Amittai

Amittai Aviram
PhD Student in Computer Science
Yale University
646 483 2639
amittai.aviram-***@public.gmane.org
http://www.amittai.com
Siddhesh Poyarekar
2012-05-27 09:15:52 UTC
Permalink
I would be glad and honored to do so, but I could not find an "edit" link on the Wiki, and it says at the top right, just below the "Home Page" tab, "Immutable Page."  Is there a way that I can contribute this to the Wiki Tips and Tricks section?  Thanks!
I guess you haven't signed up on the wiki? You'll need to register
before you can make changes. Click on the 'Login' link at the top and
the resulting page will have a link to create a new account. Or just
follow this link:

http://sourceware.org/glibc/wiki/HomePage?action=newaccount
--
Siddhesh Poyarekar
http://siddhesh.in
Amittai Aviram
2012-05-27 19:25:13 UTC
Permalink
Post by Siddhesh Poyarekar
Post by Amittai Aviram
I would be glad and honored to do so, but I could not find an "edit" link on the Wiki, and it says at the top right, just below the "Home Page" tab, "Immutable Page." Is there a way that I can contribute this to the Wiki Tips and Tricks section? Thanks!
I guess you haven't signed up on the wiki? You'll need to register
before you can make changes.
Done. Thanks a lot. (Had just failed to see the "login" link at the top until I read your message. :-/ )

Amittai Aviram
PhD Student in Computer Science
Yale University
646 483 2639
amittai.aviram-***@public.gmane.org
http://www.amittai.com

Continue reading on narkive:
Search results for 'Build a program using a custom glibc' (Questions and Answers)
7
replies
What is the best Linux OS for computer programming?
started 2014-02-01 06:05:01 UTC
programming & design
Loading...