Compiling SDL on OS X with makefile - c++

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

Related

Compile a c++ program on PC to communicate with DJI phantom 4 pro

I am trying to receive on my PC(linux mint), a streaming from the camera of a DJI phantom 4 using OpenCV.
The example I am following is : https://developer.dji.com/guidance-sdk/documentation/tutorials/index.html (I am following the linux part)
I copied the Copy libDJI_guidance.so in /usr/local/lib and I checked, it is there.
The makefile is :
#define a compiler
CXX = g++
#define target name
TARGET = main
#define dependencies of target
OBJECTS = main.o DJI_utility.o
#define the Include and Library path
CFLAGS = -g -Wall -I/usr/local/include -I../../../include
LDFLAGS = -Wl,-rpath,./ -lpthread -lrt -L./ -L/usr/local/lib/ -lDJI_guidance -lusb-1.0 `pkg-config --cflags --libs opencv`
$(TARGET) : $(OBJECTS)
$(CXX) -o $(TARGET) $(OBJECTS) $(LDFLAGS)
main.o : main.cpp DJI_utility.h
$(CXX) $(CFLAGS) -c main.cpp DJI_utility.h
DJI_utility.o : DJI_utility.cpp DJI_utility.h
$(CXX) $(CFLAGS) -c DJI_utility.cpp DJI_utility.h
clean:
rm -rf *.o *.gch *.avi $(TARGET)
But when i excute a make in the command line i get:
g++ -o main main.o DJI_utility.o -Wl,-rpath,./ -lpthread -lrt -L./ -L/usr/local/lib/ -lDJI_guidance -lusb-1.0 `pkg-config --cflags --libs opencv`
/usr/bin/ld: skipping incompatible /usr/local/lib//libDJI_guidance.so when searching for -lDJI_guidance
/usr/bin/ld: skipping incompatible /usr/local/lib/libDJI_guidance.so when searching for -lDJI_guidance
/usr/bin/ld: skipping incompatible //usr/local/lib/libDJI_guidance.so when searching for -lDJI_guidance
/usr/bin/ld: cannot find -lDJI_guidance
/usr/bin/ld: cannot find -lusb-1.0
collect2: error: ld returned 1 exit status
Makefile:12: recipe for target 'main' failed
make: *** [main] Error 1
The project is located in:
~/Documents/studies_SRT/SRT5/TX_drone/Guidance-SDK/demo/guidance_track
The output of an ls is:
DJI_guidance.h DJI_utility.cpp DJI_utility.h DJI_utility.h.gch DJI_utility.o main.cpp main.o Makefile
Thank you.
You seem to have downloaded library files for the wrong architecture. If you're on a 64-bit system, download the 64-bit version of the libraries.

aws-sdk-cpp: unresolved symbols

I am trying to build a simple example using aws sdk cpp. But I am stumbled on a building step. I am linking libaws-cpp-sdk-s3.so library, which is supposed to have all symbols from the source file. But the linker cannot even find a couple of them. The source file is:
#include <aws/core/Aws.h>
int main( int argc, char ** argv)
{
Aws::SDKOptions options;
Aws::InitAPI(options);
{
// make your SDK calls here.
}
Aws::ShutdownAPI(options);
return 0;
}
by using this Makefile:
CC = g++
CFLAGS = -g -c -Wall -std=c++11
LDFLAGS = -g
EXECUTABLE = ex1
RM = rm -f
SOURCES = main.cpp
OBJS = $(SOURCES:.cpp=.o)
all: $(EXECUTABLE)
$(EXECUTABLE): main.o -laws-cpp-sdk-s3
$(CC) $(LDFLAGS) main.o -o $#
main.o: main.cpp
$(CC) $(CFLAGS) $^ -o $#
.PHONY: clean
clean:
$(RM) $(EXECUTABLE) $(OBJS) $(SOURCES:.cpp=.d)
When I run make, I got this error. But why? I built
g++ -g main.o -o ex1
main.o: In function main':
/home/username/workspace/ex1/src/main.cpp:6: undefined reference toAws::InitAPI(Aws::SDKOptions const&)'
/home/username/workspace/ex1/src/main.cpp:12: undefined reference to `Aws::ShutdownAPI(Aws::SDKOptions const&)'
collect2: error: ld returned 1 exit status
Makefile:13: recipe for target 'ex1' failed
make: *** [ex1] Error 1
I don't see where you are linking libaws-cpp-sdk-core
You probably need:
$(EXECUTABLE): main.o -laws-cpp-sdk-s3 -laws-cpp-sdk-core
$(CC) $(LDFLAGS) main.o -o $#

setup SDL2 mac through command line

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

Compiling a multiple cpp files Error undefined reference to `main'

