Make dynamic library work on macos

This commit is contained in:
zv0n 2020-11-22 21:58:37 +01:00
parent 01ac90dbfc
commit afc836902b
5 changed files with 46 additions and 4 deletions

1
.gitignore vendored
View File

@ -3,6 +3,7 @@
*.o *.o
*.so *.so
*.so.* *.so.*
*.dylib
demo demo
test test
tetris/tetris tetris/tetris

View File

@ -1,12 +1,24 @@
CXX ?= g++ CXX ?= g++
CPPFLAGS = -std=c++14 -Wall -Wextra -pedantic CPPFLAGS = -std=c++14 -Wall -Wextra -pedantic
LIBRARYFLAGS = -fPIC
MAJOR ?= 1 MAJOR ?= 1
MINOR ?= 0 MINOR ?= 0
RELEASE ?= 0 RELEASE ?= 0
ifeq ($(OS),Windows_NT)
UNAME_S := Windows
else
UNAME_S := $(shell uname -s)
endif
ifeq ($(UNAME_S),Linux)
SDLPPLIBRARY = libsdlpp.so.${MAJOR}.${MINOR}.${RELEASE} SDLPPLIBRARY = libsdlpp.so.${MAJOR}.${MINOR}.${RELEASE}
LIBRARYFLAGS = -fPIC
endif
ifeq ($(UNAME_S),Darwin)
SDLPPLIBRARY = libsdlpp.dylib
LIBRARYFLAGS =
endif
OBJECTFILES = sdlpp_circlecolider.o sdlpp_circlerenderer.o sdlpp_collision.o sdlpp_common.o sdlpp_font.o sdlpp_linerenderer.o sdlpp_rectcolider.o sdlpp_rectrenderer.o sdlpp_renderer.o sdlpp_renderobject.o sdlpp_scene.o sdlpp_textrenderer.o sdlpp_texture.o sdlpp_window.o OBJECTFILES = sdlpp_circlecolider.o sdlpp_circlerenderer.o sdlpp_collision.o sdlpp_common.o sdlpp_font.o sdlpp_linerenderer.o sdlpp_rectcolider.o sdlpp_rectrenderer.o sdlpp_renderer.o sdlpp_renderobject.o sdlpp_scene.o sdlpp_textrenderer.o sdlpp_texture.o sdlpp_window.o
@ -15,13 +27,20 @@ all: ${SDLPPLIBRARY}
%.o: %.cpp %.o: %.cpp
${CXX} ${CPPFLAGS} ${LIBRARYFLAGS} -c $< -o $@ ${CXX} ${CPPFLAGS} ${LIBRARYFLAGS} -c $< -o $@
ifeq ($(UNAME_S),Linux)
${SDLPPLIBRARY}: ${OBJECTFILES} ${SDLPPLIBRARY}: ${OBJECTFILES}
${CXX} ${CPPFLAGS} -shared -Wl,-soname,libsdlpp.so.${MAJOR}\ ${CXX} ${CPPFLAGS} -shared -Wl,-soname,libsdlpp.so.${MAJOR}\
-o ${SDLPPLIBRARY} $^ -o ${SDLPPLIBRARY} $^
ln -sf ${SDLPPLIBRARY} libsdlpp.so ln -sf ${SDLPPLIBRARY} libsdlpp.so
ln -sf ${SDLPPLIBRARY} libsdlpp.so.${MAJOR} ln -sf ${SDLPPLIBRARY} libsdlpp.so.${MAJOR}
endif
ifeq ($(UNAME_S),Darwin)
${SDLPPLIBRARY}: ${OBJECTFILES}
${CXX} ${CPPFLAGS} -dynamiclib -install_name ${SDLPPLIBRARY}\
-current_version ${MAJOR}.${MINOR} $^ -lSDL2 -lSDL2_image -lSDL2_gfx -lSDL2_ttf -o $@
endif
clean: clean:
${RM} *.so* *.o ${RM} *.so* *.o *.dylib
.PHONY: all clean test .PHONY: all clean test

View File

@ -1,3 +1,4 @@
#include <cmath>
#include "sdlpp_linerenderer.hpp" #include "sdlpp_linerenderer.hpp"
namespace SDLPP { namespace SDLPP {

View File

@ -1,3 +1,4 @@
#include <cmath>
#include "sdlpp_rectrenderer.hpp" #include "sdlpp_rectrenderer.hpp"
namespace SDLPP { namespace SDLPP {

View File

@ -3,11 +3,28 @@ CFLAGS ?= -O2 -Wall -Wextra -std=c++14
PREFIX ?= /usr/local/bin PREFIX ?= /usr/local/bin
LDFLAGS ?= -lSDL2 -lSDL2_image -lSDL2_gfx -lSDL2_ttf -pthread LDFLAGS ?= -lSDL2 -lSDL2_image -lSDL2_gfx -lSDL2_ttf -pthread
ifeq ($(OS),Windows_NT)
UNAME_S := Windows
else
UNAME_S := $(shell uname -s)
endif
TETRIS_OBJECTS = tetris.o scenes.o config.o functions.o global_vars.o
ifeq ($(UNAME_S),Linux)
TETRIS_OBJECTS += libsdlpp.so
SDLLIB = libsdlpp
endif
ifeq ($(UNAME_S),Darwin)
TETRIS_OBJECTS += libsdlpp.dylib
SDLLIB = sdlpp
endif
.PHONY: default .PHONY: default
default: tetris default: tetris
tetris: tetris.o scenes.o config.o functions.o global_vars.o libsdlpp.so tetris: ${TETRIS_OBJECTS}
$(CXX) $(CFLAGS) -o $@ $^ ${LDFLAGS} -L -llibsdlpp $(CXX) $(CFLAGS) -o $@ $^ ${LDFLAGS} -L $(shell pwd) -l${SDLLIB}
tetris.o: tetris.cpp ../sdlpp/sdlpp.hpp config.hpp custom_classes.hpp scenes.hpp global_vars.hpp functions.hpp tetris.o: tetris.cpp ../sdlpp/sdlpp.hpp config.hpp custom_classes.hpp scenes.hpp global_vars.hpp functions.hpp
$(CXX) $(CFLAGS) -c -o $@ $< $(CXX) $(CFLAGS) -c -o $@ $<
@ -23,6 +40,9 @@ libsdlpp.so: ../sdlpp
$(MAKE) -C ../sdlpp $(MAKE) -C ../sdlpp
cp ../sdlpp/libsdlpp.so . cp ../sdlpp/libsdlpp.so .
ln -sf libsdlpp.so libsdlpp.so.1 ln -sf libsdlpp.so libsdlpp.so.1
libsdlpp.dylib: ../sdlpp
$(MAKE) -C ../sdlpp
cp ../sdlpp/libsdlpp.dylib .
start: start:
LD_LIBRARY_PATH=$$(pwd) ./tetris LD_LIBRARY_PATH=$$(pwd) ./tetris