Undefined reference to function when included w/ header - c

I've been confused as to why this specific error is coming up.
The function being called looks the same so I don't think it is a type/case-sensitive error. I've included my makefile, the header in question, and the code snippet of the C file using the header + calling the function.
If anything else would be relevant I could supply it.
Thanks to anyone that helps!
game_state.h
#ifndef GAME_STATE_H
#define GAME_STATE_H
typedef struct Game_Condition_struct {
bool vertical_win;
bool down_diag_win;
bool up_diag_win;
char* player_char;
} GAME;
void Game_Over(BOARD* board);
void Win_Check(BOARD* board, GAME* game_condition);
#endif
moves.c snippet
#include "game_state.h"
... // other code above
else {
Make_Move(board, user_move, player_char);
Print_Board(board);
// IF-ELSE to change the player turn
if (*player_char == 'X') {
*player_char = 'O';
}
else {
*player_char = 'X';
}
Game_Over(board);
}
game_state.c
void Game_Over(BOARD* board) {
GAME game_condition;
Win_Check(board, &game_condition);
}
makefile
connectn.out: board.o main.o read_args.o moves.o game_state.o
gcc -g -Wall -o connectn.out board.o main.o read_args.o moves.o
main.o: board.h read_args.h moves.h game_state.h main.c
gcc -g -Wall -c -o main.o main.c
board.o: board.h board.c
gcc -g -Wall -c -o board.o board.c
read_args.o: read_args.h read_args.c
gcc -g -Wall -c -o read_args.o read_args.c
moves.o: moves.h board.h game_state.h moves.c
gcc -g -Wall -c -o moves.o moves.c
game_state.o: game_state.h board.h game_state.c
gcc -g -Wall -c -o game_state.o game_state.c
clean:
rm *.o *.out
The code works as I expect if I don't include the Game_Over(board) call, so I'm confused as to why it's not defined.
BOARD is a struct I made, similar to GAME.

You didn't link game_state.o into connectn.out. Add game_state.o to the end of the 2nd line of Makefile.

Related

SDL file not found while executing Makefile

