C语言 代码::块错误:ld 返回 1 个退出状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42874506/
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
Code::Blocks error: ld returned 1 exit status
提问by Nikos
I'm using Code::Blocks v 16.1.0.0 along with the MINGW that came with it. There is a problem with the linker. I cannot link a single header / source file to a source file using #include "sth". To narrow the problem down i only have 1 source and 1 header file in my project, but i cannot bypass this error no matter what files i use and options i try.
我正在使用 Code::Blocks v 16.1.0.0 以及随附的 MINGW。链接器有问题。我无法将单个头文件/源文件链接到使用#include "sth". 为了缩小问题的范围,我的项目中只有 1 个源文件和 1 个头文件,但是无论我使用什么文件和尝试什么选项,我都无法绕过这个错误。
This is the build log
这是构建日志
-------------- Build: Debug in MISC (compiler: GNU GCC Compiler)---------------
gcc.exe -Wall -Wextra -Wall -g -std=c99 -c C:\Users\username\Documents\CodeBlocks\C\MISC\readFileByChars.c -o obj\Debug\readFileByChars.o
g++.exe -LC:\Users\username\Documents\CodeBlocks\C\MISC -o bin\Debug\MISC.exe obj\Debug\readFileByChars.o readFileByChars.h.gch
readFileByChars.h.gch: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))
This is the toolchain directories :
这是工具链目录:
I don't have any previous instances of programs running. I also have working MINGW standalone (without including its bin folder in the environment variables not to confuse codeblocks during build), but for codeblocks i include the prepackaged one that came with its installation. When i click the option to link a header file in my project the project won't build (but if i don't link the file how can i build my application?). I repeat this project is empty, i have only one header and only one source file included. I have seen other similar questions about this on here but their solutions did not work. Help will be appreciated. Thank you.
我没有任何以前运行的程序实例。我也有独立的 MINGW 工作(不将其 bin 文件夹包含在环境变量中,以免在构建过程中混淆代码块),但对于代码块,我包含了安装时附带的预打包代码块。当我单击在我的项目中链接头文件的选项时,该项目将不会构建(但如果我不链接该文件,我如何构建我的应用程序?)。我再说一遍,这个项目是空的,我只有一个标题和一个源文件。我在这里看到了其他类似的问题,但他们的解决方案不起作用。帮助将不胜感激。谢谢你。
回答by Mike Kinghan
There is a problem with the linker. I cannot link a single header / source file to a source file using #include "sth"
链接器有问题。我无法使用 #include "sth" 将单个头文件/源文件链接到源文件
There isn't a problem with the linker. You cannot link header files or sources files. You can only compilesource
files (which may #includeheader files), to produce object files.
链接器没有问题。您不能链接头文件或源文件。您只能编译源文件(可能是#include头文件)以生成目标文件。
A header file that you #includecan be a precompiled header file,
for compilers that support this concept, subject to compiler-specific restrictions
on its use (and despite the name, a precompiled header file is not compiled: it is not
an object file).
一个头文件,你#include可以是一个预编译头文件,对于支持这个概念的编译器,它的使用受到编译器特定的限制(尽管有这个名字,预编译的头文件没有被编译:它不是一个目标文件)。
The linker can only link object files and dynamic libraries to produce an executable. It can consume object files either directly or extract them from a static library.
链接器只能链接目标文件和动态库以生成可执行文件。它可以直接使用目标文件,也可以从静态库中提取它们。
Your failing linkage command:
您失败的链接命令:
g++.exe -LC:\Users\username\Documents\CodeBlocks\C\MISC -o bin\Debug\MISC.exe obj\Debug\readFileByChars.o readFileByChars.h.gch
shows that you are attempting to link a precompiled header readFileByChars.h.gch. The linker says:
表明您正在尝试链接预编译头readFileByChars.h.gch。链接器说:
readFileByChars.h.gch: file not recognized: File format not recognized
because a precompiled header is not an object file or a static or dynamic library. It is not something the linker understands.
因为预编译头不是目标文件,也不是静态或动态库。这不是链接器理解的东西。
Correct your project options so that you are no longer passing readFileByChars.h.gchas a linker input.
更正您的项目选项,以便您不再readFileByChars.h.gch作为链接器输入传递。
Presumably you have gone through the special stepsto generate the precompiled header readFileByChars.h.gchin your Code::Blocks project.
If you have followed that documentation correctly, there is nothing else you need to do that the documentation
does not mention. Your other project options do not need to tell the compiler or linker anything
about the precompiled header.
想必您已经完成了在 Code::Blocks 项目中
生成预编译头文件的特殊步骤readFileByChars.h.gch。如果您正确地遵循了该文档,则无需执行文档中未提及的任何其他操作。您的其他项目选项不需要告诉编译器或链接器有关预编译头的任何信息。
It is not necessary to use precompiled headers at all and, as you see, their correct use is not foolproof, and is compiler-specific. I would suggest you build this and other projects in the ordinary way, without precompiled headers, until and unless you are facing obstructively long compilation times, which a precompiled header might usefully curtail.
根本没有必要使用预编译头文件,正如您所见,它们的正确使用并非万无一失,而是特定于编译器的。我建议你以普通的方式构建这个和其他项目,没有预编译头,直到并且除非你面临阻塞性很长的编译时间,预编译头可能会有效地缩短编译时间。
回答by Nizam Uddin Sikder
Here how I solved this issue: 1. If you run the file from a project exit codeblocks first. 2. Open the file separately(Do not open codeblocks project file!) and run.
我是如何解决这个问题的: 1. 如果您首先从项目退出代码块运行文件。2.单独打开文件(不要打开codeblocks项目文件!)并运行。
回答by Threnx
Real answer: 1. open task manager( ctrl + shift + esc )
2. go to details
3. search the project name and end task ( there will be a .exe with the project's name)
Done!
真实答案: 1. 打开任务管理器( ctrl + shift + esc )
2. 进入详细信息
3. 搜索项目名称和结束任务(会有一个带有项目名称的 .exe)
完成!


