C++ “无法打开输出文件 filename.exe:权限被拒绝”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23945456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:35:49  来源:igfitidea点击:

"cannot open output file filename.exe: Permission denied"

c++makefiledev-c++

提问by Homberto

I have just learned C++ and I am trying to make a program using the windows.h header. I am using the Dev-C++ compiler and i am getting three errors that I can't find a solution to.

我刚刚学习了 C++,我正在尝试使用 windows.h 头文件制作一个程序。我正在使用 Dev-C++ 编译器,但遇到了三个找不到解决方案的错误。

These are the errors:

这些是错误:

cannot open output file filename.exe: Permission denied
[Error] ld returned 1 exit status recipe for target 'filename.exe' failed

无法打开输出文件 filename.exe:权限被拒绝
[错误] ld 返回 1 个目标“filename.exe”的退出状态配方失败

Here is my code:

这是我的代码:

#include &#60windows.h&
#include &#60iostream&#62
#include &#60fstream&#62
#include &#60string&#62
#include &#60vector&#62
using namespace std;

HWND textfield;
/* This is where all the input to the window goes to */
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
    case WM_CREATE:

            CreateWindow("STATIC",
            "DocJoin Document Combiner",
            WS_VISIBLE | WS_CHILD | WS_BORDER,
            20,20,300,25,
            hwnd,NULL,NULL,NULL);
            break;

    /* Upon destruction, tell the main thread to stop */
    case WM_DESTROY: {
        PostQuitMessage(0);
        break;
    }

    /* All other messages (a lot of them) are processed using default procedures */
    default:
        return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}

/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int     nCmdShow) {
WNDCLASSEX wc; /* A properties struct of our window */
HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
MSG Msg; /* A temporary location for all messages */

/* zero out the struct and set the stuff we want to modify */
memset(&wc,0,sizeof(wc));
wc.cbSize        = sizeof(WNDCLASSEX);
wc.lpfnWndProc   = WndProc; /* This is where we will send messages to */
wc.hInstance     = hInstance;
wc.hCursor       = LoadCursor(NULL, IDC_ARROW);

/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */

if(!RegisterClassEx(&wc)) {
    MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
    return 0;
}

hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","DocJoin",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, /* x */
    CW_USEDEFAULT, /* y */
    683, /* width */
    730, /* height */
    NULL,NULL,hInstance,NULL);

if(hwnd == NULL) {
    MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
    return 0;
}

/*
    This is the heart of our program where all input is processed and 
    sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
    this loop will not produce unreasonably high CPU usage
*/
while(GetMessage(&Msg, NULL, 0, 0) > 0) { /* If no error is received... */
    TranslateMessage(&Msg); /* Translate key codes to chars if present */
    DispatchMessage(&Msg); /* Send it to WndProc */
}
return Msg.wParam;

}

}

and here is the Makefile.win:

这是 Makefile.win:

# Project: ProjectName

# Project: ProjectName

# Makefile created by Dev-C++ 5.6.3

# Makefile created by Dev-C++ 5.6.3

CPP      = g++.exe
CC       = gcc.exe
WINDRES  = windres.exe
OBJ      = main.o
LINKOBJ  = main.o
LIBS     = -L"[...]" -L"[...]" -static-libgcc -mwindows -g3
INCS     = -I"[...]" -I"[...]" -I"[...]"
CXXINCS  = -I"[..]" -I"[...]" -I"[...]" -I"[...]"
BIN      = filename.exe
CXXFLAGS = $(CXXINCS) -g3
CFLAGS   = $(INCS) -g3
RM       = rm.exe -f

.PHONY: all all-before all-after clean clean-custom

all: all-before $(BIN) all-after

clean: clean-custom
${RM} $(OBJ) $(BIN)

$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)

main.o: main.cpp
$(CPP) -c main.cpp -o main.o $(CXXFLAGS)

I spent 3 hours doing research and troubleshooting and I am just stumped. This is my first time using a C++ compiler, meaning that its my first program.

我花了 3 个小时进行研究和故障排除,但我被难住了。这是我第一次使用 C++ 编译器,这意味着它是我的第一个程序。

回答by zahid

You have to change the file location.

您必须更改文件位置。

NOTE: Do not save your file in Program Files& Program Files (x86)in Microsoft Windows.

注意:不要将文件保存在Program FilesProgram Files (x86)Microsoft Windows中。

You can use another path for example C:\USER\.....

例如,您可以使用其他路径 C:\USER\.....

回答by quantdev

The linker cannot delete the existing file filename.exe.

链接器无法删除现有文件filename.exe

  1. Check that "filename" process is not currently runnning.
  2. Check the permissions on it
  3. Try to manually delete/rename it
  1. 检查“文件名”进程当前没有运行。
  2. 检查它的权限
  3. 尝试手动删除/重命名它

As far as the compiler is concerned, there is no error in the source code.

就编译器而言,源代码没有错误。

If I may, you should start with simpler applications if this is your first program (particularly, avoid GUI and go for Console applications)

如果可以的话,如果这是您的第一个程序,您应该从更简单的应用程序开始(尤其是避免使用 GUI 并使用控制台应用程序)

回答by user2740402

I had this same issue for a couple months. I tried all the solutions listed, to no avail. Today I found a solution that worked for me, and I want to share.

几个月来我遇到了同样的问题。我尝试了列出的所有解决方案,但无济于事。今天我找到了一个对我有用的解决方案,我想分享一下。

1 - Uninstall Dev-C++.

1 - 卸载 Dev-C++。

2 - Run regedit, find "orwell", "devcpp", "dev-++", "bloodshed", deleting every record.

2 - 运行 regedit,找到“orwell”、“devcpp”、“dev-++”、“bloodshed”,删除每条记录。

3 - Run Wise Registry Cleaner, scan, cleanup, defrag registry (this step may be overkilling)

3 - 运行 Wise Registry Cleaner,扫描、清理、碎片整理注册表(这一步可能有点矫枉过正)

4 - Delete the program folder "C:\Program Files (x86)\Dev-Cpp" or whatever.

4 - 删除程序文件夹“C:\Program Files (x86)\Dev-Cpp”或其他任何东西。

5 - Download the last version of Dev-C++ from Orwell.

5 - 从 Orwell 下载最新版本的 Dev-C++。

6 - Install normally, default options all the time.

6 - 正常安装,一直默认选项。

That did the trick. I'm happy.

这就是诀窍。我很高兴。