误删/usr/bin/ld怎么解决?

误删/usr/bin/ld怎么解决?

1.问题

​ 在ubuntu22.04上不小心误删/usr/bin/ld链接,它是gcc和g++的默认链接器,在binutils包中使用。有网友说重新安装binutils可以解决这个问题,于是操作如下:

sudo apt-get install binutils

但是遇到下面的错误:

/usr/bin/ld: 1: Syntax error: newline unexpected
collect2: error: ld returned 2 exit status

2.解决思路

2.1 安装binutils基础库

​ 因为链接器的删除,在没有备份的情况下无法复制同样系统的/usr/bin/ld文件(用sudo 复制也无法链接到).重装操作系统是可以的,在安装软件不多的情况下适用.我装的软件比较多,于是想尝试一下其他方法.

//删除与ld链接器相关的的所有库,系统自带的库,如python2.7等都会被删除
sudo apt-get autoremove binutils
//建立基础连接
sudo apt-get install binutils

此时在终端操作:

ld
//输出 ld: no input files

说明ld链接正常,但是系统原生的

2.2 安装系统自带gcc等库

此时直接编译C或C++工程会提示,找不到/usr/bin/cc

重新安装吧

//确定依赖关系gcc -> g++ -> gdb,gdb依赖g++,g++依赖gcc
sudo apt-get install gdb
sudo apt install gcc-11 g++-11
sudo apt install gcc g++

hello.c

#include <stdio.h>
int main()
{
  printf ("Hello World!\n");
  return 0;
}

查看编译是否能通过

gcc hello.c -o hello
./hello

2.3 与ROS2建立连接

此时如果直接编译ros2:

colcon build

会出现如下错误

By not providing "Findrosidl_default_generators.cmake" in CMAKE_MODULE_PATH
  this project has asked CMake to find a package configuration file provided
  by "rosidl_default_generators", but CMake did not find one.

  Could not find a package configuration file provided by
  "rosidl_default_generators" with any of the following names:

    rosidl_default_generatorsConfig.cmake
    rosidl_default_generators-config.cmake

老办法,重新安装ros2吧

安装ROS2,一键安装ros2(humble):

get http://fishros.com/install -O fishros && . fishros
//大约需要40分钟

3.Summary

文件: 对根目录下的文件,尤其是usr/bin/,/bin/等操作要谨慎,要注意备份.

command: 使用sudo rm…和sudo mv…要谨慎又谨慎

4.参考文献

1.g++/gcc编译链接遇到Syntax error: newline unexpected 怎么解决?:

https://ask.csdn.net/questions/7562519

2.Ubuntu开发中,gcc默认ld链接器报错的解决办法:

https://blog.csdn.net/benben_ho/article/details/121267650

3.colcon build error on Ros2 Foxy Unity-Robotics-Hub example:

https://stackoverflow.com/questions/71189491/colcon-build-error-on-ros2-foxy-unity-robotics-hub-example

4.Ubuntu18.04 安装后应该做的事

https://blog.csdn.net/hymanjack/article/details/80285400#t12

5.成功解决 ERROR: An error occurred while performing the step: “Building kernel modules“. See /var/log/nv:

https://blog.csdn.net/qq_41185868/article/details/97529463

6.Linux安装gcc的四种方法

https://cloud.tencent.com/developer/article/2091254

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值