I'm trying to setup SDL2 to use with g++, a text editor and terminal.
I have my SDL2.framework in /Library/Frameworks.
I have a folder on my desktop named testsdl which contains two files:
1) main.cpp
2) makefile
when I type make I get the following error:main.cpp:2:10: fatal error: 'SDL2/SDL.h' file not found.
here is a copy of my makefile
CXX = g++
SDL = -framework SDL2
CXXFLAGS = -Wall -c -std=c++11 -I ~/Library/Frameworks/SDL2.framework/Headers
LDFLAGS = $(SDL) -F /Library/Frameworks -F ~/Library/Frameworks/
EXE = SDL_Lesson0
all: $(EXE)
$(EXE): main.o
$(CXX) $(LDFLAGS) $< -o $#
main.o: main.cpp
$(CXX) $(CXXFLAGS) $< -o $#
clean:
rm *.o && rm $(EXE)
and here is a copy of main.cpp:
#include <iostream>
#include <SDL2/SDL.h>
int main(int, char**)
{
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Quit();
return 0;
}
I tried changing the #include to "SDL2/SDL.h" or just or any other possible combination. I had no problem setting it up through Xcode but can't figure out how to do it without using an IDE.
a second part of the question is:
if I wanted to include the frameworks in the project folder itself so I can later distribute the binaries to people who do not have SDL on their machines how would I do that?
thanks.
You were on the right track, but -F /Library/Frameworks needs to also be in the CXXFLAGS. The resulting commands should look something like:
g++ -Wall -F /Library/Frameworks -c -o main.o main.cpp
g++ main.o -o main -framework SDL2 -I /Library/Frameworks/SDL2.framework/Headers
Here's a simplified makefile that works for me on OSX 10.12.6:
CXX = g++
CXXFLAGS = -Wall -F /Library/Frameworks
LDFLAGS = -framework SDL2 -F /Library/Frameworks -I /Library/Frameworks/SDL2.framework/Headers
all: main
main: main.o
$(CXX) main.o -o main $(LDFLAGS)
obj/main.o : main.cpp
$(CXX) $(CXXFLAGS) -c main.cpp -o main.o
clean:
rm main.o main
if you go into
/Users/path/where/i/unzipped/SDL2-2.0.5
and run
sdl2-config --cflags --libs
it prints out the header include path and library path SDL
The full generic build probably looks something like:
g++ -std=c++11 main.cpp -o main.exe `sdl2-config --cflags` `sdl2-config --libs`
which you can translate into your make file if you like
Related
I want to compile a program with makefile as bellow:
CC = g++
CFLAGS = -g -DGL_GLEXT_PROTOTYPES -I/example_01/glew -I/example_01/glfw-3.2.1 -Wno-deprecated-declarations
LDFLAGS = -lGL -lGLEW -glfw
RM = /bin/rm -f
all: main
main: example_01/src/example_01.o
$(CC) $(CFLAGS) -o as1 example_01/src/example_01.o $(LDFLAGS)
example_00/example_00.o: example_01/example_01.cpp
$(CC) $(CFLAGS) -c example_01/src/example_01.cpp -o example_01/src/example_01.o
clean:
$(RM) *.o example_01/src/*.o as1
But there's error :
fatal error: GLFW/glfw3.h: No such file or directory
#include <GLFW/glfw3.h>
my makefile located in
graphics/example_01
glfw-3.2.1 and glew file located in
graphics/example_01/example_01
glfw3.h located in
graphics/example_01/example_01/glfw-3.2.1/include/GLFW
example_01.cpp located in
graphics/example_01/example_01/src
So how to compile ? Thanks!
inside glfw-3.2.1
inside GLFW
Your include directory is wrong.
CFLAGS = -g -DGL_GLEXT_PROTOTYPES -I/example_01/glew -I/example_01/glfw-3.2.1 -Wno-deprecated-declarations
You should change -I/example_01/glfw-3.2.1 to -I/example_01/glfw-3.2.1/include
It is possible that you'll need to make a similar change to the GLEW include directory.
I have installed SDL through homebrew, and it works perfectly with my test program if I enter the following command directly in the terminal:
g++ -O3 -g -Wall -Wextra -std=c++1y hello.cpp hello_main.cpp `sdl2-config --cflags --libs` -o hello
but unfortunately my attempts to write a makefile (I will definitely need one) have yielded unsuccessful/unexplained results.
I am following this, but my configuration is different/I am not specifying Cocoa (I don't need to) so I expect that the issues I am encountering are probably due in part to my different requirements:
Compiling SDL on OS X with makefile
Example:
CC=g++
CFLAGS=-c -Wall
SDLFLAGS=`sdl-config --cflags --libs` -framework Cocoa
SOURCES=main.cpp Game.cpp IO.cpp Board.cpp Pieces.cpp Piece.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=tetris
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(OBJECTS) $(SDLFLAGS) -o $#
.cpp.o:
$(CC) $(CFLAGS) $< -o $#
clean:
rm -rf *.o $(EXECUTABLE)
My makefile so far:
CXX = g++
CXXFLAGS = -c -O3 -g -Wall -Wextra -std=c++1y
SDLFLAGS = `sdl2-config --cflags --libs`
SOURCES = hello_main.cpp hello.cpp
OBJECTS = $(SOURCES:.cpp=.o)
EXECNAME = hello
all: $(SOURCES) $(EXECNAME)
$(EXECUTABLE): $(OBJECTS)
$(CXX) $ (OBJECTS) $(SDLFLAGS) -o $#
.cpp.o:
$(CXX) $(CXXFLAGS) $< -o $#
clean :
-rm -f *.o *.core $(EXECNAME)
In my .hpp header file if I #include <SDL.h> and run the one-liner command, everything is successful. If I try my makefile above, cannot be found, but if I then change the directive into #include <SDL2/SDL.h> the library is discovered. Yet the console output is the following:
g++ -c -O3 -g -Wall -Wextra -std=c++1y hello.cpp -o hello
which is odd.
Running ./hello yields a "permission denied" error, which confirms that the linking and compilation were not successful.
Everyone's system is a little bit different and the questions I've found so far don't help in this case.
I am very close to having this working (but then again, how would I start using this in an IDE? I suppose that as long as I can import the fixed makefile or build from the terminal/edit only from the IDE, I am fine.)
What changes in the makefile do I need to make?
Thank you.
EDIT:
Variation 1:
CXX = g++
CXXFLAGS = -O3 -g -Wall -Wextra -std=c++1y -c
SDLCFLAGS = `sdl2-config --cflags`
SDLLIBFLAGS = `sdl2-config --libs`
SOURCES = hello_main.cpp hello.cpp
OBJECTS = $(SOURCES:.cpp=.o)
EXECNAME = hello
all: $(SOURCES) $(EXECNAME)
$(EXECUTABLE): $(OBJECTS)
$(CXX) $ (OBJECTS) $(SDLLIBFLAGS) -o $#
.cpp.o:
$(CXX) $(CXXFLAGS) $(SDLCFLAGS) $< -o $#
clean :
-rm -f *.o *.core $(EXECNAME)
I chatted with a friend and figured what was wrong: a bunch of typos and rule oddities. The following works, for anyone out there who needs a basic makefile:
CXX = g++
CXXFLAGS = -O3 -g -Wall -Wextra -std=c++1y
#LDFLAGS = -lSDL2_image
SDLCFLAGS = $(shell sdl2-config --cflags)
SDLLIBFLAGS = $(shell sdl2-config --libs)
SOURCES = hello_main.cpp hello.cpp
OBJECTS = $(SOURCES:.cpp=.o)
EXECNAME = hello
all: $(EXECNAME)
$(EXECNAME): $(OBJECTS)
$(CXX) $(OBJECTS) $(SDLLIBFLAGS) $(LDFLAGS) -o $#
%.o: %.cpp
$(CXX) -c $(CXXFLAGS) $(SDLCFLAGS) $< -o $#
clean :
-rm -f *.o *.core $(EXECNAME)
You should split your sdl2-config into two - as there are two steps. sdl2-config --cflags should go in the compiler step - thats is the .cpp:.o line in your example. The linking step should be sdl2-config --libs then. The second one seems fine for your case, the additional --cflags there does no harm but is not required.
I have been looking this up for a while and there are a bunch of solutions but I don't think they are quite what my problem is,
at the moment I have a folder with some classes for a 'UI' for my client, I have compiled and tested these classes separate to my main project and they all work great, and my current makefile for my project without this new package works fine, but when I add my new package to my project and update my makefile and include the new UI files in my main I get a problem saying that previous classes in my project that I have already tested no longer work, for a better Idea of what I mean here
Terminal:
make -f makeLedger2.mk
gcc -o sqlite3 SQLite/sqlite3.c SQLite/shell.c -lm -lrt -lpthread -ldl
g++ -c rsa/Number.cpp
g++ -c rsa/BigInt.cpp -lm -lrt -lpthread -ldl
g++ -c rsa/Rsa.cpp -lm -lrt -lpthread -ldl
g++ -c database/Entry.cpp
g++ -c rsa/Key.cpp
g++ -c database/PersonalDataBase.cpp -lm -lrt -lpthread -ldl
gcc -o sqlite.o -c SQLite/sqlite3.c -lm -lrt -lpthread -ldl
g++ -c ClientUI/UIOutput.cpp
g++ -c ClientUI/UserCommand.cpp
g++ -c ClientUI/KeyboardController.cpp
g++ -c Network/P2P/Network.cpp -lm -lrt -lpthread -ldl
g++ -c main.cpp -lm -lrt -lpthread -ldl
main.cpp: In function ‘int main()’:
main.cpp:23:11: error: ‘overlay’ was not declared in this scope
main.cpp:23:25: error: expected type-specifier before ‘Network’
main.cpp:23:25: error: expected ‘;’ before ‘Network’
make: *** [main.o] Error 1
The biggest problem here is this is untouched from before I updated my makefile, (i.e. my Network class does not run into this error before I include, or all my code would compile without the addition of this new package 'ClientUI')
my makefile:
CXX = g++
CC = gcc
LIB = -lm -lrt -lpthread -ldl
BIN = SQLite ledger database Network/P2P Control ClientUI
****Added : UIOutput.o UserCommand.o KeyboardController.o***
OBJECTS = Number.o BigInt.o Rsa.o LedgerEntry.o Entry.o Key.o PersonalDataBase.o sqlite.o UIOutput.o UserCommand.o KeyboardController.o Network.o main.o
VPATH = SQLite rsa database Network/P2P Control ClientUI
all : $(BIN)
sqlite3: sqlite3.c shell.c
$(CC) -o $# $^ $(LIB)
ledger: $(OBJECTS)
$(CXX) -o $# $^ $(LIB)
Number.o: rsa/Number.cpp rsa/Number.h
$(CXX) -c rsa/Number.cpp
BigInt.o: rsa/BigInt.cpp rsa/BigInt.h rsa/Number.h
$(CXX) -c rsa/BigInt.cpp$(LIB)
Rsa.o: rsa/Rsa.cpp rsa/Rsa.h rsa/BigInt.h rsa/Number.h
$(CXX) -c rsa/Rsa.cpp $(LIB)
Key.o: rsa/Key.cpp rsa/Key.h rsa/Number.h
$(CXX) -c rsa/Key.cpp
Entry.o: database/Entry.cpp database/Entry.h rsa/Number.h
$(CXX) -c database/Entry.cpp
PersonalDataBase.o: database/PersonalDataBase.cpp database/PersonalDataBase.h SQLite/sqlite3.h database/Entry.h rsa/Key.h
$(CXX) -c database/PersonalDataBase.cpp $(LIB)
*****NEW PACKAGE******
UIOutput.o: ClientUI/UIOutput.h ClientUI/UIOutput.cpp
$(CXX) -c ClientUI/UIOutput.cpp
UserCommand.o: ClientUI/UserCommand.cpp ClientUI/UserCommand.h
$(CXX) -c ClientUI/UserCommand.cpp
KeyboardController.o: ClientUI/KeyboardController.cpp
$(CXX) -c ClientUI/KeyboardController.cpp
*****NEW PACKAGE End******
Network.o: Network/P2P/Network.cpp
$(CXX) -c Network/P2P/Network.cpp $(LIB)
***Include KeyboardController below***
main.o: main.cpp database/PersonalDataBase.h Network/P2P/Network.h ClientUI/KeyboardController.h
$(CXX) -c main.cpp $(LIB)
sqlite.o: sqlite3.c
$(CC) -o $# -c $^ $(LIB)
clean:
rm -f $(BIN)
rm -f $(OBJECTS)
.PHONEY: all, clean
for New Dependencies,
Message includes UIOutput
UserCommand Extends Message (includes)
KeyboardController includes UserCommand
this makefile for jsut these files works (and a test main.cpp for them)
out: UIOutput.o UserCommand.o KeyboardController.o main.o
g++ -o out UIOutput.o UserCommand.o KeyboardController.o main.o
UIOutput.o: UIOutput.h UIOutput.cpp
g++ -c UIOutput.cpp
UserCommand.o: UserCommand.cpp UserCommand.h Message.h UIOutput.h
g++ -c UserCommand.cpp
KeyboardController.o: KeyboardController.cpp KeyboardController.h UserCommand.h Message.h UIOutput.h
g++ -c KeyboardController.cpp
main.o: KeyboardController.h main.cpp
g++ -c main.cpp
main.cpp
#include "Network/P2P/Network.h"
#include "rsa/BigInt.h"
#include "rsa/Number.h"
#include "rsa/Rsa.h"
#include "database/PersonalDataBase.h"
//****Litterally All I do is include it here and I get an issue
//if I commented it out I would be fine and all the .o files would be built, including my new ones, and this can compile
#include "ClientUI/KeyboardController.h"
#include <iostream>
#include <sys/time.h>
#include <stdlib.h>
#include <limits>
using namespace std;
using namespace BigIntOperators;
using namespace RSA;
int main()
{
Network *overlay = new Network("Alice", "LOLO1");
....
.....
I'm doing nothing different to how I import my other packages, and I have compiled all the newly added files and tested them without problem seperatley, furthermore the packages ClientUI shares no dependancies with any other file, except in my main function... what's happening here?
As the title states I'm trying to create a makefile for compiling C++ programs using SDL2 on Windows. I have MinGW installed and working. I'm using Sublime 2 as my environment. Here's what I have so far:
CXX = g++
CXXFLAGS = -std=c++0x -g -O3 -w -Wl,-subsystem,windows
INCLFLAGS = -IC:\Libraries\i686-w64-mingw32\include\SDL2
LDFLAGS = -LC:\Libraries\i686-w64-mingw32\lib -lmingw32 -lSDL2main -lSDL2
OBJECTS = main.o
TARGET = 1_hellosdl
$(TARGET) : $(OBJECTS)
$(CXX) $(INCLFLAGS) $(LDFLAGS) $(CXXFLAGS) -o $(TARGET) $(OBJECTS)
main.o :
clean:
rm -rf $(OBJECTS) $(TARGET)
remake:
clean $(TARGET)
Right now when I compile I get the following error:
g++ -std=c++0x -g -O3 -w -Wl,-subsystems,windows -c -o main.o main.cpp
In file included from main.cpp:1:0:
main.hpp:4:17: fatal error: SDL.h: No such file or directory
#include <SDL.h>
So the issue is that g++ can't find the SDL include file when it tries to compile main.cpp. I get that this is because $(INCLFLAGS) isn't being added to the line under main.o :.
Optimally, I'd like to specify INCLFLAGS implicitly similar to CXXFLAGS and LDFLAGS, but based on this it doesn't look like it's possible.
Is there a way to do this using an implicit variable or, failing that, what's the best alternative? Is there anything else I am doing wrong?
I managed to solve this by moving $(INCLFLAGS) into $(CXXFLAGS):
INCLFLAGS = -IC:\Libraries\i686-w64-mingw32\include\SDL2
CXXFLAGS = $(INCLFLAGS) -std=c++0x -g -O3 -w -Wl,-subsystem,windows
Additionally, I had to move $(LDFLAGS) to the end in order for it to link correctly:
$(TARGET) : $(OBJECTS)
$(CXX) $(CXXFLAGS) -o $(TARGET) $(OBJECTS) $(LDFLAGS)
I have an SDL game I have been working on as my first somewhat real project. I decided to introduce Box2D physics as I was not happy with the collision detection. So I installed it to /usr/local/lib/Box2D and in the folder is Box2D.h and supporting folders. I am using MacVim to code on OSX 10.9.2 to develop, and clang++ compiler from the command line.
In my game code I am just trying to create a simple world to test things out:
#include <Box2D/Box2D.h>
.......
world = new b2World(b2Vec2(0.0,9.81));
My make command finds the library, but errors out trying to build.
$ make clean && make
rm -rf obj bin
clang++ -Wall -c -std=c++11 -I/usr/local/lib src/Ball.cpp -o obj/Ball.o
clang++ -Wall -c -std=c++11 -I/usr/local/lib src/Game.cpp -o obj/Game.o
clang++ -Wall -c -std=c++11 -I/usr/local/lib src/Paddle.cpp -o obj/Paddle.o
clang++ -Wall -c -std=c++11 -I/usr/local/lib src/TextureManager.cpp -o obj/TextureManager.o
clang++ -Wall -c -std=c++11 -I/usr/local/lib src/main.cpp -o obj/main.o
clang++ -framework SDL2 -framework SDL2_image -F /Library/Frameworks -L/usr/local/lib/Box2D obj/Ball.o obj/Game.o obj/Paddle.o obj/TextureManager.o obj/main.o -o bin/game
Undefined symbols for architecture x86_64:
"b2World::b2World(b2Vec2 const&)", referenced from:
Game::init() in Game.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: *** [game] Error 1
And here is my Makefile. Box2D is in /usr/local/lib/Box2D/Box2D.h. I am pretty sure my issue is somewhere in the Makefile.
CXX = clang++
CXXFLAGS = -Wall -c -std=c++11 -I/usr/local/lib
SDL = -framework SDL2 -framework SDL2_image
LDFLAGS = $(SDL) -F /Library/Frameworks -L/usr/local/lib/Box2D
SRC_DIR = src
SOURCES = $(wildcard $(SRC_DIR)/*.cpp)
OBJ_DIR = obj
OBJECTS = $(subst $(SRC_DIR)/, $(OBJ_DIR)/, $(patsubst %.cpp, %.o, $(SOURCES)))
#$(warning $(OBJECTS))
BIN_DIR = bin
EXE = game
# run these no matter what
.PHONY: all clean run
all: $(EXE)
$(EXE): $(OBJECTS)
#mkdir -p $(BIN_DIR)
$(CXX) $(LDFLAGS) $(OBJECTS) -o $(BIN_DIR)/$(EXE)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
#mkdir -p $(OBJ_DIR)
$(CXX) $(CXXFLAGS) $< -o $#
clean:
rm -rf obj bin
run:
./$(BIN_DIR)/$(EXE)
It does not appear that you ever actually link the Box2D library anywhere? You use -L to specify directories to search while linking, but I don't see a -l option to actually link the Box2D library (whatever it's called).
Your output line seems to bear this out:
clang++ -framework SDL2 -framework SDL2_image -F /Library/Frameworks -L/usr/local/lib/Box2D obj/Ball.o obj/Game.o obj/Paddle.o obj/TextureManager.o obj/main.o -o bin/game
You need to get -lbox2d (or whatever the correct name for the Box2D library is) in there.