MinGW gcc gives errors when other compilers do not - c++

Before you asked, yes i did look this up FOR DAYS. Im completely stuck... I'm using MINGW32 (my shortcut says MSYS) to compile my c and cpp code. For about 2 or 3 days now I have been getting strange errors. (below) It was working JUST FINE before. I even ran the same code i've compiled before and it gave the same error. I then go into DevC++ and open then compile an it works just fine.
ERRORS:
namespace: command not found
using: command not found
syntax error:
int main(){
(sometimes it gives me a big unreadable mess)
I'm really stuck... I dont want to have to switch to DevC++... I like to use my own text editor and compile in a command line.

From your errors namespace: command not found and using: command not found says to me that you aren't compiling the code with an appropriate compiler.
For reference in MinGW32 toolchain:
gcc.exe = C
g++.exe = C++
You may find it useful to take a look at what the IDE's actualy do with your compiler.
My current IDE allows me to see all the commands that it runs to build my project:
C:/mingw32/bin/g++.exe -c "C:/MyProgram/main.cpp" -g -O0 -std=c++14 -Wall -o ./Obj/main.cpp.o -I. -IDependencies/Something/include
So lets examine what this does.
My current toolchain is MinGW32 which is located in C:/mingw32/bin/g++.exe
g++ is our c++ compiler so we call g++.exe and we pass the following switch:
-c "C:/MyProgramm/main.cpp"
This tells my compiler to compile the main.cpp from my project directory. then my IDE adds a few additional command line switches. For the purpose of the answer I will only consider -o. This tells us the output file from our code main.cpp into an output file.
The reason we produce such a file is to save us time compiling so that we do not have to compile the same file twice without making changes to it. We perform this step on each of our files creating a collection of .o files.
The new file is then saved in "C:/MyProgramm/Obj/main.cpp.o"
Which means that your command line function will look something like this:
C:/mingw32/bin/g++.exe -c "<my project directory>/<file>.cpp" -o ./Obj/<file>.cpp.o
I would recommend that you read up on documentation for the g++ function and learn from different IDE's as you will soon find that you need to do more advanced things with your compiler.
For example to enable features from c++14 I add -std=c++14
*Edited to reflect feedback.

Related

Problems with including custom c++ library in Visual Studio Code

I was trying to include the GMP library, which was simply the code below(I did nothing else):
#include <gmpxx.h>
However, when I tried to compile the code, the following error from g++ compiler occured:
myCode.cpp:3:10: fatal error: gmpxx.h: No such file or directory
#include <gmpxx.h>
^~~~~~~~~~~~~~~~~~~~~~
I have tried everything I searched online, putting the GMP lib here and there, adding INFINITE includepaths in c_cpp_properties.json, still, it keeps showing the message, although, I can find the file through "Go to Definition" option.
Is there any known solution to this?
It's not enough to configure VS Code includes, you need to pass those options to the compiler as well.
You don't mention your platform at all, so I'm going to use an example from my personal machine, a Macbook Pro with the fmt library.
When compiling with the fmt library, I have to provide three more options to the compiler.
-I/usr/local/include // Tells the compiler where to look for extra includes
-L/usr/local/lib // Tells the compiler where to look for extra libraries
-lfmt // fmt-specific command to use fmt library
So the full command ends up looking like this:
g++ -Wall -std=c++17 -I/user/local/include -L/usr/local/lib -lfmt main.cpp
I need all three options because fmt is installed in a non-standard location that the compiler doesn't check by default. According to the documentation, you can get away with just -lgmp and -lgmpxx if you installed the library in a standard location (happens by default with *nix and a package manager, I imagine).
If you use build tasks in VS Code, this can be set up and automated for you.

How to link using GCC commands in Bloodshed Dev-C++

I was trying to figure out how to link Fortran and C++ code, and one of the tutorials had written 2 programs, one in C++ in a file named testC.cpp, and the other in Fortran in a file named testF.f but I need to input the following compilation instructions:
gfortran -c testF.f
g++ -c testC.cpp
g++ -o test testF.o testC.o -lg2c
Problem is, I'm working in an IDE called Bloodshed Dev-C++ so I have no idea how to do this. I tried going in compiler options and in the general section I appended those instructions in the option "add the following commands when calling the compiler". Doesn't work.
Maybee you need use custom Makefile. Project->project options. Or include *.mak files

Can we use g++ compile code to do performance analysis with Solaris Studio's Performance Analyser?

I am getting the following error while running the collect command
$ collect -c on sample
bit (warning): Cannot operate on /home/user1/ANALYSIS/SAMPLE_PROGRAM/sample. Ple ase recompile it on a machine with Solaris10 update 5 or higher (or OpenSolaris version snv_52 or higher). If using an older OS, try -xbinopt=prepare (SPARC onl y).
The sample program was build with following g++ flags
g++ -c -Wall -g3 -m64 -pthread -O2 -DSOLARIS -DSS_64BIT_SERVER
The sample program is simple
contains only the following code
while (true)
{
sleep (10);
}
I was just trying to see whether c++ code compiled code can be used with collect command.
As we have a huge g++ compiled binary which we would not like to compile again with Solaris Studio C++ compilers
I don't think so. Studio option -xbinopt=prepare includes special code to binaries to use performance counters. I haven't been using it for years... as far as I remeber -xbinopt=prepare makes binary to write perfomance data to files in CWD (or dir specified by other parameter) and late you can use those data with -xbinopt=use.
Rules are compile 1st with prepare, then run to collect data, later recompile with collected performance data to get better optimized code. Similar to JIT compiler, but in compile time.

Are there compiler options in clang

I am learning from The C++ Primer. One of the exercises is to compile a program with arguments in main(). For this I am trying to use mac terminal.
I need to compile a C++11 Unix executable file named main which takes f as an argument. I am using Xcode 4.6.3 on OS X Lion.
I compiled the program with clang++ -std=c++11 -stdlib=libc++ main.cpp -o main.
But don’t know what to do next.
I found -frecord-gcc-switches while searching compiler options on google. It does what I need to do. Is there a clang version of this?
Please use simple language. I have never used command line before. I tried going through the clang manual but a lot of it is out of my depth.
If the compiler didn't complain about anything, you should have a fresh new file named main in the same directory as the source file, and you can run it from the command line using ./main -f or the like.

minGW CPP G++ Proper Command to Compile

I installed the following:
MINGW32_NT-6.1 i686 Msys
I am working with the command line.
Wrote the "typical" HelloWorld.cpp program.
IF I compile with: cpp HelloWorld.cpp -o HelloWorld.exe COMPILE is good. (18k)
BUT execution fails: 16 bit MS-DOS Subsystem. NTVDM CPU error
IF I compile with: g++ HelloWorld.cpp -o HelloWorld.exe COMPILE is good. (48k)
Execution is good.
I cannot determine the BEST way to execute the compile and what the difference is between the methods. Any suggestions? or good references?
THANKS.
"cpp" is the "C PreProcessor", not the compiler. So you're just getting something strange in HelloWorld.exe
Execute the "type HelloWorld.exe" and see what it gives. It shouldn't even be a binary file - just a long text file with all the "#includes" and "#defines" replaced.
To your question - the second way is "right", because you actually invoke the compiler/linker and produce a valid executable. The first "way" is a valid command, but it has almost nothing to do with compilation and linking.