0

I'm attempting to make a custom header in C for user input. Whenever I try to use it, it throws the error:

"Undefined Reference to (FUNCTION_NAME)"

It's in the same folder as all the other header files and vscode doesn't have a problem finding the header file. Both the .h and .c are in the same folder.

Here is the code in the header, c file, and program I'm using:

SGGINPUT.h

#ifndef SGGINPUT
#define SGGINPUT
#include <stdio.h>

char SGGchar();

#endif

SGGINPUT.c

#include <stdio.h>
#include "SGGINPUT.h"

char SGGchar() {
        char c;
        printf("Enter Char: ");
        scanf("%c",&c);
        printf("\n");
        return c;
}

test.c

#include <stdio.h>
#include "SGGINPUT.h"

int main() {
    char c = SGGchar();
    printf("%c",c);
}
Setaganga
  • 3
  • 5
  • That's a linker error. So it means the include is fine. Please show the exact build commands. Seems you are not linking all the `.o` files that are compiled from the `.c` files. – kaylum May 28 '22 at 03:18
  • 1
    I cannot *fathom* the search for [`[c] undefined reference`](https://stackoverflow.com/search?q=%5Bc%5D+undefined+reference) yielded no results worthy of solving this. – WhozCraig May 28 '22 at 03:19
  • I have tried ```gcc SGGINPUT.c test.c``` but it shows: ```cc1.exe: fatal error: SGGINPUT.c: No such file or directory```. I'm also not familiar with .o files, what are they? – Setaganga May 28 '22 at 03:21
  • And @WhozCraig believe me, I have searched for answers far and wide, but none of them have helped so far – Setaganga May 28 '22 at 03:22
  • If the compiler can't find the file then it's not where you think it is, isn't named what you think it is, or possibly you do not have permission to access it. What platform are you on? Have you tried renaming the file to something else? – Retired Ninja May 28 '22 at 03:27
  • @RetiredNinja I'm pretty sure I have permissions. I am on windows, and no I have not. However I have tried what kaylum said and followed this: https://stackoverflow.com/questions/68214647/how-do-i-link-two-header-files-and-3-c-files-into-a-single-executable-in-c-progr – Setaganga May 28 '22 at 03:30
  • Are you sure that your `#include "SGGinput.h"` isn't case sensitive? When I compile the code with clang, I get this warning suggesting case-sensitivity could be the problem: `test.c:2:10: warning: non-portable path to file '"SGGINPUT.h"'; specified path differs in case from file name on disk [-Wnonportable-include-path]` – Michael M. May 28 '22 at 03:30
  • What does `ls -l SGGINPUT.c` show? It would be productive for you to do basic troubleshooting and show that to us. – kaylum May 28 '22 at 03:30
  • I found out an answer. I dunno if I can flag it as solved for a comment, but https://stackoverflow.com/questions/68214647/how-do-i-link-two-header-files-and-3-c-files-into-a-single-executable-in-c-progr helped me solve it. Granted I usually run it with a vscode extension, but using it in a executable file still works. Thanks everyone for your help – Setaganga May 28 '22 at 03:32

2 Answers2

0

Try:

gcc -c SGGINPUT.h SGGINPUT.c

A result file will be an obj file:

SGGINPUT.o

Next compile it this way:

gcc -g SGGINPUT.o test.c -o "Output_name"

Plus you don't really need to include <stdio.h> in your header, you are just declaring your functions there and nothing more.

David Eden
  • 28
  • 6
-1

My solution was:

gcc -c test.c
gcc -c 'C:\msys64\mingw64\include\SGGINPUT.c'
gcc -o test test.o SGGINPUT.o

And then run the executable.

So this solution works if I were to make o files in the same directory as test.c, but I really want it to function like <stdio.h> or any other default header.

Setaganga
  • 3
  • 5
  • The `.c` file is not a header. Why do you put it into a `include` directory? You initially said all the source files were in the same directory. If that were true then `gcc SGGINPUT.c test.c` should work. – kaylum May 28 '22 at 03:42
  • Well I had the header and c file in 'C:\msys64\mingw64\include\SGGINPUT.c' but my test.c somewhere else. I meant that the SGGINPUT.h and SGGINPUT.c were in the same directory. My bad on not specifying that. – Setaganga May 28 '22 at 03:44
  • And I am trying to have it become a default header, so that I don't have to compile it each time like so. – Setaganga May 28 '22 at 03:46
  • Also @kaylum after you make the executable, can you delete the o files or are they still required to run? – Setaganga May 28 '22 at 04:11
  • Try it. That's a much better way to learn than ask. But before you do, why do you think they are, or are not, required? – Cheatah May 28 '22 at 04:22
  • I tried it and I learned: no, they aren't required. My main expertise is in python, and the making of an exe is a foreign concept to me, so I honestly wouldn't have known. – Setaganga May 28 '22 at 04:35
  • "*And I am trying to have it become a default header, so that I don't have to compile it each time like so*" That does not makes sense. A header is very much for compiling. If you don't want to compile something then there is no need for any headers. It's not clear what you are trying to do or avoid. – kaylum May 28 '22 at 05:17
  • I want it to function like stdio.h, so that it doesn’t need compiling. Is that something I can do? – Setaganga May 29 '22 at 07:46
  • SGGINPUT.h and SGGINPUT.c are both in the C “include” directory – Setaganga May 29 '22 at 07:50