0

I am creating a program in c that requires—like most—linking between header files using #include. When I run the main.c file through the terminal i get the following error message:

The command:

gcc main.c -o main.exe

The error message:

/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccxC9Mr4.o:main.c:(.text+0xe): undefined reference to `SomeMethod'.

I have simplyfied the files to make it more readable, but the problem does still occur. The files reside in the same folder, so you'd think that they wouldn't have any problems. No noticable sytax errors either.

Main.c file:

#Include "SomeFile.h"

void main(void){
    SomeMethod();
}

SomeFile header file:

#ifndef FOLDER_SOMEFILE_H_
#define FOLDER_SOMEFILE_H_

int SomeVar;
void SomeMethod(void);

#endif /* FOLDER_SOMEFILE_H_ */

SomeFile file:

#include "SomeFile.h"

void SomeMethod(){}

I expect it to not give the errors, that's about it. I've tried remaking the files and header files and recreating the project folder.

Follow-up question: I wrote the command below which fixed the initial problem presented in this question, but a new problem arose. Now apparently there are occurrences of multiple definitions of variables in the header file(added 'SomeVariable' to the code example to demonstrate this). I think it might be from the header file being called multiple times due to the SomeFile now being included in the gcc command. The command and error message can be seen below.

gcc command:

gcc main.c aaa.c simHandler.c gui.c -o main.exe

error message:

/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccdrnxub.o:SomeFile.c:(.bss+0x0): multiple definition of `SomeVar'; /tmp/cc0y4cs4.o:main.c:(.b
Jay
  • 1
  • 1
  • 2
    "header linker" and "linking between header files using #include." This does not make any sense. (To quote Inigo Montoya from *Princess Bride*: "You keep using that word. I do not think it means what you think it means.") Please state the exact command-line you execute and the exact error message you receive (without "...") – Mike Nakis Apr 11 '23 at 10:52
  • Please add that to the question, as formatted text. In comments it is rather unreadable. – Gerhardh Apr 11 '23 at 10:59
  • How should the linker find `SomeMethod` if you don't tell it to use anything else than `main.c`? You must tell it to use `SomeFile.c` as well. Use `gcc main.c SomeFile.c -o main.exe` – Gerhardh Apr 11 '23 at 11:00
  • 1
    Even before you do as Gerhardh suggests, I can tell what the problem is: you are not including "SomeFile file" in the compilation process. So, it does not get linked either. So, `Main.c` tries to reference `SomeMethod()` but the linker does not know of any `SomeMethod()` because "Somefile file" has not been linked, not even compiled. – Mike Nakis Apr 11 '23 at 11:01
  • @MikeNakis @Gerhardh I have now added the error message and command to the question. Shouldn't it find the `SomeMethod` using the #include? thought that was the whole point of it. – Jay Apr 11 '23 at 11:03
  • @MikeNakis How would I do this that you mention? do I just add SomeMethod.c to the command after main.c? – Jay Apr 11 '23 at 11:05
  • Including a header tells the compiler that somewhere out there is a function called `SomeMethod`. The compiler then know the name, its parameters and return type. The header does not include the defintion – Gerhardh Apr 11 '23 at 11:05
  • It would make a lot of sense if that was the whole point of it, but unfortunately, it is not. That's just how C and C++ are: #include is completely unrelated to linking. #include directives are processed during the compilation step and their use alone does not contain any information that is usable by the linker. – Mike Nakis Apr 11 '23 at 11:07
  • I am not sure how to tell gcc to compile and link multiple files; but it should be fairly easy to look up in gcc's help. – Mike Nakis Apr 11 '23 at 11:14
  • @MikeNakis @Gerhardh The solution you both posed seems to have fixed the initial issue. Some new problems arose which I can only imagine would have something to do with the header file being called twice thus creating multiple instances of the variable `someVar`? I have included it as a follow-up question in the original question query. – Jay Apr 11 '23 at 11:34
  • 1
    That is true. Including the header in both files will result in multiple definitions. You should never define functions or variables in headers. Only provide extern declarations for them and define them in only one C file. – Gerhardh Apr 11 '23 at 11:57
  • 1
    Your follow-up question is unrelated, and this is discouraged on stackoverflow. To do it right, you should open a new question. The quick answer is that your header file needs to declare things `extern` while the file that contains the actual implementations must *not* declare them as extern. – Mike Nakis Apr 11 '23 at 12:00
  • 1
    See [How do I use `extern` to share variables between source files?](https://stackoverflow.com/q/1433204/15168) for the answer to your updated question. But that should be a separate question — except that there is already an answer on SO. – Jonathan Leffler Apr 11 '23 at 13:34

0 Answers0