makefiles and linking a library in a different folder - c++

I've search around a bit on StackOverflow and tried a few suggestions but as of yet nothing has solved the problem.
I'm making a makefile for a school project and as part of my project I'm generating a static library and linking against it. The compiler throws an error when it gets to a header include in the static library. The code for that is just #include "StringUtil.h"
So in the makefile I have these relevant parts of code
LINKFLAGS=-Llib/ -lHTMLtools
bin : lib $(BIN_FILE)
lib : $(LIB_OBJ_FILES)
ar r lib/libHTMLtools.a $(LIB_OBJ_FILES)
$(BIN_FILE) : $(OBJ_FILES) #This is only obj/crawler.o for now
g++ -o bin/crawler obj/crawler.o
obj/crawler.o : src/crawler.cpp inc/crawler.h
g++ -c -static $(LINKFLAGS) -o obj/crawler.o -I inc src/crawler.cpp
so whenever I run the make bin command it generates lib.libHTMLtools.a as expected but when it gets to the
g++ -c -static $(LINKFLAGS) -o obj/crawler.o -I inc src/crawler.cpp
line it returns this error.
src/crawler.cpp:2:24: fatal error: StringUtil.h: No such file or directory compilation terminated.
Any help or advice will be appreciated!

In C++, library files are not enough. They are not used when compiling source code, but only when linking. To compile source file, you need to include headers. But the compiler need to know where to find it. Try adding -I utils/inc to your last line like this
g++ -c -static $(LINKFLAGS) -o obj/crawler.o -I inc -I utils/inc src/crawler.cpp

Related

How to adapt C++ Makefile to allow for using openmpi parallelization compiling several files into 1 executable

Just a disclaimer I'm not too experienced with Make files, so forgive me if the answer is simple! I have a Make file that compiles several .cpp & .h files into .o files, and then produces an executable. I am using g++ as the compiler currently. I'd like to adapt this file to allow for openmpi multi-core computing. To note: I'm on a Windows 11 x 64 architecture, using Cygwin to build run this cpp code. I also would like this to run on MacOS systems as well.
In particular, the parallelization macros are only in 1 .cpp file, what I'd consider the 'main' file of this project (mag_spec_tracker.cpp). I don't know if that makes a difference here but thought it could be useful to let be known.
My current Makefile is below - note I added a line to try and grab the mpicc compile flags, but I am unsure on how to use them here.
IDIR = include
CXX = g++
CXXFLAGS = -I$(IDIR) -std=c++17
ODIR = obj
_DEPS = my_functions.h particle.h beam.h threevector.h threematrix.h screen.h magnet.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ = my_functions.o particle.o beam.o mag_spec_tracker.o screen.o magnet.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/%.o: %.cpp $(DEPS)
$(CXX) -c -o $# $< $(CXXFLAGS)
run: $(OBJ)
$(CXX) -o $# $^ $(CXXFLAGS)
.PHONY: clean
clean:
rm -f $(ODIR)/*.o *~ core $(IDIR)/*~
I have tried adding lines into the Makefile to get the mpi compile flags based on other reference I've found online, but was unsure how to adapt my current code to add these flags to allow for correct usage of openmpi. The lines I added to grab compile flags were:
MPI_COMPILE_FLAGS = $(shell mpicc --showme:compile)
MPI_LINK_FLAGS = $(shell mpicc --showme:link)
Thank you in advance!
Update (12/6/2022)
Responding to the comment from Matzeri ('for opempi the compiler is mpicc not gcc. for c++ is mpicxx not g++'), I replaced 'g++' in my makefile with 'mpicxx' and recieved the following error:
$ make
mpicxx -c -o obj/mag_spec_tracker.o mag_spec_tracker.cpp -Iinclude -std=c++17
mpicxx -o run obj/my_functions.o obj/particle.o obj/beam.o obj/mag_spec_tracker.o obj/screen.o obj/magnet.o -Iinclude -std=c++17
C:/Users/Jason/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lmpi_cxx: No such file or directory
C:/Users/Jason/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lmpi: No such file or directory
C:/Users/Jason/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lopen-rte: No such file or directory
C:/Users/Jason/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lopen-pal: No such file or directory
collect2.exe: error: ld returned 1 exit status
make: *** [Makefile:19: run] Error 1
I notice that its looking for an ld executable in 'mingw64'. I tried removing that directory (previously I had installed mingw64, but switched to cygwin), and got the following error:
$ make
mpicxx -o run obj/my_functions.o obj/particle.o obj/beam.o obj/mag_spec_tracker.o obj/screen.o obj/magnet.o -Iinclude -std=c++17
--------------------------------------------------------------------------
The Open MPI wrapper compiler was unable to find the specified compiler
g++ in your PATH.
Note that this compiler was either specified at configure time or in
one of several possible environment variables.
--------------------------------------------------------------------------
make: *** [Makefile:19: run] Error 1
I also added c:\cygwin\bin to my environment paths, and this issue still occurs. I confirmed that mpicxx is in that bin folder.
Any ideas?

Console ./a.out not working

I'm trying to compile a simple program in c++ with gsl. On our university server we have installed GSL. The main problem is, that I'm compilling:
g++ atest.cpp -c -lgsl -lgslcblas -c lm
And after that, I'm typing:
./a.out
And I get :
-bash: ./a.out : No such file or directory
What's the problem? Thanks.
You're only compiling, not linking. From man gcc:
-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.
By default, the object file name for a source file is made by
replacing the suffix .c, .i, .s, etc., with .o.
Unrecognized input files, not requiring compilation or assembly,
are ignored.
So -c option (command line argument) means compile
g++ atest.cpp -c -lgsl -lgslcblas -lm
g++ atest.o
this will produce a.out file, or you can use short hand
g++ atest.cpp -lgsl -lgslcblas -lm
which will compile and link code in one.
Plus, you could use ls to check your directory does it contain a.out before trying to run it.

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.

How do I link multiple files without the use of a makefile?

For example, I'm given carModels.cpp, carModels.h, carType.in, manufacturers.h, manufacturers.o, and lastly my own file tester.cpp. How would I go about linking all of these using g++ in a Linux terminal? Would I have to create any additional ".o" files? I'm supposed to assume that the given files already work. Multiple lines in terminal are fine, I just I want a clear understanding of it. (I'm coming from a C++ IDE that I didn't really care for.)
Compile each source file to its own object file:
g++ -I . -c carModels.cpp -o carModels.o
g++ -I . -c tester.cpp -o tester.o
Now link all object files together:
g++ carModels.o tester.o manufacturers.o -o outputname
Consider adding more options like -O3, -std=c++11, -Wall, etc. as needed.
you can do this in two steps, first compile to *.o files,
gcc -c your.cpp other.cpp .....
then link them
gcc -o you_out_put_name the_object_files.o ...
In a single line, that would be just g++ -o tester *.cpp *.o. GCC will sort everything out. In particular, the *.h files are referenced via #include "" statements in the .cpp files.

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.