[cfe-dev] Static linking a program

Wink Saville via cfe-dev cfe-dev at lists.llvm.org
Tue Jun 26 14:12:28 PDT 2018


I've created a simple program [1] which I'm trying to statically link.
I've got it compiling and linking, but it segfaults when I run it. Obvioulsy
I'm linking the wrong libraries, advice appreciated:

  $ cat main.cpp
  #include <iostream>

  int main(int argc, char *argv[]) {
    for (int i=0; i < argc; i++) {
      std::cout << "argv[" << i << "]: " << argv[i] << std::endl;
    }
  }

  $ cat Makefile
  CXX:=clang++
  CFLAGS:=-std=c++17 -Wall -pedantic -static

  LINKER:=ld.lld
  LFLAGS:=-Bstatic
  LIBS=/usr/lib/libstdc++.a
  LIBS+=/usr/lib/gcc/x86_64-pc-linux-gnu/8.1.1/libgcc_eh.a
  LIBS+=/usr/lib/gcc/x86_64-pc-linux-gnu/8.1.1/libgcc.a
  LIBS+=/usr/lib/libc.a
  LIBS+=/usr/lib/crt1.o /usr/lib/crti.o /usr/lib/crtn.o

  main: main.o Makefile
  $(LINKER) $(LFLAGS) -o $@ $< $(LIBS)

  main.o: main.cpp Makefile
  $(CXX) $(CFLAGS) -c -o $@ $<

  .PHONY: clean
  clean:
  rm -rf main.o main

  $ ld.lld --version
  LLD 6.0.0 (compatible with GNU linkers)
  wink at wink-desktop:~/prgs/explore-cpp/static-linking

  $ clang++ --version
  clang version 6.0.0 (tags/RELEASE_600/final)
  Target: x86_64-pc-linux-gnu
  Thread model: posix
  InstalledDir: /usr/bin

  $ make clean && make
  rm -rf main.o main
  clang++ -std=c++17 -Wall -pedantic -static -c -o main.o main.cpp
  ld.lld -Bstatic -o main main.o /usr/lib/libstdc++.a
/usr/lib/gcc/x86_64-pc-linux-gnu/8.1.1/libgcc_eh.a
/usr/lib/gcc/x86_64-pc-linux-gnu/8.1.1/libgcc.a /usr/lib/libc.a
/usr/lib/crt1.o /usr/lib/crti.o /usr/lib/crtn.o

  wink at wink-desktop:~/prgs/explore-cpp/static-linking (master)
  $ ./main
  Segmentation fault (core dumped)

  wink at wink-desktop:~/prgs/explore-cpp/static-linking (master)
  $ coredumpctl gdb
             PID: 23339 (main)
             UID: 1000 (wink)
             GID: 100 (users)
          Signal: 11 (SEGV)
       Timestamp: Tue 2018-06-26 13:51:43 PDT (7s ago)
    Command Line: ./main
      Executable: /home/wink/prgs/explore-cpp/static-linking/main
   Control Group: /user.slice/user-1000.slice/session-c2.scope
            Unit: session-c2.scope
           Slice: user-1000.slice
         Session: c2
       Owner UID: 1000 (wink)
         Boot ID: 65910f0244bf4b62905c0198842b62d4
      Machine ID: 8f80fd742eae4659baed812cd07a9439
        Hostname: wink-desktop
         Storage:
/var/lib/systemd/coredump/core.main.1000.65910f0244bf4b62905c0198842b62d4.23339.1530046303000000.lz4
         Message: Process 23339 (main) of user 1000 dumped core.

                  Stack trace of thread 23339:
                  #0  0x0000000000382ef6 n/a
(/home/wink/prgs/explore-cpp/static-linking/main)

  GNU gdb (GDB) 8.1
  Copyright (C) 2018 Free Software Foundation, Inc.
  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  This is free software: you are free to change and redistribute it.
  There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
  and "show warranty" for details.
  This GDB was configured as "x86_64-pc-linux-gnu".
  Type "show configuration" for configuration details.
  For bug reporting instructions, please see:
  <http://www.gnu.org/software/gdb/bugs/>.
  Find the GDB manual and other documentation resources online at:
  <http://www.gnu.org/software/gdb/documentation/>.
  For help, type "help".
  Type "apropos word" to search for commands related to "word"...
  Reading symbols from
/home/wink/prgs/explore-cpp/static-linking/main...(no debugging
symbols found)...done.
  [New LWP 23339]
  Core was generated by `./main'.
  Program terminated with signal SIGSEGV, Segmentation fault.
  #0  0x0000000000382ef6 in _dl_get_origin ()
  (gdb) bt
  #0  0x0000000000382ef6 in _dl_get_origin ()
  #1  0x000000000038255f in _dl_non_dynamic_init ()
  #2  0x000000000037ef41 in __libc_init_first ()
  #3  0x0000000000393d27 in __libc_start_main ()
  #4  0x000000000039368a in _start ()

[1]: https://github.com/winksaville/explore-cpp-static-linking



More information about the cfe-dev mailing list