Code::Blocks error: ld returned 1 exit status

31,058

Solution 1

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.

Solution 2

In the workspace

  1. right click on file containing your main method -> options -> (check) enable both

  2. For other *.c files only (check) compile

  3. For *.h files (check) disable both

Build & Run

Share:
31,058
KeyC0de
Author by

KeyC0de

Updated on July 09, 2022

Comments

  • KeyC0de
    KeyC0de almost 2 years

    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 :

    enter image description here

    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.

  • ProgMasta
    ProgMasta over 2 years
    I think this is a more complicated way of saying what I was doing incorrectly. Maybe? For me the error wasn't giving me any additional info just "ld returned 1 exit status". I finally determined that I was trying to instantiate a variable after the .h file but before the .cpp. The message wasn't telling which file was the issue so I thought it was a compiler issue. I think I was misunderstanding what "precompiled" meant.