minGW CPP G++ Proper Command to Compile - c++

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.

Related

MinGW gcc gives errors when other compilers do not

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.

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.

Why xlC compiler on aix always generates different object files for the same c++ source code?

When I compile a simple test.cpp file twice using xlC compiler on AIX:
xlC_r test.cpp -o test1
xlC_r test.cpp -o test2
Then test1 and test2 are diffrent! They have diffrent md5sum.
But when I do the same on linux (with g++ of course instead of xlc) then test1 and test2 are the same...
Anybody please could tell me why this strange behavior happens in AIX??
Thanks
Yes, the compiler stores a timestamp in each object file that it creates. As of Version 12.1 of the compiler, one can specify the option -qnotimestamps when compiling to suppress the storing of timestamps.
Maybe it includes a timestamp in the compiled program. You could try and find where they differ by saving an octal or headecimal dump of each program and comparing the results. Many UNIX variants provide the od and diff programs which you could use, but I don't know if they are available under AIX.

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.