Makefile doesn't find libs - c++

I'm trying to write Makefile for my project.
Here is the file structure:
Makefile:
CXXFLAGS = -ISDL2/include -std=c++11
LXXFLAGS = -lSDL2/lib/x86 -lSDL2main -lSDL2 -lSDL2_image
main.exe: main.o BoardField.o ChessPiece.o Game.o
g++ main.o BoardField.o ChessPiece.o Game.o -o main.exe $(LXXFLAGS) -std=c++11
main.o: main.cpp
g++ main.cpp -c $(CXXFLAGS)
BoardField.o: BoardField.cpp
g++ BoardField.cpp -c $(CXXFLAGS)
ChessPiece.o: ChessPiece.cpp
g++ ChessPiece.cpp -c $(CXXFLAGS)
Game.o: Game.cpp
g++ Game.cpp -c $(CXXFLAGS)
And I get these errors:
g++ main.o BoardField.o ChessPiece.o Game.o -o main.exe -lSDL2/lib/x86
-lSDL2main -lSDL2 -lSDL2_image -std=c++11 c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe:
cannot find -lSDL2/lib/x86
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe:
cannot find -lSDL2main
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe:
cannot find -lSDL2
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe:
cannot find -lSDL2_image
Where is the problem?

-lSDL2/lib/x86 is incorrect. You use the lower-case -l option, which is used to add a library to link with, not a path to search for libraries.
To add a path use the upper-case -L option: -LSDL2/lib/x86

Related

Makefile for a C++ SDL project

I'm still learning how to set up a Makefile and I'm kind of lost here. I'm using windows and currently trying to fire up my Makefile for small C++ SDL project.
I have 3 .cpp files:
main.cpp
window.cpp
rect.cpp
As well as 2 extra header files:
Window.h
rect.h
So having trouble setting up everything on a Makefile
This is what i currently have:
CXXFLAGS = -Ideps/include -std=c++0x
LXXFLAGS = -Ldeps/lib -lmingw32 -lSDL2main -lSDL2
cup: main.o
g++ main.o -o cup $(LXXFLAGS)
main.o: main.cpp
g++ main.cpp -c $(CXXFLAGS)
window.o: window.cpp
g++ window.cpp -c
rect.o: rect.cpp
g++ rect.cpp -c
But I'm getting a bunch of undefined reference errors for my constructors on my command prompt.
Help please!
From the Makefile contents I read that cup binary is only created from main.o and it does not link window.o nor rect.o, which is where probably those missing references are defined. At the very least I would update the primary rule to say:
cup: main.o window.o rect.o
g++ $(LXXFLAGS) -o cup $^
Thus said, you could make even more use from implicit rules that are built into make, and, if following the standard naming for linking flags, the Makefile could be reduced even further to just a linking line (as compilation rules are implicit), e.g.:
$ cat Makefile
CXXFLAGS = -Ideps/include -std=c++0x
LDFLAGS = -Ldeps/lib
LDLIBS = -lmingw32 -lSDL2main -lSDL2
cup: main.o window.o rect.o
$(LINK.cc) $^ $(LDLIBS) -o $#
Output:
$ make
g++ -Ideps/include -std=c++0x -c -o main.o main.cpp
g++ -Ideps/include -std=c++0x -c -o window.o window.cpp
g++ -Ideps/include -std=c++0x -c -o rect.o rect.cpp
g++ -Ideps/include -std=c++0x -Ldeps/lib main.o window.o rect.o -lmingw32 -lSDL2main -lSDL2 -o cup

expected type-specifier before 'classname'

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?

My c++ application fails to link, and i don't know why

