Using the g++ C++ compiler from cygwin - c++

I am trying to execute my first "Hello World!" in C++. I am using Windows XP, and I have installed cygwin, in which the g++ C++ compiler is installed. I have written a small hello-world program, and saved it in hello.cpp. From the command prompt I write:
g++ hello.cpp
But I get:
'g++' is not recognized as an internal or external command, operable program or batch file.
I have installed cygwin in my D:\programs\cygwin. I have made another directory with my hello-world file in D:\cpp. Something with my installation or my paths seems to be not Ok, but I cannot figure what. I have run the exe file of cygwin, and in the installation directory I have got all necessary files, I think: bin folder, lib, Cygwin.bat, etc.
I have read that in case of such error message I should check whether the cygwin1.dll file is copied into the main folder of Windows. Is this the C:\WINDOWS directory? I have looked there, and I don't have such a file there. I have cygwin1.dll in the bin folder of cygwin: D:\programs\cygwin\bin.
Also, how do I check whether the bin folder (D:\programs\cygwin\bin) is considered in the search path of commands?
I think that the hello-world program shouldn't contain any syntax errors, I have just copied it. Also, when I write
g++ -v
I get the same error message: that the command is not recognized.
I would appreciate if someone give me a hint what should I look at. Thank you.
Here is the code of the hello-world program:
#include <iostream>
using namespace std;
int main() {
cout <<"Hello World!"<<endl;
return 0;
}
I have installed g++, and now when I type g++ -v, I get: Reading specs from: \d\cygnus\cygwin-b20\H-i586-cygwin32\bin..\lib\gcc-lib\i586-cygwin32\egcs-2.91.57\specs gcc version egcs-2.91.57 19980901 (egcs-1.1 release)
In the bin directory I have both g++.exe, and gcc.exe. I don't understand why I get "gcc" above and not "g++". I don't know how important this is, but I receive an error message when I try to compile the program in the DOS command prompt:
g++ hello.cpp
I get:
hello.cpp:1: parse error before character 0357
hello.cpp: In function 'int main()':
hello.cpp:'cout' undeclared (first use this function)
hello.cpp: (Each undeclared identifier is reported only once
hello.cpp: for each function it appears in.)
hello.cpp: 'endl' undeclared (first use this function)
And here is what I get when I try to compile the program in the cygwin shell:
hello.cpp:1: parse error before character 0357
hello.cpp: In function 'int main()':
hello.cpp:'cout' undeclared (first use this function)
hello.cpp: (Each undeclared identifier is reported only once
hello.cpp: for each function it appears in.)
hello.cpp: 'endl' undeclared (first use this function)
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files

Ah, I have thought that g++ is installed when I install cygwin. So, I should actually first install g++ in that case, shouldn't I?
(and then reboot)

As people have said, check the path. Don't start copying the cygwin DLLs around - that is not necessary, may cauxse problems later and won't solve the problem.
And when it comes to setting the Windows PATH variable (and others) I've found this small program to be quite useful and better than squinting at the MS control panel aplet.
Edit: It seems the OP had mistakenly not installed g++ - a lesson for all of us who replied in asking the obvious question first :-)

I am assuming you are using the Windows Shell here. If you invoke the compiler from a cygwin Bash shell, all the paths should be set for you.

make sure it's on the path or you're running the bash shell cmd prompt.

I wrote a tutorial about this a few years ago, which might help:
http://www.codeguru.com/cpp/misc/misc/compilerandpre-compiler/print.php/c8107__1/
Did you install g++? It's not installed by the default settings of the Cygwin installer.

Are you sure you installed g++? G++ is C++ compiler, not C compiler. If you only installed C compiler, you would have command gcc but not command g++

Make sure they c:\cygwin\bin is in the PATH.
Hope it helps.

To get the path in cygwin:
echo $PATH
This will get us on the road to understanding the problem.
Edit:
The next command will tell us where cygwin thinks it should be looking for all those files within windows
mount
The line which includes /usr/bin is the one we need a copy of.

bring up dos shell.
cd c:\cygwin\bin
type c++ or cpp
or do a directory command do see the contents. Look for cpp or c++.
If they aren't there, you haven't downloaded it.

In addition to adding it to the path (which will fix your problem), you may want to download make and dbg, which are also on the development path but not checked by default. Make sure to select "keep" as the overall install options so it doesn't re-download everything.

