This is a pretty noob question. Basically I can't seem to be able to compile a basic Hello World program on OSX (Yosemite) using the SDL2 external library.
I'm trying to do this on console, without the help of any IDEs. I already installed the SDL 2.0.3 and it is located on the /Library/Frameworks/SDL2.framework path.
My main file looks like this:
#include <SDL2/SDL.h>
#include <stdio.h>
bool init();
void close();
SDL_Window* gameWindow = NULL;
SDL_Surface* gameScreenSurface = NULL;
bool init()
{
...
}
void close()
{
...
}
int main( int argc, char** argv)
{
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
SDL_Delay( 2000 );
}
close();
return 0;
}
And I also have a makefile (taken from an example I found somewhere) that looks like this:
CC = g++
LDFLAGS = -g -Wall
PROGNAME = doom
SOURCES = main.cpp
INCLUDES =
OBJECTS = $(subst %.cc, %.o, $(SOURCES))
ROOTCFLAGS := $(shell root-config --cflags)
ROOTLIBS := $(shell root-config --libs)
ROOTGLIBS := $(shell root-config --glibs)
ROOTLIBS := $(shell root-config --nonew --libs)
CFLAGS += $(ROOTCFLAGS)
LIBS += $(ROOTLIBS)
all: doom
$(PROGNAME): $(OBJECTS)
$(CC) $(LDFLAGS) -o doom $(OBJECTS) $(LIBS)
%.o : %.cc $(INCLUDES)
$(CC) ${CFLAGS} -c -g -o $# $<
And that's about it. When I run make, I receive this response:
make: root-config: Command not found
make: root-config: Command not found
make: root-config: Command not found
make: root-config: Command not found
g++ -g -Wall -o doom main.cpp
Undefined symbols for architecture x86_64:
"_SDL_CreateWindow", referenced from:
init() in main-8b6fae.o
"_SDL_Delay", referenced from:
_main in main-8b6fae.o
"_SDL_DestroyWindow", referenced from:
close() in main-8b6fae.o
"_SDL_GetError", referenced from:
init() in main-8b6fae.o
"_SDL_GetWindowSurface", referenced from:
init() in main-8b6fae.o
"_SDL_Init", referenced from:
init() in main-8b6fae.o
"_SDL_Quit", referenced from:
close() in main-8b6fae.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: *** [doom] Error 1
So, can I get some guidance please? I'm a little lost on where to start. I have never compiled a program on OSX or any Unix-based OS before.
I searched for that root-config thing that I'm missing, and It seems that I have to install a library called Root. I did it. Uncompressed it on a directory, don't know where to go from there.
Thanks in advance.
The makefile you found has variables for the ROOT data analysis framework, and not for SDL2.
Try running
g++ $(sdl2-config --cflags) -g -Wall -o doom main.cpp $(sdl2-config --libs)
to get started.
As amitp said, you are trying to use compiler and linker flags for the ROOT framework. Try this instead:
CFLAGS += -F/Library/Frameworks
LDFLAGS += -F/Library/Frameworks
LIBS += -framework SDL2
Related
I get the following error when trying to compile my simple program with make all:
❯ make all
clang++ --std=c++11 -Wall main.cpp
Undefined symbols for architecture arm64:
"file_reader::read_file()", referenced from:
_main in main-99040e.o
"file_reader::file_reader(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main-99040e.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main.o] Error 1
I can compile the program manually if I do clang++ --std=clang++11 main.cpp file_reader.cpp (and the executable works correctly), so I don't think it's an architecture error like it suggests. I think it might be a problem with my makefile, which I will include below, so if anyone has any insight that would be really helpful. I looked up several conventions for makefiles and none of them helped. The structure is pretty basic with main.cpp creating an object of type file_reader and then referencing one of its methods, so I use #include "file_reader.h" in my main.cpp.
makefile:
# compiler
CXX = clang++
# compiler flags:
# --std=clang++11 : verion
CXXFLAGS = --std=c++11 -Wall
all: main.out
main.out: main.o file_reader.o
$(CXX) main.o file_reader.o -o main
main.o: main.cpp file_reader.h
$(CXX) $(CXXFLAGS) main.cpp
file_reader.o: file_reader.cpp file_reader.h
$(CXX) $(CXXFLAGS) file_reader.cpp
clean:
rm -rf *.o restaurant
I've been trying to get a simple SDL2 project to build on Mac. I found a makefile on a different post (SDL2 not being built on VS code) but when I try to use it it gives me the same error I had originally.
Here is the makefile:
CXX := g++
CXX_FLAGS := -std=c++2a -ggdb
BIN := bin
SRC := src
INCLUDE_PATHS := /usr/local/Cellar/sdl2/2.0.16/include
LIBRARY_PATHS = /usr/local/Cellar/sdl2/2.0.16/lib
LIBRARIES := -lSDL2
EXECUTABLE := main
.PHONY: clean all
all: $(BIN)/$(EXECUTABLE)
run: clean all
./$(BIN)/$(EXECUTABLE)
$(BIN)/$(EXECUTABLE): $(SRC)/*.cpp
$(CXX) $(CXX_FLAGS) $^ -o $# -I$(INCLUDE_PATHS) -L$(LIBRARY_PATHS) $(LIBRARIES)
clean:
-rm $(BIN)/*
The simple SDL2 code:
#include <SDL2/SDL.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
cout << "not working" << endl;
else
cout << "working" << endl;
return 0;
}
And finally, here is the error:
g++ src/main.cpp -o src/main
Undefined symbols for architecture x86_64:
"_SDL_Init", referenced from:
_main in main-e1dd26.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: *** [src/main] Error 1
I'm on the mac operating system (if that makes any difference) and I installed SDL2 through homebrew.
I'm starting a new project using C++ and OpenGL to make a simple game and to teach myself OpenGL. I do not want to use GLUT, and would rather use SDL for making windows and such. I'm having a lot of weird trouble with the Makefile, however. Currently, when I type make, I get this response when I include $(LFLAGS) $(LDFLAGS) in my G++ commands:
g++-5 tetris.o -o tetris -std=c++14
Undefined symbols for architecture x86_64:
"_SDL_CreateWindow", referenced from:
_main in tetris.o
"_SDL_GL_CreateContext", referenced from:
_main in tetris.o
"_SDL_Init", referenced from:
_main in tetris.o
"_SDL_PollEvent", referenced from:
game_loop(SDL_Window*) in tetris.o
"_glewGetErrorString", referenced from:
_main in tetris.o
"_glewInit", referenced from:
_main in tetris.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make: *** [tetris] Error 1
and this response when I do not:
g++-5 -Wall -framework SDL2 tetris.o -o tetris -std=c++14
ld: framework not found SDL2
collect2: error: ld returned 1 exit status
make: *** [tetris] Error 1
Here is my new Makefile:
CC = g++-5
CFLAGS = -Wall -c -Wno-deprecated-declarations
LFLAGS = -Wall
LDFLAGS = -framework SDL2
LDLIBS= -lSDL2 -lglew -lGL
INCLUDES = -I/Library/Frameworks/SDL2.framework/Headers/
STD = -std=c++14
all: tetris
tetris: tetris.o
$(CC) $(LFLAGS) $(LDFLAGS) tetris.o -o tetris $(STD)
tetris.o: tetris.cpp
$(CC) $(CFLAGS) $(LDFLAGS) -o $# tetris.cpp $(STD)
.PHONY: clean
clean:
rm tetris *.o
And the headers part of my .cpp file:
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include <unistd.h>
#include <iostream>
maybe you need to add
LDFLAGS = -framework GLUT
to your Makefile
Just use freeglut, you don't need to use SDL so that you can get started with OpenGL.
I would like to use tinyxml. I have used it in the past, and it works great. The only problem is I was developing on my Linux box which is a laptop. I have a mac mini and I have that set up as my desktop, so I would like to use it for a big chunk of development.
I have a simple tinyxml example that one of my instructors gave me. With this example came a Makefile like so:
ifeq ("$(shell whoami)", "malloy")
CXX = clang++
else
CXX = g++
endif
# Warnings frequently signal eventual errors:
CXXFLAGS=`sdl-config --cflags` -g -W -Wall -Weffc++ -Wextra -pedantic -O0
ifeq ("$(shell uname)", "Darwin")
LDFLAGS = -framework Foundation -framework GLUT -framework OpenGL -lm
else
ifeq ("$(shell uname)", "Linux")
LDFLAGS = `sdl-config --libs` -lm -lSDL_ttf -lSDL_image -ltinyxml
endif
endif
OBJS = \
main.o
EXEC = run
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $#
$(EXEC): $(OBJS)
$(CXX) $(CXXFLAGS) -o $# $(OBJS) $(LDFLAGS)
main.o: main.cpp
clean:
rm -rf $(OBJS)
rm -rf $(EXEC)
And when I try to make this simple project I get all of these Undefined symbol errors.
This is what terminal outputs after I type make:
g++ `sdl-config --cflags` -g -W -Wall -Weffc++ -Wextra -pedantic -O0 -o run main.o - framework Foundation -framework GLUT -framework OpenGL -lm
Undefined symbols for architecture x86_64:
"TiXmlString::nullrep_", referenced from:
TiXmlString::quit() in main.o
"TiXmlDocument::LoadFile(TiXmlEncoding)", referenced from:
_main in main.o
"TiXmlDocument::TiXmlDocument(char const*)", referenced from:
_main in main.o
"TiXmlNode::Clear()", referenced from:
_main in main.o
"TiXmlNode::~TiXmlNode()", referenced from:
TiXmlDocument::~TiXmlDocument() in main.o
"TiXmlElement::Attribute(char const*) const", referenced from:
_main in main.o
"TiXmlNode::FirstChildElement() const", referenced from:
TiXmlNode::FirstChildElement() in main.o
"TiXmlNode::NextSiblingElement() const", referenced from:
TiXmlNode::NextSiblingElement() in main.o
"vtable for TiXmlDocument", referenced from:
TiXmlDocument::~TiXmlDocument() in main.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [run] Error 1
I know it is a linker error that is about it. I used Homebrew to install tinyxml. It was actually giving me problems so after I got the tar-ball for tinyxml I extracted it and put the folder in /usr/local/include. g++ doesn't complain about finding the files. Just stuff with the v-table.
Any help is greatly appreciated! Thanks in advance!!!
You should modify the makefile and instruct the linker to use tinyxml (-ltinyxml) in the "Darwin" case. AFAIK, tinyxml is not bundled with OSX so you may need to find or build that. Instead, you can use expat (simpler-easier) or libxml2 (faster-detailed), which already come with OSX (you can also use (c++) wrappers, google them...)
I'm trying to compile the tetris program I wrote with C++ and SDL on OS X. First I tried doing this:
`g++ -o tetris main.cpp `sdl-config --cflags --libs` -framework Cocoa`
and got this:
Undefined symbols:
"Game::startGame()", referenced from:
_main in ccQMhbGx.o
"Game::Game()", referenced from:
_main in ccQMhbGx.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Here is the main.cpp file:
#include <iostream>
#include "Game.h"
int main(int argc, char* argv[]) {
Game *game = new Game();
game->startGame();
return 0;
}
Game.h is the game class where all of the other classes (Board.h, IO.h, Piece.h, Pieces.h) are included and the main logic of the game is contained.
I'd really like to be able to write a makefile for this or find some way to easily distribute it to friends.
EDIT:
here is the final makefile in case anyone else is having the same problem:
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)
I think your compile issue is related to the SDL main function.
The compile failure is because you're missing references to "Game.o" or whatever the object file resulted out of compiling Game.cpp is called. Try:
g++ -o tetris main.cpp Game.o Pieces.o Whateverelse.o `sdl-config --cflags --libs` -framework Cocoa