Makefile Compilation for an SDL-dependant app fails miserably - c

Here's the makefile:
makefile.win
CC=gcc
CFLAGS=-Wall -ID:\dev\include -LD:\dev\lib -LD:\dev\bin
LIBS=-l mingw32 -l SDLmain -l SDL
TARGET=-mwindows
EXECUTABLE=main.exe
all:
$(CC) $(CFLAGS) $(LIBS) $(TARGET) main.c -o $(EXECUTABLE)
clean:
rm *o
(libSDL and libSDLmain are in D:\dev\lib. SDL.dll is in D:\dev\bin.)
and here's the code
#include <SDL/SDL.h>
int main(int argc, char* argv[])
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
printf("Could not initialize!");
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE|SDL_DOUBLEBUF);
if (!screen) printf("Could not load video!");
int done = 0;
SDL_Event event;
while(!done)
{
while(SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
done = 1;
}
SDL_Flip(screen);
}
SDL_FreeSurface(screen);
printf("Exited cleanly");
return 0;
}
I build it with this command:
mingw32-make -f makefile.win
and mingw32-make translates the makefile into:
gcc -Wall -ID:\dev\include -LD:\dev\lib -LD:\dev\bin -l mingw32 -l SDLmain -l SD
L -mwindows main.c -o main.exe
which is alright.
But then I get all thes charming errors:
main.c:(.text+0x42): undefined reference to `SDL_SetVideoMode'
main.c:(.text+0x7c): undefined reference to `SDL_PollEvent'
main.c:(.text+0x8b): undefined reference to `SDL_Flip'
main.c:(.text+0x9c): undefined reference to `SDL_FreeSurface'
collect2: ld returned 1 exit status
mingw32-make: *** [all] Error 1
So, since I'm linking with mingw32, SDL and SDLmain. And I am adding the directory to the SDL headers. Why am I getting the errors?

You should place the library flags as last:
gcc -o main.exe main.c -lSDL -lSDLmain -lmingw32

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

undefined references when using fftw3.3.6 with threads and float enabled

