C++,如果新文件出现问题,使用 g++ 编译工作正常

标签 c++

我在后台运行 countryServer 之后遇到了这个问题,然后我将其终止。我尝试重新编译一个新版本,这就是我得到的

root@ubuntu:/home/baoky/shell_assignment/shell_assn2# g++ -o countryServer CountryServer.cpp
root@ubuntu:/home/baoky/shell_assignment/shell_assn2# ./countryServer &
[1] 3097
root@ubuntu:/home/baoky/shell_assignment/shell_assn2# 
Running server program 'css' ...... 

Country Directory server started

root@ubuntu:/home/baoky/shell_assignment/shell_assn2# ps
  PID TTY          TIME CMD
 2993 pts/3    00:00:00 su
 3001 pts/3    00:00:00 bash
 3097 pts/3    00:00:00 countryServer
 3098 pts/3    00:00:00 ps
root@ubuntu:/home/baoky/shell_assignment/shell_assn2# kill 3097
[1]+  Terminated              ./countryServer
root@ubuntu:/home/baoky/shell_assignment/shell_assn2# g++ -o countryServer CountryServer.cpp
/usr/bin/ld: cannot open output file countryServer: No such device or address
collect2: ld returned 1 exit status

root@ubuntu:/home/baoky/shell_assignment/shell_assn2# ls -l
total 60
-rw-r--r-- 1 baoky baoky 19545 Aug  2 10:33 Countries.txt
-rwxr-xr-x 1 root  root  14756 Aug  4 03:17 countryClient
-rw-r--r-- 1 baoky baoky  3514 Aug  4 03:19 CountryClient.cpp
-rw-r--r-- 1 baoky baoky  4740 Aug  4  2012 CountryData.c
-rw-r--r-- 1 baoky baoky  2022 Aug  2 10:33 CountryData.h
srwxr-xr-x 1 root  root      0 Aug  4 03:29 countryServer
-rw-r--r-- 1 baoky baoky  7762 Aug  4 03:18 CountryServer.cpp

我得到一个返回给我这个的错误 /usr/bin/ld: 无法打开输出文件 countryServer: 没有这样的设备或地址 collect2: ld 返回 1 个退出状态

如果我执行 rm -rf countryServer 我可以修复它

但是我该如何修复它以便它可以覆盖旧的 countryServer(在我终止它的进程并重新编译它之后)

最佳答案

您的 countryServer 程序似乎正在当前目录中创建一个名为 countryServer 的套接字文件,并在此过程中覆盖自身。

在编译您的代码之前删除该套接字文件,或者更好:更改您的代码,使其不会覆盖其自身的可执行文件。

关于C++,如果新文件出现问题,使用 g++ 编译工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11807826/

相关文章:

c++ - 枚举映射对重构具有鲁棒性

c++ - 使用非成员 ostream 重载函数打印任何 c++11 数组

c++ - 我可以将 Boost 源代码+头文件添加到我自己的(开源)项目中吗?

c++ - 如何将可变数量的参数传递给构造函数?

c++ - 适合初学者的良好调试器教程

c++ - 在 CMake 中,命令 SUBDIRS() 和 ADD_SUBDIRECTORY() 有什么区别

c++ - hpp 文件 && 实现类扩展 vector

c++ - 解决类似 Flood-It 难题的最少点击次数

c++ - 在 C++ 中返回多维数组

c++ - 在父类(super class)中重载运算符,但子类声称它已经定义