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

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

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.

Running a SFML C++ program with minGW on Windows 10

So I'm trying to run an example SFML program on my Windows laptop. If relevant, the source code is on this page.
So first I make the .o file using this command -
g++ -c a.cpp -ISFML/SFML/include
Where a.cpp is the main file, and my SFML package is located in SFML/SFML.
Then I compile using this command -
g++ a.o -o a -LSFML/SFML/lib -lsfml-graphics -lsfml-window -lsfml-system
When I first ran the program I got the errors about not being able to find certain dlls, sfml-graphics-2 etc. So I found them and put them next to the exe. But now when I run, I get this weird error:
The procedure entry point
_ZNSt7__cxx1112basic_stringSt11char_traitsIcESalcEE7reserveEj could not be located in the dynamic link library.
What is going on here?
As the SFML download page states, You could be using the wrong version of the compiler, other library versions of SFML that you have not removed from your working directory that could mismatch between code and linker. Worst case, if your compiler is not listed there, you have to compile SFML yourself:
Get CMake. Get the source code for 2.4.2 by going to the bottom of the SFML download page. Follow this guide on SFML's GitHub repo. Alternatively, you could use the guide on SFML's page but it is for an older version. It might answer some questions that the first guide misses.
Ones CMake have generated the makefiles, you're on your way to build SFML.
Good luck!
I've had this problem for so long so I just wanted to help someone out who had the same problem. I have a windows 10 FYI and MinGW-w64 8.1.0 (if it doesnt work try a 32 bit mingw instead)
for a debug mode (debug is when your still working on the game and would like command prompt to open whenever you run it)
(make sure your in the right directory first by doing "cd")
g++ -c (file_name).cpp -o (file_name).o -I(path_to)SFML-64-bit/include -g -m64 -Wall &&
g++ (file_name).o -o (game_name).exe -L(path_to)SFML-64-bit/lib -lsfml-graphics -lsfml-window -lsfml-system
The code above when placed in command will compile everything for you if its all in the same directory so make sure you keep an eye out for that
and now for release mode (if you dont want command prompt to show up)
g++ -c (file_name).cpp -o (file_name).o -I(path_to)SFML-64-bit/include -O3 -m64 &&
g++ (file_name).o -o (game_name).exe -L(path_to)SFML-64-bit/lib -lsfml-graphics lsfml-window -lsfml-system -mwindows
Noticed all I added was the -mwindows and the -O3 aswell as removing -g and -Wall which are not necessary since we wont be using command prompt
Make sure to go to SFML/bin and take all the .dlls and put it into the same directory has your .exe sorry xd
Hope this helped.

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.

Constant Makefile Error

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

C++ programs, compiling with g++

I am very aware of compiling C++ programs with g++ in linux environment. But, may be I am missing something, I am getting this strange output/behaviour.
I have source file in test.cpp.
To compile this, I did
(1)
g++ -c test.cpp
g++ -o test test.o
./test
Everything works fine.
But when I did compling and linking in same stage, like this
(2)
g++ test.cpp -o test
./test => Works fine
(3)
g++ -c test.cpp -o test => Doesn't work
In my last case, test is generated but is no more executable; but in my guess it should work fine.
So, what is wrong or do I need to change some settings/configuration ??
I am using g++ 4.3.3
Thanks.
When you say:
g++ -c test.cpp -o test
The -c flag inhibits linking, so no executable is produced - you are renaming the .o file.
Basically, don't do that.
You are forcing compiler to produce an object file and name it like an executable.
Essentially your last line tells: compile this to an object file, but name it test, instead of test.obj.
-c flag means Compile Only
Try
g++ -o test test.cpp
Specifying -o in the g++ command line tells the compiler what name to give the output file. When you tried to do it all in one line, you just told the compiler to compile test.cpp as an object file named test, and no linking was done.
Have a look at the fabulous online manual for GCC for more details.
from the gcc manual:
-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.
You must link the compiled object files to get the executable file.
More info about compiling and linking and stuff is here.
Read man g++. The switch -c is to compile only but not to link.
g++ -c test.cpp -o test
does what
g++ -c test.cpp
does but the object file will be test istead of the default name test.o. An object file cannot be executed.