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)
CodePudding user response:
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)
CodePudding user response:
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)