Author Topic: undefined reference to / file not recognized: File format not recognized error  (Read 6028 times)

Offline Alexandwich

  • Single posting newcomer
  • *
  • Posts: 3
I started a project with multiple files (all are .h or .c) in C. For some reason a function declared in a .h file can't be used in the main file even if the .h is included at the top of the file. The only solution that i found was to link the .h file but it gives me another error:

Quote
.h.gch: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Don't do this or you'll enter the unpleasant lands of precompiled headers.

Undefined reference error means that you have not defined the function that is mentioned in the error message.
This means that you have not placed the code for the function in a .c file that is being compiled and linked.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Alexandwich

  • Single posting newcomer
  • *
  • Posts: 3
I tried including the .c file instead in the main and now the function is usable however it now says
Quote
multiple definition of
so i tried adding a guard but it doesn't change anything

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Including .c files should be done only by people who know what they're doing!
You're not one of them yet, so don't do it.

Have you checked our FAQ and especially this item:
http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28errors%29#Q:_How_do_I_troubleshoot_a_compiler_problem.3F ?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Alexandwich

  • Single posting newcomer
  • *
  • Posts: 3
I changed the extention of the main file (I didn't notice it was called main.cpp and not main.c) now it works thanks for the help anyway