C++:关于“ cannot open Debug/ * .exe for writing”

本文记录了一个关于LNK1168错误的问题及其解决方案。作者在修改并重新编译程序时遇到了该错误,经过多次尝试后发现,关闭先前运行的.exe文件可以避免这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转:http://hi.baidu.com/chotenfu/blog/item/72121e059e8f74c67b8947d9.html

一个低级的小问题让我“郁闷”了1天。

今天在编一个通讯函数时,出现了一个奇怪的问题。

描述:函数第一次运行,OK。当进行修改后,运行总是出现如下的错误提示。

--------------------Configuration: community - Win32 Debug--------------------
Compiling...
community.cpp
Linking...
LINK : fatal error LNK1168: cannot open Debug/community.exe for writing
Error executing link.exe.

当将整个环境关闭后,重新执行,也OK。如果在修改后运行仍然出现上述错误提示。......

反复进行上述动作后,仍然无解,结果郁闷到现在。被折磨了一天后,找到正解。原来是我在第一次运行后忘记关掉debug\community.exe

经测试,如果运行后将原先的.exe文件关闭后。再次修改运行,不会出现上述的“奇怪”问题。

此属“低级错误”虽不是技术问题,但第一次遇见,记录之。

转载于:https://www.cnblogs.com/KeenLeung/archive/2012/04/08/2437645.html

In file included from /workspace/x2/dfx/modules/hiviewx_watcher/component/log_watcher/main.cpp:6: /workspace/x2/dfx/modules/hiviewx_watcher/component/log_watcher/include/glog_sink.h: In member function ‘void CustomFileSink::ReopenLogFile()’: /workspace/x2/dfx/modules/hiviewx_watcher/component/log_watcher/include/glog_sink.h:250:43: error: cannot convert ‘std::basic_ofstream<char>::__filebuf_type*’ {aka ‘std::basic_filebuf<char>*’} to ‘FILE*’ 250 | int fd = fileno(outfile_.rdbuf()); | ~~~~~~~~~~~~~~^~ | | | std::basic_ofstream<char>::__filebuf_type* {aka std::basic_filebuf<char>*} In file included from /usr/include/c++/11/cstdio:42, from /usr/include/c++/11/ext/string_conversions.h:43, from /usr/include/c++/11/bits/basic_string.h:6608, from /usr/include/c++/11/string:55, from /usr/include/c++/11/stdexcept:39, from /usr/include/boost/asio/execution/receiver_invocation_error.hpp:19, from /usr/include/boost/asio/execution/detail/as_invocable.hpp:22, from /usr/include/boost/asio/execution/execute.hpp:20, from /usr/include/boost/asio/execution/executor.hpp:20, from /usr/include/boost/asio/associated_executor.hpp:20, from /usr/include/boost/asio.hpp:21, from /workspace/x2/dfx/modules/hiviewx_watcher/component/log_watcher/main.cpp:2: /usr/include/stdio.h:809:26: note: initializing argument 1 of ‘int fileno(FILE*)’ 809 | extern int fileno (FILE *__stream) __THROW __wur; | ~~~~~~^~~~~~~~ make[2]: *** [modules/hiviewx_watcher/component/log_watcher/CMakeFiles/log_watcher.dir/build.make:76: modules/hiviewx_watcher/component/log_watcher/CMakeFiles/log_watcher.dir/main.cpp.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:585: modules/hiviewx_watcher/component/log_watcher/CMakeFiles/log_watcher.dir/all] Error 2 make: *** [Makefile:136: all] Error 2 ================================================== 这是我的报错信息. void ReopenLogFile() { std::lock_guard<std::mutex> lock(file_mutex_); // 先关闭现有文件 if (outfile_.is_open()) { outfile_.flush(); outfile_.close(); } // 确保目录存在 auto dir_path = std::filesystem::path(log_file_path_).parent_path(); if (!dir_path.empty()) { std::filesystem::create_directories(dir_path); } // 重新打开文件 outfile_.open(log_file_path_, std::ios::out | std::ios::app); if (!outfile_.is_open()) { std::cerr << "[ERROR] Failed to open log file: " << log_file_path_ << " - " << strerror(errno) << std::endl; } else { std::cout << "[DEBUG] Successfully reopened log file: " << log_file_path_ << std::endl; // 获取底层文件描述符 (仅用于调试) int fd = fileno(outfile_.rdbuf()); std::cout << "[DEBUG] New file descriptor: " << fd << std::endl; // 强制立即写入到磁盘 fsync(fd); } }这是我的代码.错误如何修改呢
最新发布
07-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值