Im working with C on MacOS, when i compile the program by myself with
gcc main.c -o prog $(sdl2-config --cflags --libs)
It works fine, but when i try to make it work with a makefile i keep facing this error
gcc -o main.o -c main.c prog
clang: warning: prog: 'linker' input unused [-Wunused-command-line-argument]
main.c:1:10: fatal error: 'SDL.h' file not found
#include <SDL.h>
There is my code
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdbool.h>
int main (int argc, char **argv)
{
SDL_Window *window = NULL;
if ( SDL_Init(SDL_INIT_VIDEO) != 0)
{
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
exit(EXIT_FAILURE);
}
window = SDL_CreateWindow("Bomberman", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_MINIMIZED);
if (window == NULL)
{
SDL_Log("Unable to create window: %s", SDL_GetError());
exit(EXIT_FAILURE);
}
bool window_open = true;
while (window_open)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
window_open = false;
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
And here is my makefile
main.o: main.c
gcc -o main.o -c main.c prog $(sdl2-config --cflags --libs)
Well, look at the command you ran from the compile line:
gcc main.c -o prog $(sdl2-config --cflags --libs)
This builds an output prog from the source file. And compare it to your recipe:
gcc -o main.o -c main.c prog $(sdl2-config --cflags --libs)
^^^^^^^^^^^^
They aren't the same, so clearly you won't get the same result. Plus you've told make you're trying to build the output file main.o, not prog as your command line version does.
First, remove the extra stuff and fix the target.
Second, $ is special to make (it introduces a make variable). So in your makefile recipe $(sdl2-config --cflags --libs) is actually expanding a very oddly-named make variable.
You want this:
prog: main.c
gcc main.c -o prog $$(sdl2-config --cflags --libs)
Make already defines a builtin rule for building .o files from .c files with the same base name. You should use that and the standard FLAGS variables. So you end up wanting something like:
CFLAGS = $$(sdl2-flags --cflags)
LDLIBS = $$(sdl2-flags --libs)
With just that, you can type make main.o to compile main.o and make main to build an executable main from main.o (if it exists) or main.c (if there's no main.o already)
If you want to build prog, you can add a rule
prog: main.o
$(CC) $(LDFLAGS) -o $# $^ $(LDLIBS)

Did a Mac update ruin my ability to use gcc?

I am trying to compile two c files into one executable. In the directory I have only three files; Makefile, main.c and myfunction.c.
Makefile:
CC = gcc
CFLAGS = -Wall -g -O0
LIBS = -lm
SRCS = $(wildcard *.c)
OBJS = $(SRCS:.c=.o)
MAIN = main
all: $(MAIN)
#echo Program has been compiled
$(MAIN): $(OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LIBS)
clean:
$(RM) *.o *~ $(MAIN)
main.c:
#include <stdio.h>
void myfunc();
int main (int argc, char *argv[]) {
myfunc();
return 0;
}
myfunction.c:
#include <stdio.h>
void myfunc() { printf("hello world"); }
output after make:
gcc -Wall -g -O0 -c -o main.o main.c
gcc -Wall -g -O0 -c -o myfunction.o myfunction.c
gcc -Wall -g -O0 -o main main.o myfunction.o -lm
Undefined symbols for architecture x86_64:
"_myfunc", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1
I had something nearly identical working in the past. I have since clean installed MacOS and updated to Big Sur. Is this the issue or have I overlooked something?
I fixed the issue. I’m not sure what part fixed it, but installed Homebrew and used it to install gcc-10. I also deleted the project and started over.
myfunc would define like file header
myfunc.h
void myfunc()
Declare in another file
myfunc.c
void myfunc() { printf("hello world"); }
Follow the following tutorial
https://developer.gnome.org/anjuta-build-tutorial/stable/build-make.html.en

compiler shouts an error with the make file

After a certain attempt to write a simple program with a main and one function,
I ask for your help to find the bug. I include the 3 files that are in action:
the main function in base.c
the function in fun.c
the makefile
The compiler says that the function is called in a bad way in the main:
undefined reference to `fun'
base.c
#include <stdio.h>
int fun(char c);
main()
{
printf("please enter a single char\n");
char c=getchar();
fun(c);
return 0;
}
fun.c
#include <stdio.h>
int fun(char c)
{
printf("%d3 is the value of your char!\n", 'c');
return 0;
}
makefile
charprint: base.o fun.o
gcc -g -Wall -ansi base.o fun.o -o charprint
base.o: base.c
gcc -g -Wall -ansi base.c -o base.o
fun.o: fun.c
gcc -g -Wall -ansi fun.c -o fun.o
The compilation problem is that you forgot the -c flags in the compiler line for base.o and fun.o. One obvious simple (but not very good) way to fix that is:
charprint: base.o fun.o
gcc -g -Wall -ansi base.o fun.o -o charprint
base.o: base.c
gcc -c -g -Wall -ansi base.c -o base.o
fun.o: fun.c
gcc -c -g -Wall -ansi fun.c -o fun.o
Interestingly, the simplest fix would be to delete the two compiler command for the two object files — make knows how to compile C files to object files. You could set CFLAGS += -Wall (or CFLAGS = -Wall) to get the (very important) -Wall flag included. Adding -Werror too would be good.
CFLAGS += -Wall -Werror -g -std=c11
charprint: base.o fun.o
${CC} ${CFLAGS} base.o fun.o -o $#
base.o: base.c
fun.o: fun.c
In the code for fun(), your argument to printf() should be just c and not 'c'. You probably also mean %3d rather than %d3 though that 'works'; it just doesn't do what you expect. Note that you should use an explicit int main(void) { … }. And you should probably create a header fun.h containing:
extern int fun(char c);
and #include "fun.h" in both source files, and add fun.h after the source file name in the dependency lines in the makefile:
CFLAGS += -Wall -Werror -g -std=c11
charprint: base.o fun.o
${CC} ${CFLAGS} base.o fun.o -o $#
base.o: base.c fun.h
fun.o: fun.c fun.h
In fact, you don't need to list the source files as dependencies for the object files; make will infer that dependency automatically. But you do need to specify the header file dependency.

undefined referance to function included in header

I would apreciate any insights to a couple of linker and compiler issues I have
I have main.c file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "assemble.h"
char *fileName;
FILE *file;
int main(int argc, char *argv[]){
char inputFile[MAX_INPUT];
int i, flag = TRUE;
for(i=1; i<argc; i++){
fileName = argv[i];
strcpy(inputFile,argv[i]);
file = fopen(inputFile,"r");
}
flag = assemble(file, fileName); //****this is the problam line****
if(!flag)
printf("Errors found, compilation aborted\n");
else
printf("File %s compiled\n", fileName);
fclose(file);
return 0;
}
In the header assemble.h I have the decleration:
int assemble(FILE *file, char *fileName);
The first issue is in main.c I get undefined referance to 'assemble', id returned 1 exit status
The second issue is in assemble.h I get FILE was not declared in this scope.
Does anyone know what causes these errors and what is the fix?
Thanks in advance
[EDIT]: the makefile.
assembler: main.o assemble.o functions.o
gcc -Wall -ansi -pedantic main.o assemble.o functions.o -o assembler -lm
main.o: main.c assemble.c
gcc -Wall -ansi -pedantic -c main.c assemble.c -lm
assemble.o: assemble.c
gcc -Wall -ansi -pedantic -c assemble.c -lm
functions.o: functions.c
gcc -Wall -ansi -pedantic -c functions.c -lm
clean:
rm -f assembler
rm -f *.o
You need to compile both files and link them together.
gcc main.c assemble.c
Or compile them separately to object files and link them.
gcc -c main.c
gcc -c assemble.c
gcc main.o assemble.o

How to include helper functions in C?

There are 4 files:
helper.h //contains the signatures of functions in helper.c
helper.c //implements the signatures in helper.h
file.h //has all the includes needed to run file.h
file.c //this file includes file.h and helper.h
In file.c, I need to use the function that is defined in helper.c in my main function. However, file.c is saying that there is an undefined reference to 'func_found_in_helper.c'
Is this structure correct?
Yes, provided file.c contains
#include "helper.h"
and when building your program you link together helper.o and file.o.
You also need to ensure you compile each of the files with -c so that the compiler only compiles (and not links); do the link later with all the object files.
Here's a working example (I don't actually need a main.h but if you have one of those, #include it from main.c):
main.c
#include <stdio.h>
#include <stdlib.h>
#include "helper.h"
int
main (int argc, char **argv)
{
test ();
exit (0);
}
helper.c
#include <stdio.h>
void
test ()
{
printf ("Hello world\n");
}
helper.h
void test ();
To compile
gcc -Wall -Werror -c -o main.o main.c
gcc -Wall -Werror -c -o helper.o helper.c
To link
gcc -Wall -Werror -o test main.o helper.o
In a Makefile
test: main.o helper.o
gcc -Wall -Werror -o test main.o helper.o
%.o: %.c
gcc -c -Wall -Werror -o $# $<
clean:
rm -f *.o test
To run
$ ./test
Hello world
It's a bit difficult to tell what else might be wrong without the program; my guess is you simply forgot the -c flag to gcc, or forgot to link in helper.o.
undefined reference to 'func_found_in_helper.c'
That's a little odd, as it suggests you have tried to call the function using the '.c' extension, rather than just the function name. Maybe the '.' is just a typo in the question ?
Also a linker will flag an undefined symbol, so it may also be that you have not told the linker where to find helper.o ( the helper.c file compiled to the an object file ). The compiler will start the linker automatically. Did you compile helper.c first ?

Resources