Reputation: 5257
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.
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.
Upvotes: 3
Views: 38653
Reputation: 51
In the workspace
right click on file containing your main method -> options -> (check) enable both
For other *.c files only (check) compile
For *.h files (check) disable both
Build & Run
Upvotes: 1
Reputation: 1
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!
Upvotes: -2
Reputation: 585
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.
Upvotes: -1
Reputation: 61232
There is a problem with the linker. I cannot link a single header / source file to a source file using #include "sth"
There isn't a problem with the linker. You cannot link header files or sources files. You can only compile source
files (which may #include
header files), to produce object files.
A header file that you #include
can 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).
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: 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.gch
as a linker input.
Presumably you have gone through the special steps
to generate the precompiled header readFileByChars.h.gch
in 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.
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.
Upvotes: 4