rizsotto / Bear

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

How to set CMAKE_INSTALL_LIBDIR after manually compiling GCC? #422

Closed tr4v3ler closed 2 years ago

tr4v3ler commented 2 years ago

Describe the bug After manually compiling gcc 9.4.0, compile bear and report an error.

To Reproduce Manually compile GCC and install it in the /usr/local/gcc-9 directory, and then use manually compiled GCC to compile bear:

# compile gcc 9.4.0
$ ../configure --build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu --target=x86_64-pc-linux-gnu --prefix=/usr/local/gcc-9 --enable-languages=c,c++ --program-suffix=-9
$ make && sudo make install

# compile bear
$ ~/data/tools/cmake-3.21.3/bin/cmake -DENABLE_UNIT_TESTS=OFF -DENABLE_FUNC_TESTS=OFF -DCMAKE_INSTALL_LIBDIR=gcc-9/lib64 ..
$ make all -j40

The exception log is as follows:

# /home/tr4v3ler/data/tools/Bear/build/subprojects/Stamp/grpc_dependency/grpc_dependency-build-err.log
/home/tr4v3ler/data/tools/Bear/build/subprojects/Build/grpc_dependency/third_party/protobuf/protoc-3.14.0.0: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.26' not found (required by /home/tr4v3ler/data/tools/Bear/build/subprojects/Build/grpc_dependency/third_party/protobuf/protoc-3.14.0.0)
/home/tr4v3ler/data/tools/Bear/build/subprojects/Build/grpc_dependency/third_party/protobuf/protoc-3.14.0.0: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.26' not found (required by /home/tr4v3ler/data/tools/Bear/build/subprojects/Build/grpc_dependency/third_party/protobuf/protoc-3.14.0.0)
make[5]: *** [gens/src/proto/grpc/channelz/channelz.grpc.pb.cc] Error 1
make[5]: *** [gens/src/proto/grpc/reflection/v1alpha/reflection.grpc.pb.cc] Error 1
make[4]: *** [CMakeFiles/grpcpp_channelz.dir/all] Error 2
make[4]: *** Waiting for unfinished jobs....
make[4]: *** [CMakeFiles/grpc++_reflection.dir/all] Error 2
make[3]: *** [all] Error 2

You can see from the log that /usr/lib/x86_64-linux-gnu/libstdc++.so.6 is used instead of /usr/local/gcc-9/lib64/libstdc++.so.6

Environment:

rizsotto commented 2 years ago

I don't see where did you specify to CMake to use the new compiler?

# compile gcc 9.4.0
$ ../configure --build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu --target=x86_64-pc-linux-gnu --prefix=/usr/local/gcc-9 --enable-languages=c,c++ --program-suffix=-9
$ make && sudo make install

After this, make sure this is working... Check out an example from here (save as /tmp/test.cc), and try to compile it with /usr/local/gcc-9/bin/g++ /tmp/test.cc -std=c++17. This will prove that you got the compiler working with the previously problematic std::filesystem.

Then try to build Bear, but you need to specify to the build that you are using a different compiler. And for the CMAKE_INSTALL_LIBDIR should be aligned to your distro (see more about that in the INSTALL.md).

Clean up your build directory before you build! Remove all CMake generated files from previous runs.

$ export CC=/usr/local/gcc-9/bin/gcc
$ export CXX=/usr/local/gcc-9/bin/g++
$ ~/data/tools/cmake-3.21.3/bin/cmake -DENABLE_UNIT_TESTS=OFF -DENABLE_FUNC_TESTS=OFF -DCMAKE_INSTALL_LIBDIR=lib/x86_64-linux-gnu ..
$ make all -j40
rizsotto commented 2 years ago

I am trying to use the issues to track bugs and future works for this project. This is more like a question, or asking help, which I would prefer to do on the discussion (like this one) or I'm happy to chat on Gitter.

I'm closing this ticket now. Feel free to stay on this ticket, I'll try to help you with this compilation.

tr4v3ler commented 2 years ago

I don't see where did you specify to CMake to use the new compiler?

# compile gcc 9.4.0
$ ../configure --build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu --target=x86_64-pc-linux-gnu --prefix=/usr/local/gcc-9 --enable-languages=c,c++ --program-suffix=-9
$ make && sudo make install

After this, make sure this is working... Check out an example from here (save as /tmp/test.cc), and try to compile it with /usr/local/gcc-9/bin/g++ /tmp/test.cc -std=c++17. This will prove that you got the compiler working with the previously problematic std::filesystem.

Then try to build Bear, but you need to specify to the build that you are using a different compiler. And for the CMAKE_INSTALL_LIBDIR should be aligned to your distro (see more about that in the INSTALL.md).

Clean up your build directory before you build! Remove all CMake generated files from previous runs.

