rizsotto / Bear

Bear is a tool that generates a compilation database for clang tooling.
GNU General Public License v3.0
4.76k stars 312 forks source link

[Question] How to build static binary for linux? #430

Closed fcying closed 2 years ago

fcying commented 2 years ago

I use bear on some linux server which run old debian, I have not root account, so I want build a static bear and copy to the server.

I try add build options "-static -pthread -lrt -ldl", but build failed.

$cmake -BRelease -DCMAKE_CXX_FLAGS="-static -pthread -lrt -ldl"
-- Looking for nlohman_json dependency
-- Looking for nlohman_json dependency -- found
-- Looking for fmt dependency
-- Looking for fmt dependency -- found
-- Looking for spdlog dependency
-- Looking for spdlog dependency -- found
-- Looking for gRPC::grpc++ dependency
-- Looking for gRPC::grpc++ dependency -- found
-- Looking for protoc
-- Looking for protoc -- found
-- Looking for grpc_cpp_plugin
-- Looking for grpc_cpp_plugin -- found
-- Looking for GTest dependency
-- Looking for GTest dependency -- found
-- Configuring done
-- Generating done
-- Build files have been written to: /home/fcying/tool/src/bear/Release

$ cmake --build Release -j12
[  0%] Built target googletest_dependency
[  0%] Built target nlohmann_json_dependency
[  0%] Built target grpc_dependency
[  0%] Built target spdlog_dependency
[  0%] Built target fmt_dependency
[  5%] Performing build step for 'BearSource'
[  5%] Built target shell_a
[  6%] Linking CXX executable result_unit_test
[  6%] Built target flags_a
[ 18%] Built target sys_a
[ 19%] Built target exec_a
[ 26%] Built target rpc_a
/usr/bin/ld: attempted static link of dynamic object `/usr/lib/x86_64-linux-gnu/libpthread.so'
collect2: error: ld returned 1 exit status
gmake[5]: *** [libresult/CMakeFiles/result_unit_test.dir/build.make:110: libresult/result_unit_test] Error 1
gmake[4]: *** [CMakeFiles/Makefile2:1094: libresult/CMakeFiles/result_unit_test.dir/all] Error 2
gmake[4]: *** Waiting for unfinished jobs....
[ 30%] Built target exec
[ 31%] Linking CXX executable shell_unit_test
[ 32%] Linking CXX executable flags_unit_test
[ 38%] Built target citnames_json_a
[ 38%] Linking CXX executable sys_unit_test
[ 38%] Built target main_a
[ 39%] Linking CXX executable exec_unit_test
/usr/bin/ld: attempted static link of dynamic object `/usr/lib/x86_64-linux-gnu/libpthread.so'
collect2: error: ld returned 1 exit status
/usr/bin/ld: attempted static link of dynamic object `/usr/lib/x86_64-linux-gnu/libpthread.so'
collect2: error: ld returned 1 exit status
gmake[5]: *** [libshell/CMakeFiles/shell_unit_test.dir/build.make:112: libshell/shell_unit_test] Error 1
gmake[4]: *** [CMakeFiles/Makefile2:1175: libshell/CMakeFiles/shell_unit_test.dir/all] Error 2
gmake[5]: *** [libflags/CMakeFiles/flags_unit_test.dir/build.make:113: libflags/flags_unit_test] Error 1
gmake[4]: *** [CMakeFiles/Makefile2:1121: libflags/CMakeFiles/flags_unit_test.dir/all] Error 2
/usr/bin/ld: CMakeFiles/sys_a.dir/source/Process.cc.o: in function `(anonymous namespace)::resolve_spawn_function()::{lambda(char const*, char* const*, char* const*)#1}::operator()(char const*, char* const*, char* const*) const [clone .constprop.0]':
Process.cc:(.text+0x487a): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: attempted static link of dynamic object `/usr/lib/x86_64-linux-gnu/libpthread.so'
collect2: error: ld returned 1 exit status
gmake[5]: *** [intercept/CMakeFiles/exec_unit_test.dir/build.make:186: intercept/exec_unit_test] Error 1
gmake[4]: *** [CMakeFiles/Makefile2:1549: intercept/CMakeFiles/exec_unit_test.dir/all] Error 2
/usr/bin/ld: attempted static link of dynamic object `/usr/lib/x86_64-linux-gnu/libpthread.so'
collect2: error: ld returned 1 exit status
gmake[5]: *** [libsys/CMakeFiles/sys_unit_test.dir/build.make:154: libsys/sys_unit_test] Error 1
gmake[4]: *** [CMakeFiles/Makefile2:1229: libsys/CMakeFiles/sys_unit_test.dir/all] Error 2
gmake[3]: *** [Makefile:160: all] Error 2
gmake[2]: *** [CMakeFiles/BearSource.dir/build.make:133: subprojects/Stamp/BearSource/BearSource-build] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:250: CMakeFiles/BearSource.dir/all] Error 2
gmake: *** [Makefile:171: all] Error 2
rizsotto commented 2 years ago

You don't need root account to compile and install Bear to a machine. You can install it to your home directory. (The $HOME/.local is a good candidate for that. Most shell have that in the PATH already.)

To build static version of Bear should be possible. (Did check long time before, but not recently.) You need the dependent libraries (libc, libpthread, etc...`) static libraries installed on the machine. Not sure which package you'll need for that on Debian.