I am currently learning how to use makefile and currently dividing my main.cpp to multiple files.
here is my current structure
├── makefile
└── src
├── Graphics
│   ├── Graphics.cpp
│   └── Graphics.h
└── main.cpp
and my makefile
CC = g++
CFLAGS = -c
LFLAGS = -Wall
OUTPUT = game
game : main.o Graphics.o
$(CC) $(LFLAGS) main.o Graphics.o -lSDL2 -lSDL2_image -o $(OUTPUT)
main.o : src/main.cpp
$(CC) $(CFLAGS) src/main.cpp
Graphics.o : src/Graphics/Graphics.cpp
$(CC) $(CFlAGS) src/Graphics/Graphics.cpp -lSDL2
Everytime I compile. I always get this Error. But I have my main declared. is it looking for a main inside my Graphics.cpp?
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [Graphics.o] Error 1
Here is my main function
#include "Graphics/Graphics.h"
int main(int argc , const char * argv[]){
Graphics graphics;
while(true){
//do things here later
}
return 0;
}
//Edit.
Btw. is there also a way not to link sdl2 on Graphics.cpp, If remove -lSDL2. it would give me all that sdl2 function undefined
You have a typo in your makefile; $(CFlAGS) is not the same as $(CFLAGS), meaning that -c is not passed as a command-line option to the compiler when trying to compile src/Graphics/Graphics.cpp.
CC = g++
CFLAGS = -c
LFLAGS = -Wall
OUTPUT = game
game : main.o Graphics.o
$(CC) $(LFLAGS) main.o Graphics.o -lSDL2 -lSDL2_image -o $(OUTPUT)
main.o : src/main.cpp
$(CC) $(CFLAGS) src/main.cpp
Graphics.o : src/Graphics/Graphics.cpp
$(CC) $(CFlAGS) src/Graphics/Graphics.cpp -lSDL2
Variables in makefiles are case-sensitive, which means that even though you wanted to include the contents of CFLAGS you instead got the contents of an undeclared variable named CFlAGS.
Note: Without -c you are telling g++ to run the linker, and as such it will look for a function named main so that it can produce an executable; graphics.cpp does not contain such function, and the diagnostic you have presented is emitted.
Note: After you have fixed the issue with $(CFlAGS) you will be able to remove -lSDL2 from the relevant line.
In your error message it says:
make: *** [Graphics.o] Error 1
That means this error occurred while compiling Graphics.o, not while linking game.
Your command for compiling Graphics.o is
$(CC) $(CFlAGS) src/Graphics/Graphics.cpp -lSDL2
-lSDL2 doesn't make sense here: You want to compile an object file, so the linker is not involved. But you're getting linker errors - why?
Because to compile without linking, you need the -c option. This is part of your CFLAGS variable. But you're not using CFLAGS, you're using CFlAGS (lowercase l). So I bet the command that make is running looks like
g++ src/Graphics/Graphics.cpp -lSDL2
without -c.
Fix: remove -lSDL2, capitalize L in variable name.

Accelerated C++: compiling the programs in chapter 4

I wrote a Makefile to compile the files and output the file "main", which I will eventually execute by "./main". Below is my Makefile:
CXX=clang++
CXXFLAGS=-g -std=c++11 -Wall -pedantic
BIN=prog
SRC=$(wildcard *.cpp)
OBJ=$(SRC:%.cpp=%.o)
all: Student_info median grade main
main: main.cpp
$(CXX) $(CXXFLAGS) main.cpp -o main grade.o median.o Student_info.o
grade: grade.h grade.cpp
$(CXX) $(CXXFLAGS) grade.cpp -o grade.o
median: median.h median.cpp
$(CXX) $(CXXFLAGS) median.cpp -o median.o
Student_info: Student_info.h Student_info.cpp
$(CXX) $(CXXFLAGS) Student_info.cpp -o Student_info.o
clean:
rm -f *.o
rm -f main
However, it says
clang++ -g -std=c++11 -Wall -pedantic Student_info.cpp -o Student_info.o
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Student_info] Error 1
What is wrong with my Makefile?
Student_info: Student_info.h Student_info.cpp
$(CXX) $(CXXFLAGS) Student_info.cpp -o Student_info.o
Looks like you want this to compile to an object file - without linking. To do this, you need to pass -c. Also, the name of the target should be Student_info.o:
Student_info.o: Student_info.h Student_info.cpp
$(CXX) $(CXXFLAGS) -c Student_info.cpp -o Student_info.o
Ditto for grade and median.
main should depend on not only main.cpp but also the object files you are linking it with:
main: main.cpp Student_info.o grade.o median.o
$(CXX) $(CXXFLAGS) main.cpp -o main grade.o median.o Student_info.o