$ export CC=/usr/local/gcc-9/bin/gcc
$ export CXX=/usr/local/gcc-9/bin/g++
$ ~/data/tools/cmake-3.21.3/bin/cmake -DENABLE_UNIT_TESTS=OFF -DENABLE_FUNC_TESTS=OFF -DCMAKE_INSTALL_LIBDIR=lib/x86_64-linux-gnu ..
$ make all -j40

I specified the default gcc compiler as follows:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/local/gcc-9/bin/gcc-9 2 --slave /usr/bin/g++ g++ /usr/local/gcc-9/bin/g++-9
sudo update-alternatives --config gcc

In addition, it can be seen from the log that the compiler used is really my version 9.4.0:

~/data/tools/cmake-3.21.3/bin/cmake -DENABLE_UNIT_TESTS=OFF -DENABLE_FUNC_TESTS=OFF -DCMAKE_INSTALL_LIBDIR=gcc-9/lib64 ..
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Setting build type to 'Release' as none was specified.
-- Looking for nlohman_json dependency
-- Looking for nlohman_json dependency -- not found
-- Looking for fmt dependency
-- Looking for fmt dependency -- not found
-- Looking for spdlog dependency
-- Looking for spdlog dependency -- not found
-- Looking for gRPC::grpc++ dependency
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1") 
-- Checking for modules 'protobuf>=3.11;grpc++>=1.26'
--   No package 'protobuf' found
--   Requested 'grpc++ >= 1.26' but version of gRPC++ is 1.3.2
-- Looking for gRPC::grpc++ dependency -- not found
-- Configuring done
-- Generating done
-- Build files have been written to: /home/tr4v3ler/data/tools/Bear/build
rizsotto commented 2 years ago

Ok, is the filesystem example compiles and runs on your machine?

tr4v3ler commented 2 years ago

Ok, is the filesystem example compiles and runs on your machine?

It does seem to be a compiler problem. It does not use the new dynamic link library:

$ g++ ./test.cc -std=c++17 -o test
$ ./test
./test: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.26' not found (required by ./test)
rizsotto commented 2 years ago

Would you try to install compiler from package?

https://linuxize.com/post/how-to-install-gcc-compiler-on-ubuntu-18-04/

tr4v3ler commented 2 years ago

Would you try to install compiler from package?

https://linuxize.com/post/how-to-install-gcc-compiler-on-ubuntu-18-04/

OK, I'll try.

tr4v3ler commented 2 years ago

Would you try to install compiler from package?

https://linuxize.com/post/how-to-install-gcc-compiler-on-ubuntu-18-04/

As a temporary solution, we can use:

export LD_LIBRARY_PATH=/usr/local/gcc-9/lib64/

This allows us to link to the newly compiled dynamic link library. Now everything is OK.

tr4v3ler commented 2 years ago

Ok, is the filesystem example compiles and runs on your machine?

I set CMAKE_INSTALL_LIBDIR=gcc-9/lib64, then /usr/local/gcc-9/lib64/bear/libexec.so is generated after compilation. Before that, I added /usr/local/gcc-9/lib64 to the path of the dynamic linker by modifying the /etc/ld.so.conf file, so that GCC 9.4.0 can find the newly installed libstdc++.so. However, when I run bear, the following problems still arise. Why can't it find libexec.so after I set CMAKE_INSTALL_LIBDIR=gcc-9/lib64?

$ bear -- /usr/local/gcc-9/bin/g++-9 ./test.cc -std=c++17 -o test
ERROR: ld.so: object '/usr/local/$LIB/bear/libexec.so' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
ERROR: ld.so: object '/usr/local/$LIB/bear/libexec.so' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
ERROR: ld.so: object '/usr/local/$LIB/bear/libexec.so' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
ERROR: ld.so: object '/usr/local/$LIB/bear/libexec.so' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
ERROR: ld.so: object '/usr/local/$LIB/bear/libexec.so' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
ERROR: ld.so: object '/usr/local/$LIB/bear/libexec.so' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.

# it's ok
$ bear  --library /usr/local/gcc-9/lib64/bear/libexec.so -- /usr/local/gcc-9/bin/g++-9 ./test.cc -std=c++17 -o test
rizsotto commented 2 years ago

https://github.com/rizsotto/Bear/wiki/Troubleshooting#error-messages-which-appears-with-bear TLDR: you need to install the libexec.so to lib/x86_64-linux-gnu and not lib64 as I recommended earlier.

tr4v3ler commented 2 years ago

https://github.com/rizsotto/Bear/wiki/Troubleshooting#error-messages-which-appears-with-bear TLDR: you need to install the libexec.so to lib/x86_64-linux-gnu and not lib64 as I recommended earlier.

OK, I'll change it back.