'Linking SDL in Makefile on mac
I'm trying to learn how to use libraries when writing C code on my mac (not using Xcode). My understanding is that on macs, there is the Library/Frameworks folder where you can put common libraries that can be shared across different projects.
My goal at this point is to use the SDL library to open basic window and do nothing else, but I can't figure out how to utilize libraries on my mac. So to be very specific, I just want to have one file of application code that I have written called main.c and it will have this boilerplate SDL code:
#include "SDL2/SDL.h" // OR #include "SDL.h" (Not sure how the difference in path works)
int SCREEN_HEIGHT = 800;
int SCREEN_WIDTH = 600;
int main() {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow("SDL Game", 0, 0,
SCREEN_HEIGHT, SCREEN_WIDTH, SDL_WINDOW_HIDDEN);
SDL_ShowWindow(window);
SDL_Event event;
int running = 1;
while(running) {
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) {
running = 0;
}
}
SDL_Delay( 32 );
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
I downloaded the development library for mac from the SDL website (https://www.libsdl.org/download-2.0.php) and moved the download to the /Library/Frameworks folder on my machine, just as SDL instructed. However I don't know what to do in my Makefile for the library to be included and linked and then compiled with my main.c file.
Here are some specifics of my laptop/compiler:
MacOS Bug Sur
Version 11.3.1
MacBook Pro (16-inch, 2019)
Processor: 2.3 GHz 8-Core Intel Core i9
Memory: 32 GB 2667 MHz DDR4
Startup Disk: Macintosh HD
Graphics: AMD Radeon Pro 5500M 8 GB
Apple clang version 12.0.5 (clang-1205.0.22.9)
Target: x86_64-apple-darwin20.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Can someone show me what an example of a very simple Makefile would look like to accomplish this goal??
Solution 1:[1]
If you work from the command line or with a properly configured IDE, you link (In GCC) with the -l(library name) flag. The library you want to link needs to be in some directory like /usr/lib, /usr/local/lib or some Libraries folder MacOS might have. If it's not there, then use the -L(path/to/lib/directory) to tell GCC where to find it, and then use -l(library name) flag again.
The library name should start with lib and either end with a .a or .so suffix. So if you wish to link to your own SDL2 library build:
gcc source.c -o mygame -Llibraries/ -lSDL2
Assuming that the library is under libraries/libSDL2.so (Numbers in the end don't matter).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AggelosT |