As others have mentioned, it's probably a path issue, so you need to look at the environment variable PATH. To do this, right click on My Computer and click "Properties". Go to the "Advanced" tab and click the "Evironment Variables" button. From here, see if the paths you mentioned are included in the PATH variable. If not, add them using the same syntax you see there to separate paths (I think it's a semi-colon or a colon between paths.)
Edit: Ah, I see you've already done this (beat my post by mere seconds.) I think all you need to do now is reboot.

Try to create a new file and type in the code yourself without copying and pasting it.
You may have an illegal character in your code which may not be visible in your editor of choice.

Related

How to use untwister?

https://github.com/altf4/untwister
I wanted to use the above program to predict some PRNG. I have read the 'usage' part, I though I should use it in cmd.exe by entering the path of the untwister. I entered the path of main.cpp, only MSVC pops up. Also, MSVC doesn't allow me to debug/compile the file, so I cannot run it. I searched 'how to use untwister' on google, but there is no further instruction. I am a beginner of programming, please forgive my ignorance.
OK, based on #drescherjm comment's, I've successfully build it.
I installed msys2 and use mingw to build it. But the .exe file has
error "_zst28__throw_bad_array_new_lengthv"
I searched the error message on google, and discover that the problem
may caused by gcc version.
I downloaded an old version (10.3.0) of gcc and mingw here:
https://winlibs.com/
I typed "cd /d D:\abc" then "mingw64\bin\mingw32-make.exe" in cmd, it
showed "Nothing to be done for" error.
I discover that I can use option to force the mingw32-make.exe to
read the Makefile. I added .am to the filename of Makefile, then type
mingw64\bin\mingw32-make.exe -f D:\abc\untwister-master\Makefile.am
The mingw32-make.exe thinks that files in
D:\abc\untwister-master\prng are in D:\abc\prng, and tells me it
can't find the files.
I moved all files and folder to D:\abc, then success!

Error while compiling my c++ program

I am a c++ beginner and after reading many articles on good ways to learn programming, I have found that its a good practice to learn programming through using command line interface than through IDE's. So therefore I am trying to learn c++ through command line interface. I am following my first tutorial of a "hello world" program. I am using MinGW compiler to compile my code. As I try to compile my code in the windows command prompt, I am getting an error. I have searched throughout the internet but can't seem to find an answer for this problem. The command I use to compile my code is "g++ Motto.cpp -o Motto.exe" and I get the problem "g++:error:CreateProcess:No such file or directory". I have checked for the path environment variable and it has the MinGW path. I have also checked the MinGW folder and found that all the executives needed are installed. Please help me fix the problem.
Here is the code:
#include <iostream>
int main()
{
std::cout << "Hello world\n";
return 0;
}
That usually means the g++ cannot find some executables that it needs to run during the compilation. Especially with MinGW the installation can be little tricky.
Make sure the paths to the MinGW installation is in your PATH environment variable (echo %PATH%) and also try to restart the computer.
If you installed manually and not with the MinGW installer (mingw-get), make sure you have downloaded and installed all the prerequisities (core, c++, binutils, runtime, etc.).
The g++ --version only prints the version, so it does not need to call another executables which the compilation does (like cc/c++, ar, etc).
Check the bin folder inside your MinGW installation directory, if you have at least those executables there: cc, c++, c++filt, ld (and others like ar, as).
Also, check {MinGWDir}\libexec\gcc\mingw32{version} if it contains cc1, cc1plus, collect2.
Try running "make $file-you-want", or like #FCo said, "g++ -o Motto Motto.cpp", The error means that you're passing it an invalid filename to compile, or you're not in the proper directory. Make sure you have the file you want to compile in your working directory, either by typing "ls" or "dir" depending on the system you're using.
Information that would help answer this question:
What you're doing to cause the error (how are you running g++?)
What OS/Environment you're running on

error while executing "make" on terminal

