Constant Makefile Error - c++

I can't get this makefile error to go away. I have tried every syntax on earth and I can't figure out what is wrong.
My makefile reads:
huffmanCodeGenerator: main.o
g++ -o huffmanCodeGenerator main.o
main.o: main.cpp ReadFile.h HuffmanCode.h ConstructTree.h WriteFile.h
g++ -c main.cpp
ReadFile.o: ReadFile.cpp ReadFile.h
g++ -c ReadFile.cpp
HuffmanCode.o: HuffmanCode.cpp HuffmanCode.h
g++ -c HuffmanCode.cpp
ConstructTree.o: ConstructTree.cpp ConstructTree.h
g++ -c ConstructTree.cpp
WriteFile.o: WriteFile.cpp WriteFile.h
g++ -c WriteFile.cpp
clean:
&nbps rm *.o huffmanCodeGenerator
I have all of those files in my directory and I keep getting
error 2 - the system cannot find the file specified.
Here is a screenshot of my directory and error:
Any suggestions on how to fix this? I don't care how pretty or how correct the syntax is. As long as it makes.

First problem I see is that your first two lines are incorrect. Linker needs all the object files and not just main.o to link them together.
Secondly, are you sure your Windows knows the path to g++? What happens if you try g++ --help in cmd, for example? (Not sure about this though, I run it like that in Unix).
Thirdly, this looks similar to your problem: Internal Builder: Cannot run program "g++": The system cannot find the file specified
This link: leads to a conclusion that you need to:
1) have g++.exe in your current folder or
2) have it on your PATH

Related

C++ file compiling: -L and -I arguments don't work for boost library

There are similar questions but their answers did not work for my issue.
I have a c++ program with #include <boost/test/unit_test.hpp> on top (among other includes).
To compile correctly, if I understood, I should do the command:
g++ -g -L/path_to_boost_lib -lboost_lib myprog.cpp -o myprog.exe
If i do a locate, I get /usr/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.
Hence I edited my call to g++ by doing:
g++ -g -L/usr/lib/x86_64-linux-gnu -lboost_unit_test_framework myprog.cpp -o myprog.exe
But I still get errors of the type undefined reference to boost::unit_test.
I also tried the option -I/usr/include/ which contains the boost folder, without success.
It's because of the order. The GCC linker goes through the artifacts left-to-right, and every unknown symbol it encounters in an object file must be resolved by an artifact occurring afterwards.
The right command is thus:
g++ -g myprog.cpp -L/usr/lib/x86_64-linux-gnu -lboost_unit_test_framework -o myprog.exe
See this answer for a more thorough explanation.
I suggest using a build tool like CMake that takes care of such low-level details for you.

G++ stops working when used in makefile

I'm currently trying to use a makefile to automate my build process which I am still new to. When I run the g++ command:
g++ -I includes -L lib -lSDL2_test -lSDL2 -lSDL2main main.o -o main.exe
from the following makefile:
main: main.o
g++ -I includes -L lib -lSDL2_test -lSDL2 -lSDL2main main.o -o main.exe
main.o: main.cpp
g++ -c main.cpp
I get clang: error: linker command failed with exit code 1
However when running the same g++ command from the same directory as the makefile everything compiles correctly and I don't get the error. So how do I fix this?
EDIT:
I've done some testing and found out the problem with the makefile is that the -I includes g++ flag is not enabling the main.o file to find SDL.H, hopefully that helps to narrow down the problem.
#G.M's comment isn't unrelated - it's the correct answer to the problem. The include files are needed during the compile stage (the main.o: main.cpp rule), NOT during the link stage (i.e. the main: main.o rule.)
Move the -I includes flag to the second rule, and try again.

gsl undefined symbol: gsl_multifit_nlinear_trs_lmaccel

I'm having some troubles with the GNU Scientific Lbrary (GSL).
After having intalled it, I tested it with the example here and it works when compiling with:
gcc -Wall -c main.cpp ; gcc -L/usr/local/lib main.o -lgsl -lgslcblas -lm -libtemt
Next, I tried to compile that with no change in the source code nor the compiling command. But here is the problem, when I try to run the result, I get :
./a.out: symbol lookup error: ./a.out: undefined symbol: gsl_multifit nlinear_trs_lmaccel
I tried to comment it out, but it's making other issues. I found that variable in the fulle doc, but I can't find where it's defined.
As the first example is working, I think GSL was installed successfully, and as the same variableis used in different codes, I think it's a global variable, defined in th library.
Does anyone have an idea why I can't access it?
Thank's a lot!
Well, after a lot of tears (no, it's a joke) I found a sort of answer:
I copy and paste libgsl.a and libgslcblas.a in my directory, i wrote that makefile:
CC=gcc
CXXFLAGS=-W -Wall -ansi -pedantic
LIBS=libgsl.a libgslcblas.a -lm
main:main.o
$(CC) -o main main.o $(LDFLAGS) $(LIBS)
main.o: main.cpp
$(CC) -o main.o -c main.cpp $(CXXFLAGS)
I'm happy with that answer even if there is 2 weaknesses:
1) I don't know how to use shared library (at this moment)
2) I don't know how to make it by command line
Hope it will help someone

Trying to compile .cpp with g++ for 64 bit windows

First I tried just downloading what you get after searching mingw64 windows. That didn't work. While searching for a solution I came across this, where the answer includes what seems to be a legit version of mingw64.
This being probably the third or fourth mingw64 I've downloaded, I was happy to see a g++64.exe which I assumed would take care of everything. It doesn't, after compiling with g++64 -o hello.exe -c hello.cpp and running hello I get an error saying This version of [...]\hello.exe is not compatible[...].
What am I doing wrong? I've tried -m64. Is there some additional setting I need to change? Should I post what I get for g++64 -v?
Your command is wrong, you're not creating a .exe file, but an object file that you need to link to produce an executable. Do it like this:
g++64 -o hello.exe hello.cpp
The -c argument tells the compiler to just compile but not link your code. You can do the above in 2 steps, compile and link:
g++64 -c -o hello.o hello.cpp
g++64 -o hello.exe hello.o

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.