richfelker / musl-cross-make

Simple makefile-based build for musl cross compiler

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

I wrote a hello world applet, but it can't run

skychengzhixing opened this issue · comments

hi,friend:
I wrote a hello world applet, but it can't run;
I generated a hello world applet from GCC in the cross tool chain compiled by myself, but it failed to run. The specific errors are as follows:
The steps are as follows:
(1)x86_64-linux-musl-gcc 11.c -o main
(2)./main
Errors are reported as follows:

  1. Execute the following command : ./main Error reporting: -bash: ./main: No such file or directory
  2. Use the following command to view the dependent dynamic libraries: ldd main Error reporting:./main: error while loading shared libraries: /lib64/libc.so: invalid ELF header
    my host: Use this command to view my hosts :uname -m result:x86_64

I tried some methods, but the problem still hasn't been solved. Can you help me see what caused the problem?

bash: ./main: No such file or directory

clear indication that the dynamic link interpreter isn't in the expected path.

check readelf -a main | grep -i interp to see the expected path.

you need to either install the musl dynlinker to that location, or run path/to/musl-cross-install/lib/libc.so main. alternatively you can static link your binary. the reason the dynlinker isnt installed to your system /lib is because mcm is a cross-compiler intended to compile binaries for other systems (which have the proper filesystem layout), and since it would require additional privileges.

Thank you for your reply. According to your method, both the static link method and the dynamic link method can make the program run.
I record my method. If other people encounter similar problems, you can refer to the following:

  1. static link library: running: x86_ 64 Linux musl CC -static 11 C -o Jing
  2. dynlinker :### readelf -a main | grep -i interp ### to see the expected path,result:[Requesting program interpreter: /lib/ld-musl-x86_64.so.1]. Run the LS -al command to check whether the current library has a soft connection?
  3. Finally, copy the required library to the corresponding directory. My copy library commands are:
    sudo cp -r libc.so /usr/lib
    sudo cp -r ld-musl-x86_64.so.1 /usr/lib
    Good wishes!