[cfe-dev] Static linking a program
Wink Saville via cfe-dev
cfe-dev at lists.llvm.org
Tue Jun 26 14:48:07 PDT 2018
Yes, that worked:
$ clang++ -static main.cpp
wink at wink-desktop:~/prgs/explore-cpp-static-linking (master)
$ ls -al
total 2132
drwxr-xr-x 3 wink users 4096 Jun 26 14:43 .
drwxr-xr-x 64 wink users 4096 Jun 26 13:58 ..
-rwxr-xr-x 1 wink users 2147832 Jun 26 14:43 a.out
drwxr-xr-x 8 wink users 4096 Jun 26 14:09 .git
-rw-r--r-- 1 wink users 9 Jun 26 13:58 .gitignore
-rw-r--r-- 1 wink users 1210 Jun 26 13:58 LICENSE
-rw-r--r-- 1 wink users 159 Jun 26 14:06 main.cpp
-rw-r--r-- 1 wink users 462 Jun 26 13:58 Makefile
-rw-r--r-- 1 wink users 2926 Jun 26 14:01 README.md
wink at wink-desktop:~/prgs/explore-cpp-static-linking (master)
$ ./a.out
argv[0]: ./a.out
wink at wink-desktop:~/prgs/explore-cpp-static-linking (master)
$ ldd a.out
not a dynamic executable
Is there a way to find out what it linked it with, I like to "be in control"?
On Tue, Jun 26, 2018 at 2:36 PM, Shoaib Meenai <smeenai at fb.com> wrote:
> Is there any reason you're invoking the linker directly? That's usually
> fraught with peril, and it's best to let the driver take care of that for
> you … I do see that you're linking all the start files yourself, but the
> ordering can also make a difference.
>
>
>
> What happens if you just run something like `clang++ -static main.cpp`? Does
> the resulting a.out run successfully? (The `-static` should get passed from
> the driver to the linker.)
>
>
>
> From: cfe-dev <cfe-dev-bounces at lists.llvm.org> on behalf of cfe-dev
> <cfe-dev at lists.llvm.org>
> Reply-To: Wink Saville <wink at saville.com>
> Date: Tuesday, June 26, 2018 at 2:13 PM
> To: cfe-dev <cfe-dev at lists.llvm.org>
> Subject: [cfe-dev] Static linking a program
>
>
>
> 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
>
> _______________________________________________
>
> cfe-dev mailing list
>
> cfe-dev at lists.llvm.org
>
> https://urldefense.proofpoint.com/v2/url?u=http-3A__lists.llvm.org_cgi-2Dbin_mailman_listinfo_cfe-2Ddev&d=DwIGaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=o3kDXzdBUE3ljQXKeTWOMw&m=a-6d12kEoDpvHTe7FgHqZIzVFXeGNDaY1ojO68-yyUY&s=zXH4GyhwrDgHIBQQ88aviDyW6w65CWL5TQpvBEYKUpI&e=
>
>
More information about the cfe-dev
mailing list