I am a bit newbie to using terminal so I will appreaciate it a lot if you help me to sort this out.
This small folder named mismatch came with the code I downloaded,which is supposed take four arguments like this
./mismatch/mismatch 3 1 ${SFILE}
and compute mismatch scores for protein sequences. Folder 'mismatch' contains mismatchManager.cc, makefile and a folder named "CVS".
inside the makefile,
all: mismatchManager.cc
g++ mismatchManager.cc -o ../../bin/mismatch -lm
After a quick search, I found out that -o is used when you want to name your .exe file and -lm is used to link the executable to a library which I thought is "bin" here. So when I ran "make" command from the terminal, it gave the following error.
ld: can't open output file for writing: ../../bin/mismatch, errno=2
collect2: ld returned 1 exit status
I thought that "bin" directory could be /usr/local/bin where the executables are stored so, I replaced the above path to this one, however it gave the same error. Here there is a solution for ErrNo=21 Why does the name of a source file affect compilation?, as it is told I don't have a folder named mismatchManager though.
I also changed make file into
all: mismatchManager.cc
g++ -o mismatch mismatchManager.cc
then ran "make" command. It worked well, then I copied to mismatch.exe to /usr/local/bin manually. This time I was able to pass arguments but I didn't produce any results. I have no idea what I should do.
Thanks already!
Nice linker. Can't even interpret the error code for you? Lame. Anyway, errno=2 is "access violation", which means you don't have permissions to write to or create the file ../../bin/mismatch.
I can't say why that would be since I don't know what ../../bin is on your system, but certainly changing it to /usr/local/bin won't help since you likely don't have write permissions to that directory either.
You can try just changing it to -o mismatch which will write out a file named mismatch in the current directory, which you most likely do have write permissions for.
BTW, your statement -lm is used to link the executable to a library which I thought is "bin" here is not correct. -lm is asking the linker to link in the m, or math, library. bin is not a library, it's a directory (or some people call them "folders"), and it has nothing to do with the argument -lm.

Cannot open source file "bits/c++config.h" error with icpc

I am trying to compile a program on a 64-bit machine with icpc. Unfortunately, I get an error message of catastrophic error: cannot open source file "bits/c++config.h". I have adopded some advice from here but with no success. Also, I do not forget to run source /opt/intel/bin/compilervars.sh intel64 before hand and I'm on Ubuntu 13.10 in case this is important.
First, find the missing file:
find /usr -name c++config.h
(Headers are in /usr, most of the time.) Then, add its top dir to the include path of your compilation command, so the compiler will find "bits/c++config.h", using the -I option. A similar question was already asked on SO, BTW: Missing include "bits/c++config.h" when cross compiling 64 bit program on 32 bit in Ubuntu.
For some reason the icpc compiler doesn't parse the following directory. You can set the following variable; replace 4.8 by your g++ version
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/include/x86_64-linux-gnu/c++/4.8

Can't get cygwin to compile C++ Boost libraries

I'm trying to get up and running with Boost, so I'm trying to compile the simple example problem from Boost's "Getting Started" page. I've had two issues, and I'm not sure they're related (I'm better than a novice, but not by much) but maybe they're related...
1st issue: the "tar --bzip2 -xf /path/to/boost_1_49_0.tar.bz2" command didn't work (yes, I put the correct path in, but it gave me some errors, I forget what they were) so I used "tar -xjvf " from the directory where boost_1_49_0.tar.bz2 was located. That de-compressed the zip file and I proceeded with the example...
2nd issue: The example.cpp file will not compile, the first statement in the code is #include "boost/lambda/lambda.hpp" but then for every header file lambda.hpp is trying access, there's a "No such file or directory" compile error. For example, here are two (of the six, and I get errors for all 6) header files within lambda.hpp and the errors displayed by the cygwin compiler:
boost/lambda/lambda.hpp:14:33: boost/lambda/core.hpp: No such file or directory
boost/lambda/lambda.hpp:21:52: boost/lambda/detail/operator_actions.hpp: No such file or directory
If it helps, this is the command I'm running to compile (I generally create the executable in a separate -o command):
g++ -c example.cpp
Why can't the system find these? I added the installed directory (path/to/boost_1_49_0) to the PATH variable before I started so I know that's no it. Thanks for any advice...
(I've looked on stackoverflow and there were similar issues, but no solutions that worked)
It looks like you've already solved the first issue: namely, that you must specify the -j flag on tar to untar a bzip2'd file.
For the second issue, you need to specify boost on your include path, either by specifying it with the -I command line option or via the CPLUS_INCLUDE_PATH environment variable.