Can I merge these g++ calls into one? - c++

g++ -I include/ -I include/gliese/ -I include/othello/ -c src/OthelloHeuristics.cpp -o obj/Debug/src/OthelloHeuristics.o
g++ -I include/ -I include/gliese/ -I include/othello/ -c src/OthelloNegamaxSearch.cpp -o obj/Debug/src/OthelloNegamaxSearch.o
g++ -I include/ -I include/gliese/ -I include/othello/ -c src/main.cpp -o obj/Debug/src/main.o
g++ -L lib -l othello -l SDL_image -l SDL_ttf -l SDL obj/Debug/src/main.o obj/Debug/src/OthelloHeuristics.o obj/Debug/src/OthelloNegamaxSearch.o -o bin/Debug/othello
Currently I'm running those lines in order to build a game, and it works. But is there anyway to merge them into one? (in order to gain some speed). Thanks!

you can write like this
g++ -I include/gliese/ -c src/OthelloHeuristics.cpp src/OthelloNegamaxSearch.cpp src/main.cpp
g++ -L lib -l othello -l SDL_image -l SDL_ttf -l SDL obj/Debug/src/*.o -o bin/Debug/othello

Related

Why does my makefile file return error 193?

g++ -I "./include" -c ./src/BSTDugum.cpp -o ./lib/BSTDugum.o
g++ -I "./include" -c ./src/Kontrol.cpp -o ./lib/Kontrol.o
g++ -I "./include" -c ./src/Program.cpp -o ./lib/Program.o
./lib/BSTDugum.o ./lib/Kontrol.o ./lib/Program.o -o ./bin/program
process_begin: CreateProcess(C:\Users\monster\Desktop\ODEV_3\lib\BSTDugum.o, ./lib/BSTDugum.o
./lib/Kontrol.o ./lib/Program.o -o ./bin/program, ...) failed.
make (e=193): Error 193
Makefile:3: recipe for target 'program' failed
mingw32-make: *** [program] Error 193
I can't solve this problem everything looks good. I can debug this program on visual studio This is my makefile file :
program:BSTDugum.o Kontrol.o Program.o
./lib/BSTDugum.o ./lib/Kontrol.o ./lib/Program.o -o ./bin/program
BSTDugum.o:
g++ -I "./include" -c ./src/BSTDugum.cpp -o ./lib/BSTDugum.o
Kontrol.o:
g++ -I "./include" -c ./src/Kontrol.cpp -o ./lib/Kontrol.o
Program.o:
g++ -I "./include" -c ./src/Program.cpp -o ./lib/Program.o
Add g++ or the linker you'd like to use first on the line supposed to link your program:
program:BSTDugum.o Kontrol.o Program.o
g++ ./lib/BSTDugum.o ./lib/Kontrol.o ./lib/Program.o -o ./bin/program
As it is right now, you're trying to use the object file ./lib/BSTDugum.o as the linking command.

Makefile doesn't understand g++ but terminal does

Makefile
# Assignments
#########################################################
CC := g++
SRC := src
BUILD := build
INCLUDE := include
TARGET := bin/driver
LIB := lib
TESTS := tests
CFLAGS := -g -Wall -Wextra
PATH := -I $(INCLUDE)
#########################################################
driver: Logbook.o Entry.o main.o
g++ Logbook.o Entry.o main.o -o driver
main.o: main.cpp
g++ -c main.cpp
Logbook.o:
g++ -c $(PATH) $(LIB)/Logbook.cpp
Entry.o:
g++ -c $(PATH) $(LIB)/Entry.cpp
test:
g++ -c $(PATH) $(LIB)/Logbook.cpp
clean:
rm -f *.o *.exe driver
STDOUT
mint#mint-VirtualBox ~/Desktop/Logbook $ ls
googletest include lib main.cpp Makefile README.md src
mint#mint-VirtualBox ~/Desktop/Logbook $ make
g++ -c -I include lib/Logbook.cpp
make: g++: Command not found
Makefile:47: recipe for target 'Logbook.o' failed
make: *** [Logbook.o] Error 127
mint#mint-VirtualBox ~/Desktop/Logbook $ ls
googletest include lib main.cpp Makefile README.md src
mint#mint-VirtualBox ~/Desktop/Logbook $ g++ -c -I include lib/Logbook.cpp
mint#mint-VirtualBox ~/Desktop/Logbook $ ls
googletest include lib Logbook.o main.cpp Makefile README.md src
mint#mint-VirtualBox ~/Desktop/Logbook $
The result expected is that g++ works while being called from inside the makefile but clearly doesn't
What exactly is going on here? there are tabs where there should be and running the compilation command while in the terminal works but the Makefile just doesn't want to agree that the g++ command exists.
Don't call the variable PATH, it overrides the default environment variable that tells the Makefile where to look for binaries. Just rename it to CCFLAGS or so.
CCFLAGS := -I $(INCLUDE)
Logbook.o:
g++ -c $(CCFLAGS) $(LIB)/Logbook.cpp
More details on PATH e.g. here: https://en.wikipedia.org/wiki/PATH_(variable)

How to do makefile to compile multiple .cpp files in different directories using a single makefile?

I have the following files in my proj2 directories and need to compile them together to have one executable file.
proj2/main.cpp
proj2/model/Player.cpp
proj2/model/gameBoard.cpp
proj2/controller/TTTController.cpp
proj2/Makefile
I'm using the following command inside my makefile, but it is not working.
all:
g++ /project2_p1/main.cpp /project2_p1/controller/TTTController.cpp /model/gameBoard.cpp /model/Player.cpp -o ttt
clean:
-rm ttt
Can anybody help me please.Thank you
I strongly recommend you start learning make as it is one of the fundamental tools that programmers use. And, if you can learn C++, you can definitely learn make.
In your project you have source files buried in their own subdirectories so in order to find them all you can use the $(shell find...) command. Same with any header files in your project.
By making all: the direct target it gets executed unconditionally and you lose the benefits of using make - only compile when you change something.
Having said that the basic template I am providing here could be improved to recompile only those source files that have changed but that's an exercise for the reader.
I think this should work in your case:
# set non-optional compiler flags here
CXXFLAGS += -std=c++11 -Wall -Wextra -pedantic-errors
# set non-optional preprocessor flags here
# eg. project specific include directories
CPPFLAGS +=
# find cpp files in subdirectories
SOURCES := $(shell find . -name '*.cpp')
# find headers
HEADERS := $(shell find . -name '*.h')
OUTPUT := ttt
# Everything depends on the output
all: $(OUTPUT)
# The output depends on sources and headers
$(OUTPUT): $(SOURCES) $(HEADERS)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $(OUTPUT) $(SOURCES)
clean:
$(RM) $(OUTPUT)
thats my minGW project's makefile codes:
hepsi: derle calistir
Nesneler := ./lib/Hata.o ./lib/Hatalar.o ./lib/Dugum.o ./lib/ListeGezici.o ./lib/BagilListe.o
derle:
g++ -I ./include/ -o ./lib/Hata.o -c ./src/Hata.cpp
g++ -I ./include/ -o ./lib/Hatalar.o -c ./src/Hatalar.cpp
g++ -I ./include/ -o ./lib/Dugum.o -c ./src/Dugum.cpp
g++ -I ./include/ -o ./lib/ListeGezici.o -c ./src/ListeGezici.cpp
g++ -I ./include/ -o ./lib/BagilListe.o -c ./src/BagilListe.cpp
g++ -I ./include/ -o ./bin/test $(Nesneler) ./src/test.cpp
calistir:
./bin/test
In your project I think this will work;
all: compile run
Objects := ./lib/Player.o ./lib/gameBoard.o ./lib/TTTController.o
compile:
g++ -I ./include/ -o ./lib/Player.o -c ./model/Player.cpp
g++ -I ./include/ -o ./lib/gameBoard.o -c ./model/gameBoard.cpp
g++ -I ./include/ -o ./lib/TTTController.o -c .controller/TTTController.cpp
g++ -I ./include/ -o ./bin/main $(Objects) ./main.cpp
run:
./bin/main
lib folder contains .o files. You can chance it if you want.
include folder refers your header .h or .hpp files. You can change every one of them according to your headers location.
bin folder contains your .exe file called main.exe. You can change or remove it like that
run:
./main
I hope it'll work.
#Galik has right. if you want to learn C++, you should definitely learn make.

Trouble compiling a program using an archive (.a)

When I try to compile my file using a library (.a), I get 'fatal error: URLInputStream.h: No such file or directory
compilation terminated.
'. I'm still pretty new to C++, and this seems so simple but I can't get it to work.
Compilation commands I've tried:
g++ inc/Downloader.h lib/libcs240utils.a
g++ inc/Downloader.h -L lib -l cs240utils
g++ inc/Downloader.h -Llib -lcs240utils
g++ src/Downloader.cpp -I inc -L lib -l cs240utils
g++ -c src/Downloader.cpp -I inc -L lib -l cs240utils
How I compile my archive:
make lib
g++ -c -o utils/obj/CommandRunner.o utils/src/CommandRunner.cpp -I utils/inc
g++ -c -o utils/obj/FileInputStream.o utils/src/FileInputStream.cpp -I utils/inc
g++ -c -o utils/obj/FileSystem.o utils/src/FileSystem.cpp -I utils/inc
g++ -c -o utils/obj/HTMLToken.o utils/src/HTMLToken.cpp -I utils/inc
g++ -c -o utils/obj/HTMLTokenizer.o utils/src/HTMLTokenizer.cpp -I utils/inc
g++ -c -o utils/obj/HTTPInputStream.o utils/src/HTTPInputStream.cpp -I utils/inc
g++ -c -o utils/obj/StringUtil.o utils/src/StringUtil.cpp -I utils/inc
g++ -c -o utils/obj/URLInputStream.o utils/src/URLInputStream.cpp -I utils/inc
ar cr lib/libcs240utils.a utils/obj/*.o
The archive seems to be built correctly:
ar t lib/libcs240utils.a
CommandRunner.o
FileInputStream.o
FileSystem.o
HTMLToken.o
HTMLTokenizer.o
HTTPInputStream.o
StringUtil.o
URLInputStream.o
I've also tried various options in ar. If I specify an include path -I utils/inc then it will compile and work properly, so the file I want does work properly. I've read numerous articles and questions here on StackOverflow, and I can't seem to see what I'm doing incorrectly. Any ideas or suggestions?
Note: I'm compiling a header which seems weird. It was originally split into .cpp and .h, but to simplify problem solving I merged them. This same error happened when they were split as well: g++ src/Downloader.cpp -I inc -L lib -l cs240utils
If Downloader.h still likely needs header files to talk to types/classes/etc. in your static library.
The fact that you mention that "-I utils/inc" makes it work would lead me to think that Downloader.h references stuff in that header. You still need the header for compilation even when you have a static lib as part of the link step.

boost and cpp-netlib make compile ERROR

[SOLVED] created symlinks from /usr/lib/lib/* to /usr/lib*
[UPDATE 3] NEW VERSION:
Ok, I think I fixed something
use find / -name "libboost_system.*"
outout was
/usr/include/boost/lib/libboost_system.so
/usr/include/boost/lib/libboost_system.a
/usr/include/boost/lib/libboost_system.so.1.46.1
/usr/lib/lib/libboost_system.so
/usr/lib/lib/libboost_system.a
/usr/lib/lib/libboost_system.so.1.46.1
/usr/local/include/boost_1_46_1/bin.v2/libs/system/build/gcc-4.4.3/release/link-static/threading-multi/libboost_system.a
/usr/local/include/boost_1_46_1/bin.v2/libs/system/build/gcc-4.4.3/release/threading-multi/libboost_system.so.1.46.1
/usr/local/lib/libboost_system.so
/usr/local/lib/libboost_system.a
/usr/local/lib/libboost_system.so.1.46.1
/root/tmp/boost_1_46_1/bin.v2/libs/system/build/gcc-4.4.3/release/link-static/threading-multi/libboost_system.a
/root/tmp/boost_1_46_1/bin.v2/libs/system/build/gcc-4.4.3/release/threading-multi/libboost_system.so.1.46.1
why are these files in /usr/lib/lib ? and is it a problem ?
and the ls -l /usr/lib/lib | grep boost_system
ls -l /usr/lib/lib | grep boost_system
-rw-r--r-- 1 root root 21574 2011-05-09 15:15 libboost_system.a
lrwxrwxrwx 1 root root 25 2011-05-09 15:15 libboost_system.so -> libboost_system.so.1.46.1
-rwxr-xr-x 1 root root 20053 2011-05-09 15:15 libboost_system.so.1.46.1
atm my makefile looks like
LIBPATH=-I/usr/local/include/cpp-netlib
LIBS=$(LIBPATH) -lboost_system -lboost_filesystem -lboost_thread -lpthread
LD=g++ -g
CPP=g++ -c -g $(LIBS)
P=.
OBJ=$(P)/tmp/main.o $(P)/tmp/CLink.o $(P)/tmp/CFetcher.o
main: $(OBJ); $(LD) $(OBJ) $(LIBS) -o $#
$(P)/tmp/CLink.o: $(P)/src/CLink.cpp $(P)/include/CLink.h; $(CPP) -c $< -o $#
$(P)/tmp/CFetcher.o: $(P)/src/CFetcher.cpp $(P)/include/CFetcher.h; $(CPP) -c $< -o $#
$(P)/tmp/main.o: $(P)/src/main.cpp $(P)/include/CLink.h $(P)/include/CFetcher.h ; $(CPP) -c $< -o $#
all:
touch $(P)/tmp/*.o;
touch main;
rm -f $(P)/tmp/*.o;
rm -f main;
make main;
The Compiler output is lie
g++ -c -g -I/usr/local/include/cpp-netlib -lboost_system -lboost_filesystem -lboost_thread -lpthread -c src/main.cpp -o tmp/main.o
g++ -c -g -I/usr/local/include/cpp-netlib -lboost_system -lboost_filesystem -lboost_thread -lpthread -c src/CLink.cpp -o tmp/CLink.o
g++ -c -g -I/usr/local/include/cpp-netlib -lboost_system -lboost_filesystem -lboost_thread -lpthread -c src/CFetcher.cpp -o tmp/CFetcher.o
g++ -g ./tmp/main.o ./tmp/CLink.o ./tmp/CFetcher.o -I/usr/local/include/cpp-netlib -lboost_system -lboost_filesystem -lboost_thread -lpthread -o main
So for me all looks nice but when i try to run the program
./main
./main: error while loading shared libraries: libboost_system.so.1.46.1: cannot open shared object file: No such file or directory
The -l flags must come after the source files on linker command-line.
Yes, that means you'll have to split the LD definition to LD and LIBS, put all the -L and -l flags in the later and change the link command to:
$(LD) $(OBJ) $(LIBS) -o $#
The library (.so (dynamic) or .a (static)) files have to be the same version as the headers. While there are boost 1.46.1 headers installed in /usr/local/include/boost_1_46_1/, the corresponding library files don't seem to be installed at all. The only installed libraries are version 1.40.0 in /usr/lib, so the linker finds those (/usr/lib would be searched by default even if you didn't include the -L/usr/lib flag), but they don't contain the symbols expected by 1.46.1.
Note that when linking against shared library (using shared libraries is strongly recommended in Linux), the linker looks for the file with .so extension, but that is usually symlink to a file with added version suffix and the linker reads it and records the target name in the binary. That way programs compiled against the .1.40 will continue to work when 1.46 is installed, because the libboost*.so.1.40.0 may (and have to) stay around after the .so is redirected to the 1.46.1 version.
It should be even possible to install like:
/usr/local/lib/boost_1_46_1/libboost_system-mt.so -> /usr/local/lib/libboost_system-mt.so.1.46
/usr/local/lib/libboost_system-mt.so.1.46 -> /usr/local/lib/libboost_system-mt.so.1.46.1
/usr/local/lib/libboost_system-mt.so.1.46.1
and compile using -L/usr/local/lib/boost_1_46_1, though I currently can't find any package that would do it to confirm this. This way you could have development files for multiple versions installed and switch between them using explicit -I and -L flags while dynamic linker would still find the runtime files, for which it only looks in /usr/local/lib, /usr/lib and /lib (it can be configured in /etc/ld.so.conf, but that's the default).
Execute your application with strace. This will show the location were you app is looking for your boost libs. In my case an app was looking in /usr/lib/x86_64-linux-gnu for boost libs where the actual location was /usr/lib/lib. A simple export statement adding the paths for the boost shared libs in my case LD_LIBRARY_PATH=/usr/lib/lib worked a treat.
output from strace
open("/usr/lib/x86_64-linux-gnu/libboost_system.so.1.46.1", O_RDONLY) = -1 ENOENT (No such file or directory)
exit_group(127) = ?