I'm following this SDL tutorial to try and make use of some SDL extension libraries. My code is identical to theirs but I am still unable to make the file which leads me to believe the problem is in my makefile which looks like this:
CXX = g++
# Update these paths to match your installation
# You may also need to update the linker option rpath, which sets where to look for
# the SDL2 libraries at runtime to match your install
SDL_LIB = -L/usr/local/lib -lSDL2 -Wl,-rpath=/usr/local/lib, -lSDL2_image
SDL_INCLUDE = -I/usr/local/include
# You may need to change -std=c++11 to -std=c++0x if your compiler is a bit older
CXXFLAGS = -Wall -c -std=c++11 $(SDL_INCLUDE)
LDFLAGS = $(SDL_LIB)
EXE = SDL_Lesson3
all: $(EXE)
$(EXE): main.o
$(CXX) $< $(LDFLAGS) -o $#
main.o: main.cpp
$(CXX) $(CXXFLAGS) $< -o $#
clean:
rm *.o && rm $(EXE)
That makefile worked fine for previous examples. The only thing that has changed in this example is line 5 where I added -lSDL2_image as per the tutorial. When I try make the file I get the following traceback:
rony#comet:~/Documents/cpp/helloworld/lesson3$ make
g++ main.o -L/usr/local/lib -lSDL2 -Wl,-rpath=/usr/local/lib, -lSDL2_image -o SDL_Lesson3
/usr/bin/ld: cannot find : No such file or directory
collect2: error: ld returned 1 exit status
make: *** [SDL_Lesson3] Error 1
Is there an error with my makefile? Have I not installed the library correctly?
The problem is this rogue comma:
SDL_LIB = -L/usr/local/lib -lSDL2 -Wl,-rpath=/usr/local/lib, -lSDL2_image
^
causing the linker to look for libraries in a non-existent directory with an empty name, as well as /usr/local/lib. Removing the comma should fix it.
Related
Here is my makefile:
LIBS = libxml2.so.2.9.4
LDFLAGS = -lstdc++ -lpthread -lxml2
vpath %.hpp ./
vpath %.cpp ./
vpath %.h ./libxml2/include/libxml
all: KeyGenerator
KeyGenerator: main.o ProjectXmlParser.o xmlparser.o
g++ ${CXXFLAGS} ${LDFLAGS} -o KeyGenerator $+ ${LIBS}
main.o: main.cpp
ProjectXmlParser.o: ProjectXmlParser.cpp ProjectXmlParser.hpp
xmlparser.o: xmlparser.cpp xmlparser.hpp global.hpp
phony: clean
clean:
rm -f *.o
The output:
/usr/bin/make -f Makefile CONF=Debug
g++ -g -Wall -std=gnu++11 -pthread -I./ -I./src -I./libxml2/include -lstdc++ -lpthread -lxml2 -o KeyGenerator main.o ProjectXmlParser.o xmlparser.o libxml2.so.2.9.4
g++: error: ProjectXmlParser.o: No such file or directory
g++: error: xmlparser.o: No such file or directory
Makefile:12: recipe for target 'KeyGenerator' failed
make: *** [KeyGenerator] Error 1
In the project directory all of my hpp and cpp files exist at the same level. I do have one additional project called libxml that I am using. When I do a build main.o is created but none of the .o files are created. I've seen other posts pose this question but so far nothing has worked for me.
Thank you.
Based on the make -d output:
Must remake target 'ProjectXmlParser.o'.
Successfully remade target file 'ProjectXmlParser.o'.
it seems that make thinks you have an empty recipe to build this target. Maybe you have some stray TAB characters in your makefile after the ProjectxXmlParser.o and xmlparser.o rules, that aren't shown in the makefile you pasted into this question?
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.
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.
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.
I'm working in windows and want to send data from C++ to Matlab. I've gotten the impression this is done most easy creating a makefile. Therefore I've installed cygwin to use the make command.
My makefile is as follows:
CXX = g++
CFLAGS = -O3 -I /cygdrive/c/Program\ Files/MATLAB/R2011a/extern/include
LIBS = -L /cygdrive/c/Program\ Files/MATLAB/R2011a/extern/lib/win64/microsoft/
LIBS2 = -L /cygdrive/c/Program\ Files/MATLAB/R2011a/extern/lib/win64/microsoft/libmx.lib
LDFLAGS = -leng -lmx
RKspace2d: RKspace2d.o
$(CXX) -o $# $^ $(LDFLAGS) $(LIBS)
RKspace2d.o: RKspace2d.cpp
$(CXX) -c $(CFLAGS) $(LIBS) $<
# $# name of the target
# $^ name of all prerequisites with duplicates removed
# $< name of the first prerequisite
When I type in "make" in the cygwin terminal, being in the right directory I get the following error:
$ make
g++ -o RKspace2d RKspace2d.o -leng -lmx -L /cygdrive/c/Program\ Files/MATLAB/R2011a/extern/lib/win64/microsoft/
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: cannot find -leng
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: cannot find -lmx
collect2: ld returnerede afslutningskoden 1
makefile:8: recipe for target `RKspace2d' failed
make: *** [RKspace2d] Error 1
I believe the path is correct since both libeng.lib and libmx.lib are contained in the microsoft folder.
Hope you guys can help
Thomas