I am currently getting back in to c++. I have been running into a problem building my application.
When i run make the output becomes:
g++ -c -Wall -I headers/ -I ../libs/inc/SDL2 -L ../libs/lib/SDL2 -L/usr/local/lib -Wl,-rpath,/usr/local/lib -lSDL2 -lGL -lSDL2 -lSDL2main Debug.cpp -o Debug.o
g++ -c -Wall -I headers/ -I ../libs/inc/SDL2 -L ../libs/lib/SDL2 -L/usr/local/lib -Wl,-rpath,/usr/local/lib -lSDL2 -lGL -lSDL2 -lSDL2main GameLoop.cpp -o GameLoop.o
g++ -c -Wall -I headers/ -I ../libs/inc/SDL2 -L ../libs/lib/SDL2 -L/usr/local/lib -Wl,-rpath,/usr/local/lib -lSDL2 -lGL -lSDL2 -lSDL2main Main.cpp -o Main.o
g++ -c -Wall -I headers/ -I ../libs/inc/SDL2 -L ../libs/lib/SDL2 -L/usr/local/lib -Wl,-rpath,/usr/local/lib -lSDL2 -lGL -lSDL2 -lSDL2main -o GAME Debug.o GameLoop.o Main.o
g++: warning: Debug.o: linker input file unused because linking not done
g++: warning: GameLoop.o: linker input file unused because linking not done
g++: warning: Main.o: linker input file unused because linking not done
The file structure of my project
Makefile
Main.cpp
GameLoop.cpp
Debug.cpp
headers/
Main.h
GameLoop.h
Debug.h
Makefile:
CC := g++
TARGET := GAME
SOURCES := $(wildcard *.cpp)
OBJECTS := $(patsubst %.cpp, %.o, $(SOURCES))
DEPS := $(wildcard headers/*.h)
CPPFLAGS := -I ../libs/inc/SDL2 -L ../libs/lib/SDL2 $(shell sdl2-config --libs) -lGL -lSDL2 -lSDL2main
CFLAGS := -c -Wall -I headers/
##$(info OBJECTS= $(OBJECTS) :: SOURCES= $(SOURCES) :: EXECUTABLEOUT= $(TARGET))
default: $(TARGET)
%.o: %.cpp $(DEPS)
$(CC) $(CFLAGS) $(CPPFLAGS) $< -c -o $#
$(TARGET): $(OBJECTS)
$(CC) $(CFLAGS) $(CPPFLAGS) -o $# $^
RAW Paste Data
CC := g++
TARGET := GAME
SOURCES := $(wildcard *.cpp)
OBJECTS := $(patsubst %.cpp, %.o, $(SOURCES))
DEPS := $(wildcard headers/*.h)
CPPFLAGS := -I ../libs/inc/SDL2 -L ../libs/lib/SDL2 $(shell sdl2-config --libs) -lGL -lSDL2 -lSDL2main
CFLAGS := -c -Wall -I headers/
##$(info OBJECTS= $(OBJECTS) :: SOURCES= $(SOURCES) :: EXECUTABLEOUT= $(TARGET))
default: $(TARGET)
%.o: %.cpp $(DEPS)
$(CC) $(CFLAGS) $(CPPFLAGS) $< -c -o $#
$(TARGET): $(OBJECTS)
$(CC) $(CFLAGS) $(CPPFLAGS) -o $# $^
Currently been looking at over 60 answers on this page (and others) with no luck. :/
All your g++ calls use the -c option. From GCC's help:
-c Compile and assemble, but do not link
Your last g++ call should not have the -c option. The reason you have it is because you have specified it for all your CFLAGS:
CFLAGS := -c -Wall -I headers/
Removing it from there should fix your problem.
Remove the -c flag from the last line where you make the executable.
From Man page for g++:
-c Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.

/src/DB/sqlite3.dll: file not recognized: File format not recognized

I am trying to include a sqlite3 in my cpp project but while compilation it gives below error:
g++ -c -std=c++11 -g src/Main.cpp -I"C:/Mycode/src/DB"
-L"C:/Mycode/src/DB" -lsqlite3
g++ -g -o Main.exe Main.o Data.o SqliteApi.o -lws2_32
-L"C:/Mycode/src/DB" -lsqlite3
C:/Mycode/src/DB/sqlite3.dll: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status
make: ***[Mycode.exe] Error 1
I feel during final linking time it gives error.
I am using my own make file for compilation, below is the make command I am using:
DB_DIR="C:/Mycode/src/DB"
clean:
rm Main.o Main.exe
Main.exe: Main.o Data.o SqliteApi.o
g++ -g -o Main.exe Main.o Data.o SqliteApi.o -lws2_32 -L${DB_DIR} -lsqlite3
Main.o: src/Main.cpp
g++ -c -std=c++11 -g src/Main.cpp -I${DB_DIR} -lsqlite3
Data.o: src/Data.cpp
g++ -c -std=c++11 -g src/Data.cpp
SqliteApi.o: src/SqliteApi.cpp
g++ -c -std=c++11 -g src/SqliteApi.cpp
I have googled but I couldn't find any solution or suggestion for this error.
Any help will be much appreciated.
The recommended way of using the SQLite library is to compile the amalgamation source file directly into your program:
Main.exe: Main.o Data.o SqliteApi.o
g++ -g -o Main.exe Main.o Data.o SqliteApi.o sqlite3.o -lws2_32
sqlite3.o: src/sqlite3.c
gcc -c ... src/sqlite3.c

makefile: no executable created

I have used openGL with glut before and SDL2 on its own before. Now I try to make an optimized project with Glew, SDL2 and OpenGL. I want it to be able to run on Linux(I mainly use Debian),OSX and Windows. So I tried to write my own makefile. This code should only create a window for me.
Im developing on osx.
I have only my main.cpp, Display.cpp and Display.h
This is what i tried:
#my compiler
CC=g++
#my program name
PRGNAME=booom
#library search directory
LIBDIR=-L/usr/lib -L/usr/local/lib
#library names
#uses libSDL.a, libGLEW.a
LIBNAME=-lSDL -lGLEW
#includes for libraries
INCLUDES=-I/usr/include -I/usr/local/include
#my compiler options
CFLAGS=-c -Wall
all: main.o Display.o
$(CC) $(CFLAGS) $(INCLUDES) $(LIBDIR) $(LIBNAME) -o $(PRGNAME) main.o Display.o
#main.cpp is the dependency of main.o
main.o: main.cpp
$(CC) $(CFLAGS) $(INCLUDES) $(LIBDIR) $(LIBNAME) main.cpp
Display.o: Display.cpp
$(CC) $(CFLAGS) $(INCLUDES) $(LIBDIR) $(LIBNAME) Display.cpp
clean:
rm -rf *o booom
I get a main.o and a Display.o file, but i don't get an executable.
When I do "make clean && make" this is my output:
macbook-mehrschwein:Booom Mehrschwein$ make clean && make
rm -rf *o booom
g++ -c -Wall -I/usr/include -I/usr/local/include -L/usr/lib -L/usr/local/lib -lSDL -lGLEW main.cpp
clang: warning: -lSDL: 'linker' input unused
clang: warning: -lGLEW: 'linker' input unused
clang: warning: argument unused during compilation: '-L/usr/lib'
clang: warning: argument unused during compilation: '-L/usr/local/lib'
g++ -c -Wall -I/usr/include -I/usr/local/include -L/usr/lib -L/usr/local/lib -lSDL -lGLEW Display.cpp
clang: warning: -lSDL: 'linker' input unused
clang: warning: -lGLEW: 'linker' input unused
clang: warning: argument unused during compilation: '-L/usr/lib'
clang: warning: argument unused during compilation: '-L/usr/local/lib'
g++ -c -Wall -I/usr/include -I/usr/local/include -L/usr/lib -L/usr/local/lib -lSDL -lGLEW -o booom main.o Display.o
clang: warning: -lSDL: 'linker' input unused
clang: warning: -lGLEW: 'linker' input unused
clang: warning: main.o: 'linker' input unused
clang: warning: Display.o: 'linker' input unused
clang: warning: argument unused during compilation: '-Wall'
clang: warning: argument unused during compilation: '-I /usr/include'
clang: warning: argument unused during compilation: '-I /usr/local/include'
clang: warning: argument unused during compilation: '-L/usr/lib'
clang: warning: argument unused during compilation: '-L/usr/local/lib'
macbook-mehrschwein:Booom Mehrschwein$
The problem is in -c flag that you use in all stages.
It means to compile an object file but not to link.
Remove it for your all target. Like:
all: main.o Display.o
$(CC) $(LIBDIR) $(LIBNAME) -o $(PRGNAME) main.o Display.o
This fixed the problem with linking.
Now ensure that you have libSDL in the folders you specify.
new version with:
all: main.o Display.o
$(CC) $(LIBDIR) $(LIBNAME) -o $(PRGNAME) main.o Display.o
main.o: main.cpp
$(CC) $(CFLAGS) $(INCLUDES) main.cpp
Display.o: Display.cpp
$(CC) $(CFLAGS) $(INCLUDES) Display.cpp
no warnings for Display.o and main.o anymore.
the error that i get now:
g++ -c -Wall -I/usr/include -I/usr/local/include main.cpp
g++ -c -Wall -I/usr/include -I/usr/local/include Display.cpp
g++ -Wall -I/usr/include -I/usr/local/include -L/usr/lib -L/usr/local/lib -o booom main.o Display.o -lSDL -lGLEW
ld: library not found for -lSDL
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1
EDIT: The name is libSDL2.a, not libSDL.a
but now it tells me that he can't find libGlew.a
libGLEW.a is in /usr/lib
libSDL2.a is in /usr/local/lib
do they both need to be in the same directory?
EDIT2:
Update
I changed a few things, i now build GLEW into "/usr/local", so everything is in the same dir.
my makefile:
#my compiler
CC=g++
#my program name
PRGNAME=booom
#library search directory
LIBDIR=-L/usr/local/lib
#library names
#uses libSDL2.a, libGLEW.a
LIBNAME=-lSDL2 -lGLEW
#includes for libraries
INCLUDES=-I/usr/local/include
#my compiler options
CFLAGS=-c -Wall
all: main.o Display.o
$(CC) $(LIBDIR) $(LIBNAME) -o $(PRGNAME) main.o Display.o
#main.cpp is the dependency of main.o
main.o: main.cpp
$(CC) $(CFLAGS) $(INCLUDES) main.cpp
Display.o: Display.cpp
$(CC) $(CFLAGS) $(INCLUDES) Display.cpp
clean:
rm -rf *o booom
my error now:
rm -rf *o booom
g++ -c -Wall -I/usr/local/include main.cpp
g++ -c -Wall -I/usr/local/include Display.cpp
g++ -L/usr/local/lib -lSDL2 -lGLEW -o booom main.o Display.o
Undefined symbols for architecture x86_64:
"_glClear", referenced from:
Display::Clear(float, float, float, float) in Display.o
"_glClearColor", referenced from:
Display::Clear(float, float, float, float) in Display.o
"_glCullFace", referenced from:
Display::Display(int, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in Display.o
"_glEnable", referenced from:
Display::Display(int, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in Display.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: *** [all] Error 1
seams like i got a 32-bit version, while i need a 64-bit version, but i have build that on my own, and don't really know what i have done wrong. normally you also have do do something like ./config before building. but there is no config for GLEW. the only solution that i found on google involves macports, but i would love to do it without macports