I installed fftw3.3.6 on my Ubuntu 16.04 to test the performance of using this library with thread and float enabled.
Step 1 :
Installed the library with thread and float and SIMD instructions enabled:`
sudo ./configure --enable-float --enable-generic-simd128 --enable-generic-simd256 --enable-threads
make
make install
Step 2 :
I wrote this code (basing on the manual and tutorial) to compute an fft of 1024 points using 4 threads (complex to complex):
#include <stdio.h>
#include <stdlib.h>
#include <fftw3.h>
#include "input.h"
#define NUMBER_OF_ELEMENTS 1024
#define NUM_THREADS 4
void Load_inputs(fftwf_complex* data)
{
int i;
for(i=0;i<NUMBER_OF_ELEMENTS;i++)
{
data[i][0] = input_data[2 * i];
data[i][1] = input_data[2 * i + 1];
}
}
int main()
{
fftwf_complex array[NUMBER_OF_ELEMENTS];
fftwf_plan p;
int i;
fftwf_init_threads();
fftwf_plan_with_nthreads(NUM_THREADS);
p = fftwf_plan_dft(1,NUMBER_OF_ELEMENTS,array,array,FFTW_FORWARD,FFTW_EXHAUSTIVE);
Load_inputs(array); //function to load input data from input.h file to array[]
fftwf_execute(p);
FILE* res = NULL;
res = fopen("result.txt", "w");
for ( i = 0; i <1024; i++ )
{
fprintf(res,"RE = %f \t IM = %f\n",array[i][0], array[i][1] );
}
fclose(res);
fftwf_destroy_plan(p);
fftwf_cleanup_threads();
}
And then, I compiled this program with this makefile.
CC=gcc
CFLAGS=-g3 -c -Wall -O0 -mavx -mfma -ffast-math
SOURCES=$ test.c
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=test
all: $(TASKMAP) $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(OBJECTS) -o $# -L -lfftw3f_threads -lfftw3f
.c.o:
$(CC) $(CFLAGS) $< -lm -o $#
clean:
rm -fr $(OBJECTS) $(EXECUTABLE)
Compilation errors :
After compilation I've got these errors:
gcc test.o -o test -L -lfftw3f_threads -lfftw3f
//usr/local/lib/libfftw3f.a(mapflags.o): In function `fftwf_mapflags':
mapflags.c:(.text+0x346): undefined reference to `__log_finite'
Makefile:13: recipe for target 'test' failed
//usr/local/lib/libfftw3f.a(trig.o): In function `cexpl_sincos':
trig.c:(.text+0x2c1): undefined reference to `sincos'
//usr/local/lib/libfftw3f.a(trig.o): In function `fftwf_mktriggen':
trig.c:(.text+0x50b): undefined reference to `sincos'
trig.c:(.text+0x653): undefined reference to `sincos'
test.o: In function `main':
/home/anouar/workspace/Thread_example//test.c:27: undefined reference to `fftwf_init_threads'
/home/anouar/workspace/Thread_example//test.c:28: undefined reference to `fftwf_plan_with_nthreads'
/home/anouar/workspace/Thread_example//test.c:40: undefined reference to `fftwf_cleanup_threads'
collect2: error: ld returned 1 exit status
make: *** [test] Error 1
Is there something missing, or something wrong that I have did during installation and compilation?
Read the fine manual. From the sincos() man page:
Link with -lm.
Using -lm in the compile phase of your program is useless:
.c.o:
$(CC) $(CFLAGS) $< -lm -o $#
-lm needs to be in the link stage:
$(EXECUTABLE): $(OBJECTS)
$(CC) $(OBJECTS) -o $# -L -lfftw3f_threads -lfftw3f -lm
In my case, the problems with
undefined reference to `__log_finite'
could be solved by compiling without the -ffast-math option.

Unable to link to ncurses in shared object

For some reason I have having an issue compiling a shared object that uses ncurses. Even though I include and link with -lncurses, compiling the .so file fails. Please advise.
#include <string.h>
#include "../include/mod_curse.h" /* Includes ncurses.h and friends */
int OnConsoleCmd(McapiS *API, ArgS *CmdArgs) /* Just ignore these, they're included in mod_curse.h */
{
if(!strcmp(CmdArgs->Data, "help"))
{
API->BPrintf(STD, "\n-- mod_curse.so --\n");
return 0;
}
}
int OnLoad(McapiS *API, va_list Args)
{
initscr(); /* These are the problems */
}
/* Time to clean up and unload the module */
int OnDeload(McapiS *API, va_list Args)
{
endwin();
}
Here is the Makefile:
CC = clang
CFLAGS = -Wall -fPIC
# Object Files
OBJ = mod_curse.o
# Header Files
INCLUDE = include/mod_curse.h
# Main Module
mod_setup.so: $(OBJ) $(INCLUDE)
$(CC) -shared -Wl,-soname,mod_curse.so,--no-undefined -o ../../mod_curse.so -lncurses $(OBJ)
# Source Files
mod_curse.o: src/mod_curse.c $(INCLUDE)
$(CC) $(CFLAGS) -c src/mod_curse.c
clean:
rm $(OBJ)
Here are the errors:
3 warnings generated.
clang -shared -Wl,-soname,mod_curse.so,--no-undefined -o ../../mod_curse.so -lncurses mod_curse.o
mod_curse.o: In function `OnLoad':
src/mod_curse.c:(.text+0x81): undefined reference to `initscr'
mod_curse.o: In function `OnDeload':
src/mod_curse.c:(.text+0xb1): undefined reference to `endwin'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mod_setup.so] Error 1
I needed to change my make command to have -lncurses appear after $(OBJ).

main.c:(.text+0x25): undefined reference to `reciprocal'

This is a sample program i was trying to compile this below c program to know about the
make file.
main.c
#include<stdio.h>
#include "reciprocal.h"
int main(int argc,char **argv){
int i;
i=atoi(argv[1]);
printf("The Reciprocal of %d is %f\n ",i,reciprocal(i));
return 0;
}
reciprocal.c
#include<stdio.h>
#include<assert.h>
#include "reciprocal.h"
double reciprocal(int i){
assert(i!=0);
return 1.0/i;
}
reciprocal.h
#include<stdio.h>
#ifdef __cplusplus
extern "C"{
#endif
extern double reciprocal(int i);
#ifdef __cplusplus
}
#endif
makefile
CFLAGS:=-o2
reciprocal: reciprocal.o main.o
gcc $(CFLAGS) -o reciprocal.o main.o
main.o: main.c reciprocal.h
gcc $(CFLAGS) -c main.c -I ../include
reciprocal.o: reciprocal.c reciprocal.h
gcc $(CFLAGS) -c reciprocal.c -I ../include
clean:
rm -f *.o reciprocal
when compiled as below it throws an error.
% make
gcc -o2 -c reciprocal.c -I ../include gcc -o2 -c main.c -I ../include
gcc -o2 -o reciprocal.o main.o main.o: In function main':
main.c:(.text+0x25): undefined reference toreciprocal' collect2: ld
returned 1 exit status make: * [reciprocal] Error 1
Please help me understand what is the reason for this error.
Change your makefile:
reciprocal: reciprocal.o main.o
gcc $(CFLAGS) -o reciprocal reciprocal.o main.o
^^^^^^^^^^
Alternatively:
reciprocal: reciprocal.o main.o
gcc $(CFLAGS) -o $# $^
You have an insidious typo:
CFLAGS:=-o2
That should have been -O2 with a capital O, this way you redirect the output of every compilation to the file 2.

Resources